// Andres Guzman CSC5 Chapter 8, P. 487, #4
/******************************************************************************
*
* DETERMINE NUMBER VALIDATION
* ____________________________________________________________________________
* This program will determine whether the inputed numbers in the account match
* those of the valid numbers.
*
* Computation:
*
* Uses a selection sort to sort the numbers from lowest to highest and then
* uses binary search in the binary function to divide the values in halves in
* the search range to the input values of the user until the values are either
* equal or invalid.
* ____________________________________________________________________________
*
* INPUT
* account = The valid numbers
* accountnum = Holds the inputed value for account numbers
*
* OUTPUT
* array = Displays the status of the inputed numbers
*
******************************************************************************/
#include <iostream>
using namespace std;
int binary(const int a[], int b, int c); //Function Prototype
void selectionsort(int a[], int b); //Function Prototype
const int SIZE = 18;
int main()
{
//
//Initialization List
int account[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277, 302850,
8080125, 4562555, 5552012, 5050552, 7825877, 1250255,
1005321, 6545231, 3852085, 7576651, 7881200, 4581002};
int accountnum; //Holds Input Value for users account numbers
int array; //Holds the results of the numbers
//
//Prompt Input Display
cout << "Please enter a Number: ";
cin >> accountnum;
//
//Input Validation
while(accountnum < 0)
{
cout << "Cannot Be Negatives. Please Try Again." << endl;
cout << "Please enter a Number: ";
cin >> accountnum;
}
//
//
selectionsort(account, SIZE);
//
//Adds Function Results to Variable Status
array = binary(account, SIZE, accountnum);
//
//Determines whether the inputed values are equal to the subscript
if(array == -1)
cout << "\nInvalid." << endl;
else
cout << "\nValid" << endl;
return 0;
}
/*******************************************************************************
* Adding Definition to the function. This function will search through the list
* of values in the sorted array by halves and determine whether the value
* inside matches the inputed value of the user using a binary search.
******************************************************************************/
int binary(const int a[], int b, int c)
{
int first = 0;
int last = b - 1;
int middle;
int position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if(a[middle] == c)
{
found = true;
position = middle;
}
else if(a[middle] > c)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
/*******************************************************************************
* Adding definition to Function selectionsort. This function sorts the account
* amounts from lowest to highest. Uses selection sort to find the lowest
* value and moves it into the correct position.
******************************************************************************/
void selectionsort(int a[], int b)
{
int startscan, minIndex; //Used to scan through the array
int minValue; //Holds lowest value found
//
//Initializing For Loop
for (startscan = 0; startscan < (b - 1); startscan++)
{
minIndex = startscan; //Assigns starting position as lowest
minValue = a[startscan]; //Assigns starting number as lowest
//
//Searches for lowest account number value
for (int index = startscan + 1; index < b; index++)
{
if(a[index] < minValue)
{
minValue = a[index]; //Updates lowest number value
minIndex = index; //stores position of lowest value
}
}
//
//Swaps Values
a[minIndex] = a[startscan];
a[startscan] = minValue;
}
}