//Ava Huntington CS1A Chapter 8 Homework P. 487, #4
//
/*******************************************************************************
* VALIDATE CHARGE ACCOUNT NUMBERS
* _____________________________________________________________________________
* This program will accept a charge account number and determine whether or not
* the given number matches one in the system using a binary search.
* _____________________________________________________________________________
* INPUT
* number of accounts: given number of accounts in the system
* account number: specific account number being searched for
*
* OUTPUT
* result: true or false if account is valid or not
******************************************************************************/
#include <iostream>
using namespace std;
void selectionSort(int list[], int size);
int searchAccountsBinary(const int[], int, int);
const int SIZE = 18;
int main()
{
int accounts[SIZE] = {5658845, 8080152, 1005231, 4520125, 4562555, 6545231,
7895122, 5552012, 3852085, 8777541, 5050552, 7576651, 8451277, 7825877,
7881200, 1302850, 1250255, 4581002};
int result; //Attempts to match given number with stored one
int accountNumber; // Given charge account number
cout << "Enter charge account number:" << endl;
cin >> accountNumber;
//Sort account numbers
selectionSort(accounts, SIZE);
//Search for account number
result = searchAccountsBinary(accounts, SIZE, accountNumber);
if(result == -1)
cout << "Account number " << accountNumber << " is invalid." << endl;
else {
cout << "Account number " << accountNumber <<" is valid." << endl;
}
return 0;
}
void selectionSort(int list[], int size)
{
int startScan;
int minIndex;
int minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = list[startScan];
for(int index = startScan + 1; index < size; index++)
{
if(list[index] < minValue)
{
minValue = list[index];
minIndex = index;
}
}
list[minIndex] = list[startScan];
list[startScan] = minValue;
}
}
int searchAccountsBinary(const int list[], int numElms, int value)
{
int first = 0;
int last = numElms -1;
int middle;
int position = -1;
bool found = false;
while(!found && first <= last)
{
middle = (first + last) / 2;
if(list[middle] == value)
{
found = true;
position = middle;
}
else if (list[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
Ly9BdmEgSHVudGluZ3RvbiAgICAgICAgICAgICBDUzFBICAgICAgICAgICAgICBDaGFwdGVyIDggSG9tZXdvcmsgUC4gNDg3LCAjNAovLwovKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgogKiBWQUxJREFURSBDSEFSR0UgQUNDT1VOVCBOVU1CRVJTCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIFRoaXMgcHJvZ3JhbSB3aWxsIGFjY2VwdCBhIGNoYXJnZSBhY2NvdW50IG51bWJlciBhbmQgZGV0ZXJtaW5lIHdoZXRoZXIgb3Igbm90CiAqIHRoZSBnaXZlbiBudW1iZXIgbWF0Y2hlcyBvbmUgaW4gdGhlIHN5c3RlbSB1c2luZyBhIGJpbmFyeSBzZWFyY2guCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIElOUFVUCiAqICAgIG51bWJlciBvZiBhY2NvdW50czogICAgIGdpdmVuIG51bWJlciBvZiBhY2NvdW50cyBpbiB0aGUgc3lzdGVtCiAqICAgIGFjY291bnQgbnVtYmVyOiAgICAgICAgIHNwZWNpZmljIGFjY291bnQgbnVtYmVyIGJlaW5nIHNlYXJjaGVkIGZvcgogKiAKICogT1VUUFVUCiAqICAgIHJlc3VsdDogICAgICAgICAgICAgICAgIHRydWUgb3IgZmFsc2UgaWYgYWNjb3VudCBpcyB2YWxpZCBvciBub3QKICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCiB2b2lkIHNlbGVjdGlvblNvcnQoaW50IGxpc3RbXSwgaW50IHNpemUpOwogaW50IHNlYXJjaEFjY291bnRzQmluYXJ5KGNvbnN0IGludFtdLCBpbnQsIGludCk7CiBjb25zdCBpbnQgU0laRSA9IDE4OwogCmludCBtYWluKCkKewoJaW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