// Kurt Feiereisel CSC5 Chapter 8, p.487, #4
/*******************************************************************************
*
* Validate Account Number
* _____________________________________________________________________________
* This program allows a user to enter a Charge Account number and the program
* will determine if the account number entered is valid or invalid.
* _____________________________________________________________________________
* INPUT:
* accNum : Account number of the user
*
* OUTPUT:
* Whether account number inputted is valid or invalid
*
*
* ****************************************************************************/
#include <iostream>
using namespace std;
// Function Prototypes
int inputAccNum(int accNum);
void sort(int validAccNums[], int SIZE);
void determine(int validAccNums[], int accNum, int SIZE);
int main()
{
// Initalize Variables, Constants, Arrays
int accNum = 0;
const int SIZE = 18;
int validAccNums[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277,
1302850, 8080152, 4562555, 5552012, 5050552,
7825877, 1250255, 1005231, 6545231, 3852085,
7576651, 7881200, 4581002};
// Function Call
accNum = inputAccNum(accNum);
sort(validAccNums, SIZE);
determine(validAccNums, accNum, SIZE);
return 0;
}
/*
* Definition of inputAccNum:
* This function allows a user to enter their charge account number. This
* function returns an int (num) back to main.
*/
int inputAccNum(int num)
{
// Input num
cout << "Please enter your Charge Account Number: " << endl;
cin >> num;
return num;
}
/*
* Definition of Function: sort
* This function will sort the array validAccNums into ascending order
*/
void sort(int v[], int size)
{
// Declare Local Variables
int scan;
int minIndex;
int minNum;
// For loop to Sort Array using a selection sort
for (scan = 0; scan < (size - 1); scan++)
{
minIndex = scan;
minNum = v[scan];
for(int index = scan + 1; index < size; index++)
{
if(v[index] < minNum)
{
minNum = v[index];
minIndex = index;
}
}
v[minIndex] = v[scan];
v[scan] = minNum;
}
}
/*
* Definition of Function: determine
* This function determines whether the account number entered is a valid
* number. It will then report whether the number is valid or not valid
*/
void determine(int v[], int acc, int size)
{
// Initalize Local Variables
int first = 0; // First array element
int last = size - 1; // Last array element
int middle; // Midpoint of search
int position = -1; // Position of search Value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2;
if(v[middle] == acc)
{
found = true;
position = middle;
}
else if(v[middle] > acc)
last = middle - 1;
else
first = middle + 1;
}
if(position == -1)
cout << "Invalid Account Number." << endl;
else
cout << "Valid Account Number." << endl;
}