Help with an array syntax

So my question here is that this program is meant to ask for ten grades. Once you input those it should get the average of those and it should display them on the screen. It is necessary to create three functions, one for getting the grades the other one for calculating the average and the other one is for displaying the result.
So I managed to ask the user for 10 grades, but my problem is that I do not know if it is adding the grades and dividing them by 10. I cant get that to work.
Here is my code

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
  #include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


/**********************************************************************
 * The function Main is meant to be in every program. While it is not
 * necessary it tells your computer where to start. It is called
 * a program start up.
 ***********************************************************************/
void getGrades(int grades[])
{
   for (int i=0; i <= 9; i++)
   {
      cout << "Grade " << i + 1 << ": ";
      cin >> grades[i];
   }
}

/***********************************************
 * This function will calculate the average for
 * the ten grades that we input.
 **********************************************/

void averageGrades(int grades[])
{
   for (int i =0; i <= 9; i++)
      grades[i] += grades[i] / 10;
}

void display(int grades[])
{
   for (int i = 0; i <= 0; i++)
      cout << "Average Grade: " << grades[i]
        << "%" << endl;
}

int main()
{
   int grades[10];
   getGrades(grades);
   averageGrades(grades);
   display(grades);
   return 0;
}
Last edited on
basically your averaageGrades function has a minor flawed logic

its just taking the number and adding itself and dividing by 10 (please check your code for more clarity)

anyway here's what you may do

1
2
3
4
5
6
7
8
9
10
11
12
13
float averageGrades(int grades[])
{
float grade_avg=0;
   
for (int i =0; i <= 9; i++)
      {   
       grade_avg+=grades[i];
      }

grade_avg/10; //notice its outside the for loop

return grade_avg;
}


HOPE IT HELPS
Last edited on
Topic archived. No new replies allowed.