//Zachary Abdollahi CS1A Chapter 9, P. 538, #3
//
/*******************************************************************************
*
* CALCULATE AVERAGE WITHOUT LOWEST SCORE
* _____________________________________________________________________________
* This program dynamically allocates an array to store a user-defined
* number of test scores, sorts the scores in ascending order, drops the
* lowest score, and computes the average of the remaining scores.
*
* Computation is based on the formula:
* Average = (Sum of All Scores - Lowest Score) / (Number of Scores - 1)
* _____________________________________________________________________________
* 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 score after dropping the lowest score
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
void sortScores(double *, int);
double calcAverageDropLowest(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 score without lowest score
// Prompt User for Number of Test Scores
cout << "How many test scores will you enter? ";
cin >> numScores;
// Validate Input (Must be at least 2 to drop 1 and average remaining)
while (numScores < 2)
{
cout << "Please enter at least 2 test scores: ";
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 Dropping Lowest Score
average = calcAverageDropLowest(scores, numScores);
// Output Sorted Scores and Final Average
cout << fixed << showpoint << setprecision(2);
cout << "\nSorted Test Scores (Lowest Dropped):\n";
for (int count = 0; count < numScores; count++)
{
cout << *(scores + count);
if (count == 0)
cout << " (Dropped)";
cout << 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: calcAverageDropLowest
* Sums all elements after index 0 (the lowest score in a sorted array)
* and divides by (size - 1).
******************************************************************************/
double calcAverageDropLowest(const double *arr, int size)
{
double total = 0.0;
// Start at index 1 to skip the lowest score at *(arr + 0)
for (int count = 1; count < size; count++)
{
total += *(arr + count);
}
return total / (size - 1);
}