Something wrong with for loop
Nov 19, 2015 at 3:53am Nov 19, 2015 at 3:53am UTC
So our problem is:
Read in five scores and output the average; then, for each score, output the amount by which that score differs from the average.
For example
Enter a score: 65.8
Enter a score: 99
Enter a score: 87.5
Enter a score: 88
Enter a score: 100
The average is 88.06
65.8 is below average by 22.26
99 is above average by 10.94
87.5 is above average by 0.56
88 is above average by 0.06
100 is above average by 11.94
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
//CSC150 Lab11
//Ian Heinze
//11/3/2015
#include <iostream>
using namespace std;
const int NUM_SCORES = 5;
int main()
{
int i=0;
double test_scores[NUM_SCORES];
double sum=0;
double average;
while (i < NUM_SCORES)
{
cout << "Please enter the score: " ;
cin >> test_scores[i];
sum = sum + test_scores[i];
i++;
}
average = sum / NUM_SCORES;
cout << "The average is " << average;
for (int i=0, i < NUM_SCORES, i++)
{
if (test_scores [i] > average)
{
cout << test_scores [i] << " is above average by " << test_scores [1] - average << endl;
}
else if (test_scores [i] < average)
{
cout << test_scores [i] << " is below average by " << test_scores [1] - average << endl;
}
else
{
cout << test_scores [i] << " is equal to the average. " << endl;
}
}
return 0;
}
The code runs until it gets to the for loop, which is the only place I encounter any errors. What's wrong with it, and how would I begin to correct it?
Nov 19, 2015 at 4:19am Nov 19, 2015 at 4:19am UTC
Try
for (int i=0; i < NUM_SCORES; i++)
Nov 19, 2015 at 4:31am Nov 19, 2015 at 4:31am UTC
Thanks so much!
Topic archived. No new replies allowed.