//Cameron Pham CS1A Chapter 9, p. 538, #3
//
/******************************************************************************
* DROP LOWEST SCORE
* ______________________________________________________________________________
* This program dynamically allocates an array to store a user-defined number of
* test scores. It accepts an integer value for every test score. The program
* then sorts the scores, calculates their average, identifies the lowest score,
* and displays the sorted scores along with the average.
*
* INPUT
* Number of scores // Total number of test scores
* Scores // Every individual test score
*
* OUTPUT
* Average // Average of test scores
* Lowest // Lowest test score
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void sortScores(int* scores, int size);
double calculateAverage(int* scores, int size);
int main()
{
int numScores;
int* scores;
double average;
// Get the number of test scores
cout << "Enter the number of test scores: ";
cin >> numScores;
while (numScores < 2)
{
cout << "Please enter at least 2 test scores: ";
cin >> numScores;
}
// Dynamically allocate the array
scores = new int[numScores];
// Get and validate each test score
for (int count = 0; count < numScores; count++)
{
cout << "Enter test score " << count + 1 << ": ";
cin >> *(scores + count);
while (*(scores + count) < 0)
{
cout << "Test scores cannot be negative. Enter again: ";
cin >> *(scores + count);
}
}
// Sort the test scores
sortScores(scores, numScores);
// Calculate the average without the lowest score
average = calculateAverage(scores, numScores);
// Display the sorted scores
cout << endl << "Sorted test scores:" << endl;
for (int count = 0; count < numScores; count++)
{
cout << *(scores + count) << endl;
}
// Display the average
cout << fixed << setprecision(2);
cout << "Average after dropping lowest score: "
<< average << endl;
// Release dynamically allocated memory
delete[] scores;
scores = nullptr;
return 0;
}
// Sort scores in ascending order using bubble sort
void sortScores(int* scores, int size)
{
int temp;
for (int pass = 0; pass < size - 1; pass++)
{
for (int count = 0; count < size - 1 - pass; count++)
{
if (*(scores + count) > *(scores + count + 1))
{
temp = *(scores + count);
*(scores + count) = *(scores + count + 1);
*(scores + count + 1) = temp;
}
}
}
}
// Calculate the average without the lowest score
double calculateAverage(int* scores, int size)
{
int total = 0;
// Start at index 1 to skip the lowest score
for (int count = 1; count < size; count++)
{
total += *(scores + count);
}
return static_cast<double>(total) / (size - 1);
}