/* Test Scores #1 - This program dynamically allocates an array large
enough to hold a user-defined number of test scores. Once all scores
are entered, the array is passed to a function that sorts them in
ascending order.
Another function is called that calculates the average score. The
program displays the sorted list of scores and averages with
appropriate headings. Pointer notation is used rather than array
notation whenever possible.
Input Validation: No negative numbers for test scores is accepted. */
#include "Utility.h"
/* Function prototypes */
void mainMenu();
void displayMenuItems();
void getTestScores(double *, const int);
void sortScores(double *, const int);
double calcAverage(double *, const int);
void displayResults(const double *, const double, const int);
int main()
{
mainMenu();
pauseSystem();
return 0;
}
/* **********************************************************
Definition: mainMenu
Allows the user to select from the following items:
* Enter Test-Score
* View Score-Data
* Quit
It takes care of calling the necessary functions, passing
and receiving results, and freeing up the memory when the
user decides to quit the program.
********************************************************** */
void mainMenu()
{
/* Dynamically allocated array */
double *testScores = nullptr;
const int GET_TEST_SCORES = 1,
VIEW_RESULTS = 2,
QUIT = 3;
int menuItem = 0,
numScores = 0;
double averageScore = 0.0;
do
{
displayMenuItems();
cin >> menuItem;
/* Validate input */
while (menuItem < GET_TEST_SCORES || menuItem > QUIT)
{
cout << "\n\tInvalid menu item selected!\n"
<< "\tYour choice: ";
cin >> menuItem;
}
switch (menuItem)
{
case GET_TEST_SCORES:
{
/* Get the number of scores */
cout << "\n\tHow many test-scores do you wish to enter? ";
cin >> numScores;
cout << "\n";
/* Dynamically allocates the array to hold the number of
test scores the user wishes to enter */
testScores = new double[numScores];
/* Gets the number of scores entered by the user */
getTestScores(testScores, numScores);
/* Sorts the scores with an ascending-order selection sort
algorithm */
sortScores(testScores, numScores);
/* Calculates the average score and returns the result */
averageScore = calcAverage(testScores, numScores);
}
break;
case VIEW_RESULTS:
{
/* Displays the data after sorting the scores and
calculating the average */
displayResults(testScores, averageScore, numScores);
}
break;
case QUIT:
{
cout << "\n\tDoing a little housekeeping ... ";
/* Free up memory */
delete[] testScores;
testScores = nullptr;
cout << "\n\ttestScores cleared: " << testScores
<< "\n\tPress enter to close this window!\n";
}
break;
}
} while (menuItem != QUIT);
}
/* **********************************************************
Definition: displayMenuItems
This function displays the menu items the user can select
between.
********************************************************** */
void displayMenuItems()
{
cout << "\n\n\t\tAVERAGE TEST SCORE CALCULATOR\n\n"
<< "\t1. Enter Test-Scores\n"
<< "\t2. View Score-Data\n"
<< "\t3. Quit\n\n"
<< "\tYour choice: ";
}
/* **********************************************************
Definition: getTestScores
This function accepts testScores and numScores, containing
the number of elements to be stored in the array, as its
arguments. It gets the scores to be stored by way of input
from the user.
********************************************************** */
void getTestScores(double *testScores, const int numScores)
{
for (int index = 0; index < numScores; index++)
{
cout << "\tScore #" << (index + 1) << ": ";
cin >> *(testScores + index);
/* Validate Input */
while ((*(testScores + index) <= 0) ||
(*(testScores + index) > 100.0))
{
cout << "\n\t\tYou entered an invalid score!\n\n";
cout << "\tScore #" << (index + 1) << ": ";
cin >> *(testScores + index);
}
}
cout << "\n";
}
/* **********************************************************
Definition: sortScores
This function accepts testScores and numScores, containing
the number of elements stored in the array, as arguments.
An ascending-order selection sort is performed on the
elements stored in the array.
********************************************************** */
void sortScores(double *testScores, const int numScores)
{
int startScan = 0,
minIndex = 0,
index = 0;
double minElem = 0.0;
for (startScan = 0; startScan < (numScores - 1); startScan++)
{
minIndex = startScan;
minElem = *(testScores + startScan);
for (index = startScan + 1; index < numScores; index++)
{
if (*(testScores + index) < minElem)
{
minElem = *(testScores + index);
minIndex = index;
}
}
*(testScores + minIndex) = *(testScores + startScan);
*(testScores + startScan) = minElem;
}
}
/* **********************************************************
Definition: calcAverage
This function accepts testScores and numScores as its
argument. It steps through the array to total the scores,
then calculates and returns the average.
********************************************************** */
double calcAverage(double *testScores, int numScores)
{
double total = 0.0,
average = 0.0;
for (int count = 0; count < numScores; count++)
{
total += *(testScores + count);
}
/* Calculates and returns the average */
return average = total / numScores;
}
/* **********************************************************
Definition: displayResults
This function accepts testScores, averageScore and
numScores as its argument. It displays the scores in
sorted order and the average score.
********************************************************** */
void displayResults(const double *testScores, const double averageScore,
const int numScores)
{
int index = 0;
cout << fixed << showpoint << setprecision(2);
cout << "\n\n\tTHE SCORES IN SORTED ORDER\n";
for (index = 0; index < numScores; index++)
{
cout << "\n\t\tSCORE #" << (index + 1) << ":\t"
<< setw(5) << right << *(testScores + index)
<< " ";
}
cout << "\n\t-------------\n"
<< "\tAVERAGE SCORE:\t\t" << averageScore << "\n";
}
Monday, March 13, 2017
Programming Challenge 9.2 - Test Scores #1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment