1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <iostream>
using namespace std;
int binarySearch(int[], int, int);
void binarySort(int[], int);
// , 11/26/2016, PROGRAM CHALLENGE 4: CHARGE ACCOUNT VALIDATION MODIFICATION
int main()
{
int num, account;
// ACCOUNT NUMBERS USING SINGLE DIMENSION ARRAY
int accountNum[18] = { 5658845, 4520125, 7895112, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002 };
for (int i = 0; i < 18; i++)
{
cout << accountNum[i] << endl;
}
cout << "Enter account number you are searching for: ";
cin >> num;
// CALL FUNCTION TO SORT ACCOUNT NUMBERS FOR BINARY SEARCH
binarySort(accountNum, 18);
for (int i = 0; i < 18; i++)
// CALL FUNCTION TO SEARCH FOR ACCOUNT USING BINARY SEARCH
account = binarySearch(accountNum, 18, num);
// DISPLAY WHETHER ACCOUNT NUMBER IS VALID OR INVALID
if (account == -1)
cout << "Account is invalid." << endl;
else
cout << "Account number " << accountNum[account] << " is valid." << endl;
system("pause");
return 0;
}
// FUNCTION FOR USING BINARY SEARCH FOR ACCOUNT NUMBER
int binarySearch(int array[], int SIZE, int searchNum)
{
bool found = false;
int mid, first, last;
first = 0;
last = (SIZE - 1);
while ( found == false && first <= last)
{
mid = (first + last) / 2;
if (array[mid] == searchNum)
{
found = true;
return mid;
}
else if (array[mid] > searchNum)
last = (mid - 1);
else
first = (mid + 1);
}
return 5;
}
void binarySort(int array[], int SIZE)
{
int minValue, minIndex;
for (int scan = 0; scan < (SIZE - 1); scan++)
{
minIndex = scan;
minValue = array[scan];
for (int i = scan + 1; i < SIZE; i++)
{
if (array[i] = minValue)
{
minValue = array[i];
minIndex = i;
}
}
array[minIndex] = array[scan];
array[scan] = minValue;
}
}
|