Question: Modify the code bellow so that the lowest test score is dropped. This score should not be included in the calculation of the average. //
Modify the code bellow so that the lowest test score is dropped. This score should not be included in the calculation of the average.
// Headers
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
// Function prototype
void sort(double*, int);
double average(double*, int);
int main()
{
int numTestScores = 0;
double *testScorePtr = nullptr;
double testAverage = 0;
// get the number of test scores
cout << " How many test scores will you enter? ";
cin >> numTestScores;
// validate the input
while (numTestScores < 0)
{
cout << "The number cannot be negative. ";
cout << "Enter another number: ";
cin >> numTestScores;
}
// allocate an array to hold the test scores
testScorePtr = new double[numTestScores];
// fill the array with test scores
for (int i = 0; i < numTestScores; i++)
{
cout << "Enter test score " << (i+1) << ": ";
cin >> testScorePtr[i];
// validate the input
while (testScorePtr[i] < 0)
{
cout << " Negative scores are not allowed. ";
cout << "Enter another score for this test: ";
cin >> testScorePtr[i];
}
}
// Sort the test scores
sort(testScorePtr, numTestScores);
// get the average of the test scores
testAverage = average(testScorePtr, numTestScores);
// display the resulting data
cout << " The test scores in ascending order, and their average, are: ";
cout << " Score" << endl;
cout << " ____" << endl;
for (int j = 0; j < numTestScores; j++)
{
cout << " " << fixed << setprecision(2) << setw(6) << /**(testScorePtr + j)*/ testScorePtr[j];
}
cout << " Average score: " << setprecision(2) << fixed << testAverage << endl << endl;
// free the dynamically-allocated memory
delete[] testScorePtr;
testScorePtr = nullptr;
return 0;
}
// sort the test scores
void sort(double* score, int size)
{
int startScan, minIndex;
double minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = score[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (score[index] < minValue)
{
minValue = score[index];
minIndex = index
}
}
score[minIndex] = score[startScan];
score[startScan] = minValue;
}
}
// average function
double average(double* score, int numScores)
{
double total = 0; // accumulator
// calc the total number of scores
for (int k = 0; k < numScores; k++)
total += score[k];
// return the average score
return (total / numScores);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
