//Sam Partovi CS1A Ch. 9, P. 537, #2
/*******************************************************************************
* SORT AND CALCULATE AVERAGE OF TEST SCORES
* ____________________________________________________________
* This program dynamically allocates an array to store user-defined test
* scores. It validates input to ensure no negative scores, sorts the scores
* in ascending order, calculates the average, and displays the results.
* ____________________________________________________________
* INPUT
* scores: Dynamically allocated array to store test scores
* numScores: Number of test scores to be entered by the user
* OUTPUT
* sortedScores: Sorted list of scores in ascending order
* average: The average of the entered test scores
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void sortScores(double* scores, int numScores);
double calculateAverage(const double* scores, int numScores);
int main() {
int numScores; //INPUT - Number of test scores
//Prompt user for the number of test scores
cout << "Enter the number of test scores: ";
cin >> numScores;
//Validate input
while (numScores <= 0) {
cout << "Number of test scores must be positive. Try again: ";
cin >> numScores;
}
//Dynamically allocate memory for the test scores
double* scores = new double[numScores];
//Input test scores
for (int i = 0; i < numScores; i++) {
cout << "Enter score #" << (i + 1) << ": ";
cin >> *(scores + i);
//Validate score input
while (*(scores + i) < 0) {
cout << "Scores cannot be negative. Re-enter score #" <<
(i + 1) << ": ";
cin >> *(scores + i);
}
}
//Sort the scores in ascending order
sortScores(scores, numScores);
//Calculate the average score
double average = calculateAverage(scores, numScores);
//Display the sorted scores
cout << "\nSorted Scores:" << endl;
for (int i = 0; i < numScores; i++) {
cout << fixed << setprecision(2) << *(scores + i) << endl;
}
//Display the average score
cout << "\nAverage Score: " << fixed << setprecision(2) << average << endl;
//Free allocated memory
delete[] scores;
return 0;
}
//*****************************************************************************
//Function definition for sortScores: *
//This function sorts an array of scores in ascending order using pointer *
//notation. *
//*****************************************************************************
void sortScores(double* scores, int numScores) {
for (int i = 0; i < numScores - 1; i++) {
for (int j = i + 1; j < numScores; j++) {
if (*(scores + i) > *(scores + j)) {
double temp = *(scores + i);
*(scores + i) = *(scores + j);
*(scores + j) = temp;
}
}
}
}
//*****************************************************************************
//Function definition for calculateAverage: *
//This function calculates the average of the scores in an array using *
//pointer notation. *
//*****************************************************************************
double calculateAverage(const double* scores, int numScores) {
double total = 0.0;
for (int i = 0; i < numScores; i++) {
total += *(scores + i);
}
return total / numScores;
}