// Torrez, Elaine CS1A Chapter 8 P. 487, #4
/********************************************************************************************
*
* SORT AND SEARCH CHARGE ACCOUNT NUMBER
*
* ------------------------------------------------------------------------------------------
* This program determines whether a user-entered charge account number is valid.
* It first sorts a fixed list of valid account numbers using the selection sort
* algorithm. Then, it uses the binary search algorithm to determine whether the
* entered number exists in the list.
*
* If the number is found, the program reports that the account number is valid;
* otherwise, it reports that it is invalid.
*
* Input validation ensures that the user enters a positive 7-digit number before
* performing the search.
* ------------------------------------------------------------------------------------------
*
* INPUT
* userNum : Charge account number entered by the user
*
* OUTPUT
* Message stating whether the account number is valid or invalid
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void selectionSort(int array[], int size);
int binarySearch(const int array[], int size, int value);
int main()
{
const int SIZE = 18; // Number of account numbers
int accounts[SIZE] = { // List of valid account numbers
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002
};
int userNum; // Number entered by the user
int position; // Position if found
// ----------------------------------------------------
// INPUT VALIDATION
// ----------------------------------------------------
cout << "Enter a 7-digit charge account number: ";
cin >> userNum;
while (cin.fail() || userNum < 1000000 || userNum > 9999999)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "ERROR: Enter a VALID 7-digit positive number: ";
cin >> userNum;
}
// ----------------------------------------------------
// PROCESSING: Sort the array, then search it
// ----------------------------------------------------
selectionSort(accounts, SIZE); // Sort array in ascending order
position = binarySearch(accounts, SIZE, userNum); // Perform binary search
cout << endl;
cout << fixed << setprecision(0);
// ----------------------------------------------------
// OUTPUT RESULTS
// ----------------------------------------------------
if (position != -1)
cout << "The account number is VALID." << endl;
else
cout << "The account number is INVALID." << endl;
return 0;
}
/********************************************************************************************
* SELECTION SORT
* ------------------------------------------------------------------------------------------
* This function sorts an integer array in ascending order using the selection sort
* algorithm.
********************************************************************************************/
void selectionSort(int array[], int size)
{
int minIndex, minValue;
for (int start = 0; start < size - 1; start++)
{
minIndex = start;
minValue = array[start];
for (int index = start + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
// Swap values
array[minIndex] = array[start];
array[start] = minValue;
}
}
/********************************************************************************************
* BINARY SEARCH
* ------------------------------------------------------------------------------------------
* This function performs a binary search on a sorted integer array.
* It returns the index of the value if found, or -1 if the value is not in the array.
********************************************************************************************/
int binarySearch(const int array[], int size, int value)
{
int first = 0;
int last = size - 1;
int middle;
int position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (array[middle] == value)
{
found = true;
position = middle;
}
else if (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}