// Marvyn Jesse De La Torre CS1A Chapter 9 #3 pg. 538
/*****************************************************************
* REMOVE LOWEST SCORE
*____________________________________________________________________
* This program creates an array of test scores, sorts them from lowest to highest,
* and calculates the average without the lowest score. Pointer notation is used to
* access the array elements.
*
* INPUT
* scoreCount : Number of scores (at least 2)
* scores : Nonnegative test scores
*
* OUTPUT
* Sorted scores
* Average with the lowest score removed
*****************************************************************/
#include <iomanip>
#include <iostream>
using namespace std;
int getScoreCount()
{
int count;
do
{
cout << "How many test scores will be entered (at least 2)? ";
cin >> count;
if (count < 2)
cout << "At least two scores are required.\n";
} while (count < 2);
return count;
}
int getScore(int scoreNumber)
{
int score;
do
{
cout << "Enter score " << scoreNumber << ": ";
cin >> score;
if (score < 0)
cout << "Test scores cannot be negative.\n";
} while (score < 0);
return score;
}
void sortScores(int* scores, int size)
{
for (int startScan = 0; startScan < size - 1; ++startScan)
{
int minIndex = startScan;
for (int index = startScan + 1; index < size; ++index)
{
if (*(scores + index) < *(scores + minIndex))
minIndex = index;
}
int temporary = *(scores + startScan);
*(scores + startScan) = *(scores + minIndex);
*(scores + minIndex) = temporary;
}
}
double averageWithoutLowest(const int* scores, int size)
{
int total = 0;
for (int index = 1; index < size; ++index)
total += *(scores + index);
return static_cast<double>(total) / (size - 1);
}
int main()
{
const int scoreCount = getScoreCount();
int* scores = new int[scoreCount];
for (int index = 0; index < scoreCount; ++index)
*(scores + index) = getScore(index + 1);
sortScores(scores, scoreCount);
cout << "\nScores in ascending order:\n";
for (int index = 0; index < scoreCount; ++index)
cout << *(scores + index) << " ";
cout << fixed << setprecision(2);
cout << "\nLowest score dropped: " << *scores << endl;
cout << "Average of the remaining scores: "
<< averageWithoutLowest(scores, scoreCount) << endl;
delete[] scores;
scores = nullptr;
return 0;
}