Using Arrays Function Having problem completing it.

closed account (Nvq4jE8b)
#include <iostream.h>

int main()
{
int grade[10],sum=0,average=0,max=0;
int min=0;

for (int i=0;i<10;i++)
{
cout<<"Enter grade"<<(i+1)<<" ";
cin>>grade[i];
sum+=grade[i];
average = sum / 10;
if(grade[i] > max)
{
max=grade[i];
}
if(grade[i] < min)
{
min=grade[i];
}
}
cout<<"\nThe average is:"<<average;
cout<<"\nThe Maximum is:"<<max;
cout<<"\nThe Minimum is:"<<min;


return 0;
}

the Minimum is giving wrong output......
Please don't double post.
try setting min to 100000 or something. on line 5
Last edited on
closed account (Nvq4jE8b)
sorry for the double post its and emergency :D
already been answered, and there's no such thing as an emergency in coding unless some thugg is holding a gun to your wife's head in which case you should call the police not come here. :)
Last edited on
what is that garbage nikki17 ? don't post crap with the intention to help which actually leads to more confusion and more of a hinderence than helping. I understand that was not your intention but that is what your doing, it's the opposite.
however 1 change you made to that could be used instead of my suggestion, however it's going to give you a compilation error. So the theory was almost right: int min=grade[0]; however grade[0] has not been initialized so this cannot be possible. The rest of your changes are just as useless or pointless.


to the OP, rather than using my suggestion of 100000 what you could do is initialise it to INT_MAX or INT_MAX-1 or something. It will look cleaner and less like a dodgy work around.

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
#include <iostream>
#include <climits>  // for use of INT_MAX
using std::cin;
using std::cout;
using std::endl;

int main()
{
    int grade[10],sum=0,average=0,max=0;
    int min=INT_MAX;

    for (int i=0;i<10;i++)
    {
        cout << "Enter grade" << (i+1) << " ";
        cin >> grade[i];
        sum+=grade[i];
        average = sum / 10;
        if (grade[i] > max)
        {
            max=grade[i];
        }
        if (grade[i] < min)
        {
            min=grade[i];
        }
    }
    cout << "\nThe average is:" << average;
    cout << "\nThe Maximum is:" << max;
    cout << "\nThe Minimum is:" << min << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.