//Zachary Abdollahi CS1A Chapter 9, P. 537, #2
//
/*******************************************************************************
*
* SORT AND AVERAGE TEST SCORES
* _____________________________________________________________________________
* This program dynamically allocates an array to hold a user-defined
* number of test scores, sorts the scores in ascending order using
* pointer notation, and calculates the average score.
*
* Computation is based on the formula:
* Average = Sum of All Scores / Number of Scores
* _____________________________________________________________________________
* INPUT
* numScores : Number of test scores to process
* scores : Dynamically allocated array of test scores
*
* OUTPUT
* scores : Test scores sorted in ascending order
* average : Average of all test scores
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
void sortScores(double *, int);
double calcAverage(const double *, int);
int main()
{
int numScores; //INPUT - Number of test scores to process
double *scores; //INPUT - Pointer to dynamic array of scores
double average; //OUTPUT - Average of all test scores
// Prompt User for Number of Test Scores
cout << "How many test scores will you enter? ";
cin >> numScores;
// Validate Positive Number of Scores
while (numScores <= 0)
{
cout << "Please enter a number greater than 0: ";
cin >> numScores;
}
// Dynamically Allocate Array for Test Scores
scores = new double[numScores];
// Read Test Scores Using Pointer Arithmetic
for (int count = 0; count < numScores; count++)
{
cout << "Enter test score #" << (count + 1) << ": ";
cin >> *(scores + count);
// Validate Non-Negative Score
while (*(scores + count) < 0)
{
cout << "Negative scores are not allowed. Enter again: ";
cin >> *(scores + count);
}
}
// Sort Scores in Ascending Order
sortScores(scores, numScores);
// Compute Average Score
average = calcAverage(scores, numScores);
// Output Sorted Scores and Average
cout << fixed << showpoint << setprecision(2);
cout << "\nSorted Test Scores:\n";
for (int count = 0; count < numScores; count++)
{
cout << *(scores + count) << endl;
}
cout << "\nAverage Score: " << average << endl;
// Free Dynamically Allocated Memory
delete [] scores;
scores = nullptr;
return 0;
}
/*******************************************************************************
* Function: sortScores
* Performs an ascending selection sort on an array of doubles using
* pointer notation.
******************************************************************************/
void sortScores(double *arr, int size)
{
int startScan, minIndex;
double minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = *(arr + startScan);
for (int index = startScan + 1; index < size; index++)
{
if (*(arr + index) < minValue)
{
minValue = *(arr + index);
minIndex = index;
}
}
*(arr + minIndex) = *(arr + startScan);
*(arr + startScan) = minValue;
}
}
/*******************************************************************************
* Function: calcAverage
* Computes the arithmetic mean of an array of doubles using pointer
* notation.
******************************************************************************/
double calcAverage(const double *arr, int size)
{
double total = 0.0;
for (int count = 0; count < size; count++)
{
total += *(arr + count);
}
return total / size;
}