What is the logic in finding the highest and lowest of n?

Okay so I have been learning C++(obviously beginner) for a few monthsand my Prof. gave me a problem definition which states:

Print a the selected amount of quiz scores, with the average,highest and lowest number.
So i got it to work with the average but couldn't get the highest and lowest right so now I scoured the net on how it works, problem is what I got was the code itself and not an explanation on its logic so yeah, Need some advice on that

(This is with Codeblocks incase you were wondering)
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
48
49
  int main()
{
    float q=0,x=1,z,counter=0,sum=0,ave=0,small=100,big=0;
    char ans='Y';
    while(ans=='Y'||ans=='y')
    {
        system("cls");
        cout << "Quiz Scores Average Computation\n\n";
        cout << "Enter how many quiz scores you have (2-10 only)\n";
        cin>>z;
        while(z<2||z>10)
        {
            cout<<"Invalid input (2-10 only)\n";
            cin>>z;
        }
        for(x;x<=z;x++)
        {
            if(q>big)
            {
                big=q;
            }
            if(q<small)
            {
                small=q;
            }
            cout<<"Enter Quiz "<<x<<" : ";
            cin>>q;
            while(q<0||(q>100))
            {
                cout<<"Invalid Quiz Score(0-100 only)\n";
                cout <<"Enter Quiz "<<x<< " : ";
                cin>>q;
            }
            counter++;
            sum=sum+q;
        }
        ave = sum/counter;
        cout<<"\nYou have entered "<<counter<<" valid quiz scores\n";
        cout<<"The average quiz score is "<<ave;
        cout<<"\nThe highest score is "<<big;
        cout<<"\n\nRun the program again?";
        cin>>ans;
        x=1,q=0,counter=0,sum=0,ave=0;
    }
    cout<<"\n\nGood Bye!";


    return 0;
}
Your code is good but you need to first enter q and then to check if it's max or min, i. e. you need to put

1
2
3
4
5
6
7
8
if(q>big)
{
    big=q;
}
if(q<small)
{
    small=q;
}

after:

1
2
3
4
5
6
7
cin>>q;
while(q<0||(q>100))
{
    cout<<"Invalid Quiz Score(0-100 only)\n";
    cout <<"Enter Quiz "<<x<< " : ";
    cin>>q;
}

Btw I get a following warning:

18|warning: statement has no effect [-Wunused-value]|

when compiling with CodeBlocks because of this line:

for(x;x<=z;x++)

because there is no need to put for (x;...) if you don't want to initialize variable there. You can put for (; ...) or for (x = 1; ...) to remove that warning if you get it too.

Can't you just say avg = sum / z, I mean, there is no need for counter variable?
Last edited on
Topic archived. No new replies allowed.