Need your help

I want to write a program that is going to read 3 marks of n students and then it will find out how many students have passed.So I wrote the code but it's not giving the result I know..Can you help me fix 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
 #include <iostream>
using namespace std;
int main ()
{
    int n,g1,g2,g3,sum,greater_limit;
    float average;
    float limit=59.5;
    greater_limit=0;
    sum=0;
    average=0;
    cin >> n;
    for (int i=1;i<=n;i++)
    {
        cin >> g1 >> g2 >> g3;
        sum=g1+g2+g3;
        average=sum/3;
        if (average>limit)
        {
            greater_limit++;
        }
    }
    cout << " " << greater_limit;
    return 0;
}
Last edited on
You start with greater_limit value uninitialised.
I initiallized it,but again I don't get the desired result..I think the mistake is in the calculation of the average..maybe it's not making precise calculation (but takinkg it rounded) and then the problem is figured out in the final output..so I need help on how finding the precise average of the three marks..
Right, there is one more issue:
sum is int and 3 is int literal. So the result of sum / 3 is also int, meaning it is rounded down before being assigned to average, which is float.
Try changing line 16 into:
average = sum / 3.0; // .0 marking double literal
Last edited on
Topic archived. No new replies allowed.