Class averages

I am attempting to rework a previous assignment I had that uses the code listed below to show the highest of 10 grades.

I need it now to also show the average of these 10 grades. Will I be able to use this to find the average? I was thinking that it didn't save the grades into memory only the kept the larger of the two and moved on so I wouldn't actually be able to use this part of the code. Thanks for any help.

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
  #include <iostream>
  double larger(double a, double b);

  int main()
  {
     const int N = 10;
     
     std::cout << "Please, enter " << N << " grades, one at a time, \n"
         "and the program will return the highest grade and the average of all ten.\n" << std::endl;
     double num;
     
     std::cout << "Enter the first grade: ";
     std::cin >> num;

     double largest_so_far = num;

     for (int i = 1; i < N; ++1)
     {
          std::cout << "Enter another grade: " ;
          std::cin >> num;

          largest_so_far = larger(largest_so_far, num);
     }
     std::cout << "The highest grade is: " << largest_so_far << '\n';
     system("pause");
     return(0);
}
double larger(double a, double b) { return a < b ? b : a; }
While you are receiving the input, add the numbers (in your code the user inputs values to num) to a single number average. At the end, divide average by the number of numbers the user entered N.
Last edited on
Hi,
You declare a variable double total = 0;

After an user inputs a number, you add the number to (total) :
total = total + num;

You add another line to output the average grade :
1
2
3
std::cout << "The highest grade is: " << largest_so_far << '\n';

std::cout << "The average grade is: " << (total / (double)N) << '\n';
You are writing the for-loop the wrong way.
for (int i = 1; i < N; ++1)

The correct for-loop should be :
for (int i = 1; i <= N; ++i)
Does that help you? :)
Almost perfect :P
it seems to function correctly but the answer is incorrect, at least the way I did it.

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
  #include <iostream>
  double larger(double a, double b);

  int main()
  {
     const int N = 10;
     
     std::cout << "Please, enter " << N << " grades, one at a time, \n"
         "and the program will return the highest grade and the average of all ten.\n" << std::endl;
     double num;
     double total = 0;     

     std::cout << "Enter the first grade: ";
     std::cin >> num;

     total = total + num; // I think the problem is here 

     double largest_so_far = num;

     for (int i = 1; i < N; ++1)
     {
          std::cout << "Enter another grade: " ;
          std::cin >> num;
          total = total + num;

          largest_so_far = larger(largest_so_far, num);
     }
     std::cout << "The highest grade is: " << largest_so_far << "%" << '\n';
     std::cout << "The average grade is: " << (total / (double)N) << "%" << '\n';

     system("pause");
     return(0);
}
double larger(double a, double b) { return a < b ? b : a; }
Please don't make me post the same message again.

You are writing the for-loop the wrong way.

So see above.
oh, sorry... that was actually just a typo. I'm writing the code on a computer that doesn't have internet conn. so I'm having to copy everything over manually to this computer.

I fixed the for loop as stated in your first post, but I'm still getting a calculation error.

for instance I input from 90-100 and the resulting average was 100.4%
Seems to work correctly for me:

Please, enter 10 grades, one at a time,
and the program will return the highest grade and the average of all ten.

Enter the first grade: 90
Enter another grade: 91
Enter another grade: 92
Enter another grade: 93
Enter another grade: 94
Enter another grade: 95
Enter another grade: 96
Enter another grade: 97
Enter another grade: 98
Enter another grade: 99
The highest grade is: 99%
The average grade is: 94.5%
Press any key to continue . . .

Topic archived. No new replies allowed.