average

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
#include <iostream>

using namespace std;

int n;
int m[25];
int i;
int average;
int main()
{
    
    
    cout << " Introduce one value between 0 and 20: \n";
    cin >> n;
    
    
    {
    for (i=0;i<n;i++)
    {
        cout << " Introduce grades of students between 0 and 20:";
        cin >> m[i];
    }
    }

    average = (m[i]/n);
    cout << " The average of students is : " << average << endl; 
    
    system("PAUSE");
    return EXIT_SUCCESS;
} 


why my average didn't go to output ???
To output the average you have to get the sum of all array elements.:)
sum = (m[i]++);

it ?
-- declare total
-- add each element value to total
-- divide total by number of elements in array
Last edited on
@dioing
sum = (m[i]++);

it ?


No.
You have now two attempts.:)
iHutch105, i now what to do , but don't how !
i'm very begginer :/
vlad from moscow , i'm in such exercises a few hours ...
1
2
3
4
5
6
7
8
    int sum = 0;  
    for ( i = 0; i < n; i++ )
    {
        sum += m[i];
    }

    average = sum / n;
    cout << " The average of students is : " << average << endl; 
Last edited on
you declared i inside the for loop. After that loop is done i is no longer in scope, so calling m[i] is invalid. Even if it was valid, it would only assign the value of ONE element divided by n to average. For this to work with an array you'd have to add each element in the array, then divide by the number of elements and assign that value to average. Something like this:

1
2
3
4
5
6
int sum = 0;

for (int i = 0; i < n; i++)
    sum += m[i];

average = sum/n;
one big thank you ! I'm realy bad in c++ !
Topic archived. No new replies allowed.