Code works well except it doesn’t drop the lowest score and then calculates it.
Output Example:
How many test scores would you like to enter?
3
Enter the test score desired:
Score 1: 58
Score 2: 96
Score 3: 78
Test Scores Average with the lowest dropped is: 116.00
Problem:
As you can see on the output example, that is incorrect. It must display the average not including the lowest. Could you review my code and let me know where I have gone wrong? I’ve had several people look at it too and they’ve not been able to see any bugs in my code. Below is my code:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//To dynamically allocate an array, Accumulator, to hold the average scores.
double *score;
double total = 0;
double average;
//int for counter, to hold the number of test scores.
int count;
int numTest;
// To obtain the number of test scores the user would like to enter.
cout << "How many test scores would you like to enter? " << endl;
cin >> numTest;
//Dynamically allocates an array large enough to hold the amount of test scores to enter.
score = new double[numTest];
//Get the test scores.
cout << "Enter the test score desired. " << endl;
for (count = 0; count < numTest; count++)
{
cout << "Score " << (count + 1) << ": ";
cin >> score[count];
}
//Find lowest score.
int lowest = score[count];
for (count = 1; count < numTest; count++)
{
if (score[count] < lowest)
lowest = score[0];
}
//Calculate the total test scores.
for (count = 0; count < numTest; count++)
{
total += score[count];
total -= lowest;
}
//Calculate the test scores average minus the lowest score.
average = total / (numTest - 1);
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Test Scores Average with the lowest dropped is: " << average << endl;
//Free dynamically allocated memory
delete [] score;
score = 0; // Makes score point to null.
system("pause");
return 0;
}
You have three significant errors in this code:
First, the initial value of
lowest:The error is here:
It needs to be this:
Next, within the loop:
Should be:
So your loop should look like this:
Finally, the calculation of the total is also wrong. Subtract the lowest score after calculating the total of all scores, then divide by the number of scores less-one:
Should be :