// Torrez, Elaine CS1A Chapter 9, P. 537, #2
/********************************************************************************************
*
* TEST SCORES (DYNAMIC)
*
* ------------------------------------------------------------------------------------------
* This program dynamically allocates an array to store test scores entered by the user.
* After storing the scores, the program sorts them in ascending order, calculates the
* average score, and then displays the sorted list along with the average.
*
* ------------------------------------------------------------------------------------------
* INPUT
* numScores : Number of test scores the user wants to enter
* scores : Individual test scores entered by the user
*
* OUTPUT
* Sorted list of test scores
* Average test score
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// FUNCTION PROTOTYPES
void sortArray(double *arr, int size);
double getAverage(double *arr, int size);
int main()
{
int numScores; // Number of scores to enter
double *scores = nullptr;
// INPUT: Ask user how many scores they want to enter
cout << "How many test scores would you like to enter? ";
cin >> numScores;
// INPUT VALIDATION
while (numScores <= 0)
{
cout << "Error: Enter a value greater than 0: ";
cin >> numScores;
}
// Dynamically allocate the array
scores = new double[numScores];
// INPUT: Enter each test score
for (int i = 0; i < numScores; i++)
{
cout << "Enter score #" << (i + 1) << ": ";
cin >> scores[i];
while (scores[i] < 0) // No negative scores allowed
{
cout << "Error: Score must be 0 or higher. Re-enter: ";
cin >> scores[i];
}
}
// Sort the array
sortArray(scores, numScores);
// OUTPUT: Display sorted list
cout << "\nSorted Test Scores:\n";
for (int i = 0; i < numScores; i++)
{
cout << fixed << setprecision(2);
cout << "Score [" << i << "]: " << scores[i] << endl;
}
// OUTPUT: Display average
cout << "\nAverage Score: " << getAverage(scores, numScores) << endl;
// Free memory
delete [] scores;
scores = nullptr;
return 0;
}
//*******************************************************************************
// sortArray
//------------------------------------------------------------------------------
// Sorts an array of doubles in ascending order using simple selection sort.
//*******************************************************************************
void sortArray(double *arr, int size)
{
int minIndex;
double minValue;
for (int start = 0; start < size - 1; start++)
{
minIndex = start;
minValue = arr[start];
for (int i = start + 1; i < size; i++)
{
if (arr[i] < minValue)
{
minValue = arr[i];
minIndex = i;
}
}
arr[minIndex] = arr[start];
arr[start] = minValue;
}
}
//*******************************************************************************
// getAverage
//------------------------------------------------------------------------------
// Returns the average value of all test scores stored in the array.
//*******************************************************************************
double getAverage(double *arr, int size)
{
double total = 0;
for (int i = 0; i < size; i++)
total += arr[i];
return (total / size);
}