// Natalie Zarate CSC5 Chapter 8, P. 487, #4
/*******************************************************************************
*
* DETERMINE NUMBER VALIDITY
*
* This program will determine whether or not the user's input is valid using an
* existing database. It will accept as input a positive integer then confirm
* whether or not the entered value is vaid or invalid.
*______________________________________________________________________________
* INPUT
*
* accountNums[elements] - reference account numbers
* validNum - determining value for validity
* userAccNum - inputted account number
* AccountNumSort(accounts[], size) - function, sorts variables
* sort - value that determines when values
* are moved to be sorted in an array
* keepVal - refernce value for comarison
* first - first array element
* last - last array element
* middle - midpoint array element; reference
* point
* AccountValid(accounts[], size, number) - function, determines if inputted
* account number is valid.
* found - indicates if inputted account number
* is valid
*
* OUTPUT
*
* ValidNum - determines if inputted account value
* is valid
******************************************************************************/
#include <iostream>
using namespace std;
// Prototype for AccountValid
int AccountValid(int accounts[], int size, int number);
// Prototype for AccountNumSort
void AccountNumSort(int accounts[], int size);
int main()
{
int elements = 16;
// INPUT - reference account numbers
int accountNums[elements] = {5658845, 4520125, 7895122, 8777541, 8451277,
1302850, 8089152, 4562555, 5552012, 5050552,
7825877, 1250255, 1005231, 3852085, 7881200,
4581002};
int validNum; // INPUT - OUTPUT - determines if inputted account value
// is valid
int userAccNum; // INPUT - user's account number
// Prompt user for account number
cout << "Enter Charge Account Number: " << endl;
cin >> userAccNum;
// Rearrange array
AccountNumSort(accountNums, elements);
// Check if valid account number; Call AccountValid
validNum = AccountValid (accountNums,elements, userAccNum);
if (validNum == -1)
cout << "Invalid Number" << endl;
else
cout << "Valid Number" << endl;
return 0;
}
/*******************************************************************************
* AccountNumSort(accounts, size)
*
* This function will take the database and sort it from least to greatest.
* _____________________________________________________________________________
* INPUT
*
* sort - value that flags when values are moved for sorting in database
* keepVal - refernce value for comarison/sorting
*
* OUTPUT
*
* account[size] - sorted database, database of account number
******************************************************************************/
void AccountNumSort(int accounts[], int size)
{
bool sort; // INPUT - value that flags when values are moved for sorting
// in database
int keepVal; // INPUT - refernce value for comarison/sorting
do
{
// initialize sort as a boolean expression
sort = false;
for (int i = 0; i < (size - 1); i++)
{
if (accounts[i] > accounts[i + 1])
{
keepVal = accounts[i];
accounts[i] = accounts[i+1];
accounts [i+1] = keepVal;
sort = true;
}
}
}
while (sort);
}
/*******************************************************************************
* AccountValid(accounts[], size, number)
*
* This function will determine whether or not the user inputted value is valid
* or not.
* _____________________________________________________________________________
* INPUT
*
* first - first account number in database
* last - last account number in database
* middle - midpoint reference account number
* found - determinator for number validity
*
* OUTPUT
*
* position - reference variable, dictates input validity
******************************************************************************/
int AccountValid(int accounts[], int size, int number)
{
int first = 0; // INPUT - first account number in database
int last = size - 1; // INPUT - last account number in database
int middle; // INPUT - midpoint reference account number
bool found; // INPUT - determinate for number validity
int position = -1; // OUTPUT - reference variable, dictates input validity
// Initialize found
found = false;
while (!found && first <= last)
{
// find midpoint value
middle = (first + last) / 2;
// if account number is midpoint
if (accounts[middle] == number)
{
found = true;
position = middle;
}
// if account number if found in lower half of array
else if (accounts[middle] > number)
// Reinitate value
last = middle - 1;
// if account number is in upper half of array
else
first = middle + 1;
}
return position;
}