//Cameron Pham CS1A Chapter 8, p. 487, #4
//
/******************************************************************************
* CHARGE ACCOUNT VALIDATION
* ______________________________________________________________________________
* This program accepts an integer value for an account number and determines
* if the number is valid.
* ______________________________________________________________________________
* INPUT
* Account number // Charge account number
*
* OUTPUT
* Validation // Whether or not the account exists or not
*******************************************************************************/
#include <iostream>
using namespace std;
int main()
{
// Initialize Variables
const int SIZE = 18;
int accountNumbers[SIZE] =
{
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002
};
int accountNumber; // Account number entered by the user
int minIndex; // Index of the smallest value
int temp; // Temporary variable for swapping
int first; // First array position to search
int last; // Last array position to search
int middle; // Middle array position
bool isValid = false; // Indicates whether the account is valid
// Sort the array using selection sort
for (int start = 0; start < SIZE - 1; start++)
{
minIndex = start;
for (int count = start + 1; count < SIZE; count++)
{
if (accountNumbers[count] < accountNumbers[minIndex])
{
minIndex = count;
}
}
// Swap the smallest value into its correct position
temp = accountNumbers[start];
accountNumbers[start] = accountNumbers[minIndex];
accountNumbers[minIndex] = temp;
}
// Ask the user to enter a charge account number
cout << "Enter a charge account number: " << endl;
cin >> accountNumber;
// Initialize binary search positions
first = 0;
last = SIZE - 1;
// Search the sorted array using binary search
while (first <= last)
{
middle = (first + last) / 2;
if (accountNumbers[middle] == accountNumber)
{
isValid = true;
break;
}
else if (accountNumbers[middle] > accountNumber)
{
last = middle - 1;
}
else
{
first = middle + 1;
}
}
// Display whether the account number is valid
if (isValid)
{
cout << "The account number is valid." << endl;
}
else
{
cout << "The account number is invalid." << endl;
}
return 0;
}