//Jonathan Estrada CSC5 Chapter 8, P.487, #4
/*******************************************************************************
* VALIDATE ACCOUNT NUMBERR
* _____________________________________________________________________________
* This program will determine if the users account number is in the system or
* not. It will notify the user whether the number went through
* _____________________________________________________________________________
* INPUTS
* accountNumber : Account Numbers
* SIZE : array size
* userAccount : Users account number
*
* OUTPUT
*
*
*******************************************************************************/
#include <iostream>
using namespace std;
int accountSearch( const int[], int, int);
void selectonSort(int[], int);
const int SIZE = 18;
int main() {
int accountNumber[SIZE] = {5658845, 4530125, 7895122, 8777541, 8451277,
1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255, 1005231,
6545231, 3852085, 7576651, 7881200, 4581002};
int userAccount;
int result;
selectonSort(accountNumber, SIZE);
cout << "Please enter number associated with the account: ";
cin >> userAccount;
cout << userAccount << endl;
result = accountSearch(accountNumber, SIZE, userAccount);
if(result == -1)
cout << "Account Number is not valid." << endl;
else
cout << "Account Number was found and valid." << endl;
return 0;
}
/*******************************************************************************
* Definition of function selectionSort.
* This function will sort all the account numbers for use of the binary search
* later on.
* *****************************************************************************/
void selectonSort(int accountNumber[], int SIZE)
{
int startScan, minIndex, minValue;
for(startScan = 0; startScan < (SIZE + 1); startScan++)
{
minIndex = startScan;
minValue = accountNumber[startScan];
for(int index = startScan +1; index < SIZE; index++)
{
if(accountNumber[index] < minValue)
{
minValue = accountNumber[index];
minIndex = index;
}
}
accountNumber[minIndex] = accountNumber[startScan];
accountNumber[startScan] = minValue;
}
}
/*******************************************************************************
* Definiton of function accountSearch
* This funcion will conduct a binary search to determine if the user inputted
* a valid account number or not.
* *****************************************************************************/
int accountSearch(const int accountSearch[], int SIZE, int userAccount)
{
int first = 0;
int last = SIZE - 1;
int middle;
int positon = - 1;
bool found = false;
while(!found && first <= last)
{
middle = (first + last)/2;
if(accountSearch[middle] == userAccount)
{
found = true;
positon = middle;
}
else if (accountSearch[middle]>userAccount)
last = middle - 1;
else
first = middle + 1;
}
return positon;
}