/* Charge Account Validation Modification - This program lets the user
enter a charge account number. The program determines if the number
is valid by checking for it in the following list:
5658845 4520125 7895122 8777541 8451277 1302850
8080152 4562555 5552012 5050552 7825877 1250255
1005231 6545231 3852085 7576651 7881200 4581002
The list of numbers above is initialized in a single-dimensional array.
A simple linear search is used to locate the number entered by the user.
If the user enters a number that is in the array, the program displays
a message saying that the number is valid. If the user enters a number
that is not in the array, the program displays a message indicating that
the number is invalid.
This program is a modification of Programming Challenge 8.1. It performs
a binary search to locate valid account numbers. The selection sort
algorithm is used to sort the array before the binary search is performed. */
#include "Utility.h"
/* Function prototypes */
int getNumber();
void selectionSort(int[], int);
void displaySorted(const int[], int);
void searchNumber(const int[], const int, int);
int main()
{
/* Number of elements in the array */
const int NUMELS = 18;
/* Array initialized with a list of account numbers */
int accNumbers[NUMELS] = { 5658845, 4520125, 7895122, 8777541,
8451277, 1302850, 8080152, 4562555,
5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651,
7881200, 4581002 };
int secretNum = 0;
selectionSort(accNumbers, NUMELS);
/* Display the sorted list */
/* displaySorted(accNumbers, NUMELS); */
secretNum = getNumber();
searchNumber(accNumbers, secretNum, NUMELS);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getNumber
This function asks the user for his or her secret account
verfication number and returns it.
********************************************************** */
int getNumber()
{
int secretNum = 0;
cout << "\n\t\tAshikaga Bank - Credit Authorization Terminal\n\n"
<< "Please enter your secret number: ";
cin >> secretNum;
return secretNum;
}
/* **********************************************************
Definition: selectionSort
This function accepts accNumbers and NUMELS, containing
the number of elements in the array, as its argument. It
performs an ascending order selection sort.
********************************************************** */
void selectionSort(int accNumbers[], int NUMELS)
{
int startScan = 0,
minIndex = 0,
minValue = 0,
index = 0;
for (startScan = 0; startScan < (NUMELS - 1); startScan++)
{
minIndex = startScan;
minValue = accNumbers[startScan];
for (index = startScan + 1; index < NUMELS; index++)
{
if (accNumbers[index] < minValue)
{
minValue = accNumbers[index];
minIndex = index;
}
}
accNumbers[minIndex] = accNumbers[startScan];
accNumbers[startScan] = minValue;
}
}
/* **********************************************************
Definition: searchNumber
This function accepts accNumbers, containing a list of int
numbers secretNumber, and NUMELS as arguments. It performs
a binary search on the array. If a match has been found,
the user is informed that his or her entry was valid. If
no match could be found, a message indicating that the
secret number was not found is displayed.
********************************************************** */
void searchNumber(const int accNumbers[], const int secretNum,
int NUMELS)
{
/* Variables: First array element, Last array element, Midpoint of
search, Position of search value */
int firstElem = 0,
lastElem = NUMELS - 1,
midpoint = 0,
position = - 1;
/* Flag to indicate a match */
bool isFound = false;
while (!isFound && firstElem <= lastElem)
{
/* Calculate the midpoint */
midpoint = (firstElem + lastElem) / 2;
/* If the arrays midpoint contains the secret number, isFound is set to
true and position is set to midpoint. Else if accNumbers midpoint is greater
than secretNum, lastElement is set as new midpoint, otherwise if the
number is smaller, firstElem is set as the new midpoint to perform the next
iteration of the search. */
accNumbers[midpoint] == secretNum ? isFound = true, position = midpoint :
accNumbers[midpoint] > secretNum ? lastElem = midpoint - 1 :
firstElem = midpoint + 1;
}
isFound ? cout << "\nYour secret number has been successfully verified!" :
cout << "\nYour secret number was not found in our database ...";
}
/* **********************************************************
Definition: displaySorted
This function verifies that the list of numbers stored
in accNumbers has been sorted correctly by displaying its
contents.
********************************************************** */
void displaySorted(const int accNumbers[], int NUMELS)
{
for (int index = 0; index < NUMELS; index++)
{
cout << accNumbers[index] << " ";
cout << "\n";
}
}
Tuesday, March 7, 2017
Programming Challenge 8.4 - Charge Account Validation Modification
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment