//Roman Lara Delgado CSC5 Chapter 8, P.487, #4
//
/*******************************************************************************
*
* Identify Invalid Account Number
*_______________________________________________________________________________
* This program determines if an account number is invalid meaning it does not
* exist in a set of account numbers.
* ______________________________________________________________________________
* INPUT
* accNumber_user : Account number entered by the user.
*
* OUTPUT
* A message that says whether the account number entered
* is valid or not.
* ******************************************************************************
* FUNCTION
* ******************************************************************************
* int searchList(const int [], int, int)
* ------------------------------------------------------------------------------
* This function locates if a value exists in an array.
*
* PARAMETERS:
* array[] : receives the values of an array.
* size : receives the number of elements
* of the array.
* value : receives a value that the user
* wanted to determine whether the
* value exists in the array or not.
* ______________________________________________________________________________
* void selectionSort(int[], int)
* ------------------------------------------------------------------------------
* This function arranges the data in ascending order
*
* PARAMETERS:
* array[] : Array to arrange it in ascending order.
* size : The number of elements of the array.
*******************************************************************************/
#include <iostream>
using namespace std;
void selectionSort(int[], int); //Function prototype
int binarySearch(const int [], int, int); //Function prototype
const int SIZE = 18; //GLOBAL - Array's size.
int main ()
{
//Array that stores the account numbers of the data base.
int accNumber_saved[SIZE] = {5658845, 8080152, 1005231, 4520125, 4562555,
6545231, 7895122, 5552012, 3852085, 8777541,
5050552, 7576651, 8451277, 7825877, 7881200,
1302850, 1250255, 4581002};
int accNumber_user; //INPUT - Account number entered by the user.
int results; //Variable that determines if a number is valid or not.
//Enter the account number by the user.
cout << "Enter your account number: " << endl;
cin >> accNumber_user;
//Arrange the array in ascending order.
selectionSort(accNumber_saved, SIZE);
//Assign a value to results by calling the function searchList
results = binarySearch(accNumber_saved, SIZE, accNumber_user);
//if results is -1, then the number is invalid.
if(results == -1)
{
cout << "The number is invalid.";
}
else //Otherwise is valid.
{
cout << "The number is valid.";
}
return 0;
}
void selectionSort (int array[], int size)
{
int startScan, //First element of the array[0].
minIndex, //Minimum index.
minValue; //Minimun value.
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan; //Assign starScan in minIndex
minValue = array[startScan]; //Assign the firt element of the array.
for(int index = startScan + 1; index < size; index++)
{
if(array[index] < minValue) //If an element is less than array[0].
{
minValue = array[index]; //Assign the element.
minIndex = index; //Assign the subscript.
}
}
array[minIndex] = array[startScan]; //Assign the new value at first.
array[startScan] = minValue;
}
}
//binarySearch definition
int binarySearch(const int array[], int size, int value)
{
int first = 0, //First array element.
last = size - 1, //Last array element.
middle, //Midpoint of search.
position = -1; //Position of search value.
bool found = false; //Flag.
while (!found && first <= last)
{
middle = (first + last) /2; //Calculate midpoint.
if(array[middle] == value) //If value is found at mid.
{
found = true;
position = middle;
}
else if (array[middle] > value) //If value is in lower half.
{
last = middle - 1;
}
else
{
first = middle + 1; //If value is in upper half.
}
}
return position;
} //End binarySearch