What could possibly be wrong here?
My code runs, but is not calculating the average correctly. It is returning 15.0333, when it should be 15.1...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
double score[5] = {0.0,0.0,0.0,0.0,0.0};
double lowest = 0.0;
double highest = 0.0;
double avg = 0.0;
double sum = 0.0;
for (int i = 0.0; i<5; i = i + 1)
{
score[i] = 0.0;
}
cout << "You will be asked to enter 5 scores."<< endl;
for (int i = 0.0; i<5; i = i + 1)
{
cout << "Please enter a score: ";
cin >> score[i];
}
lowest = score[0];
for (int i=1; i<5; i=i+1)
{
if (score[i] < lowest)
{
lowest = score [i];
}
}
highest = score[0];
for (int i=1; i<5; i=i+1)
{
if (score[i] > highest)
{
highest = score[i];
}
}
for (int i=1; i<5; i=i+1)
{
avg = (score[i]+score[i]+score[i]+score[i]+score[i] - highest - lowest)/3;
}
cout << "Lowest Score: " <<lowest<<endl;
cout << "Highest Score: " <<highest<<endl;
cout << "Average score with highest score and lowest score excluded: " <<avg<<endl;
system("pause");
return 0;
}
|
Last edited on
Some hints (which may be out of your questions scope):
Line 14, 20: You may better initialize an int value with the integer 0 instead of 0.0. But its only cosmetics.
Line 14..17:
score is just initialized. So these lines are obsolete.
Line 41..44: You're assigning some values to
avg which are never read. Only the last calculated one will be used on line 48.
Maybe you wanted to write:
1 2 3 4 5 6
|
avg = 0;
for (int i = 1; i < 5; i = i + 1)
{
avg = avg + score[i];
}
avg = (avg - highest - lowest) / 3.0;
|
Topic archived. No new replies allowed.