Finding highest and lowest Score

Hi guys. I have a basic grade program. I need to find the highest and lowest value but dont know how. Im still a beginner.
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
50
51
52
#include <iostream>

using namespace std;

int main()
{
    int studentcount, students, score;
    float totalscores, average;
    string studentname;
    
    cout<<"How many students have taken the exam"<<endl;
    cin>> students;
    
    totalscores=0.0;
    studentcount=0;
    
    while(students>studentcount)
    {
        cout<<"what is the student name: "<<endl;
        cin>>studentname;
        cout<<"Please enter scores: ";
        cin>> score;
        cout<<studentname<< " got a score of "<<score<<endl;
        
        if(score<100 && score>89)
        {
            cout<<studentname<<"'s letter grade is an A."<<endl;;
        }
        else if(score<90 && score>79)
        {
            cout<<studentname<<"'s letter grade is a B."<<endl;
        }
        else if(score<80 && score>69)
        {
            cout<<studentname<< "'s letter grade is a C."<<endl;
        }
        else
        {
            cout<<studentname<<" failed."<<endl;
        }
        
        studentcount=studentcount+1;
        totalscores=totalscores+score;
    }
    
    average=totalscores/students;
    
    cout<<"The class average was :"<<average<<endl;

    system("pause");
    return 0;
}
When you read a new score, it can be higher than highest score so far. How would you test that, and what should you do if it is true?

What should the highest score be before the first score is read?


Same logic with the lowest.
set the first input of score to lowest and highest.
lowest = highest = score ;
Then compare with the rest
closed account (SECMoG1T)
Add two variables of int leastscore and highscore;
where you intialized the others intialize thix two as follows
highscore=0;
least score=100; /// if no student can get above 100 or below 0

we intialixed highscore=0 so that we can account for any number greater than 0
, here we will add a statement that checks and see if score is greater than 0 it sets highscore equal to score else highscore remains equal to highscore;

likewise happens to the variable leastscore if score is less than least score leastscore is equated to score;

while(students>studentcount)
{
cout<<"what is the student name: "<<endl;
cin>>studentname;
cout<<"Please enter scores: ";
cin>> score;
cout<<studentname<< " got a score of "<<score<<endl;

if(score>=0&&score<=100)
{
if (score<leastscore)
{
leastscore=score;
}

else if(score>highscore)
{
highscore=score;
}

else if(score<100 && score>89)
{
cout<<studentname<<"'s letter grade is an A."<<endl;;
}
else if(score<90 && score>79)
{
cout<<studentname<<"'s letter grade is a B."<<endl;
}
else if(score<80 && score>69)
{
cout<<studentname<< "'s letter grade is a C."<<endl;
}
else
{
cout<<studentname<<" failed."<<endl;
}


}

else
{
cout<<"score can't be more than 100 or less than 0 , repeate"<<endl;
studentcount=studentcount-1; ///this line will compensate for the invalid score entered
score=0; /// to make sure that all the student correct value were
/// keyed in and score dont make the total seem greater
/// than the correct values;
}


studentcount=studentcount+1;
totalscores=totalscores+score;
}

average=totalscores/students;

cout<<"The class average was :"<<average<<endl;

cout<<"The class highest score is "<<highscore<<endl;

cout<<"The class least score is " <<leastscore<<endl;
}
The program runs good now just one glitch. I will always get a high score of 0. The low score is working good.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>

using namespace std;

int main()
{
    int studentcount, students, score;
    float totalscores, average, leastscore=100, highscore=0;
    string studentname;
    
    cout<<"How many students have taken the exam"<<endl;
    cin>> students;
    
    totalscores=0.0;
    studentcount=0;
    
    while(students>studentcount)
    {
        cout<<"what is the student name: "<<endl;
        cin>>studentname;
        cout<<"Please enter scores: ";
        cin>> score;
        cout<<studentname<< " got a score of "<<score<<endl;
        
        if(score>=0&&score<=100)
        {
        if (score<leastscore)
            {
                leastscore=score;
            }
            
        else if(score>highscore)
            {
                highscore=score;
            }
        }
        
        if(score<100 && score>89)
            {
                cout<<studentname<<"'s letter grade is an A."<<endl;;
            }
        else if(score<90 && score>79)
            {
                cout<<studentname<<"'s letter grade is a B."<<endl;
            }
        else if(score<80 && score>69)
            {
                cout<<studentname<< "'s letter grade is a C."<<endl;
            }
        else
            {
                cout<<studentname<<" failed."<<endl;
            }
        
        studentcount=studentcount+1;
        totalscores=totalscores+score;
    }
    
    average=totalscores/students;
    
    cout<<"The class average was :"<<average<<endl;
    cout<<"The highscore was :"<<highscore<<endl;
    cout<<"The low score was: "<<leastscore<<endl;
    
    system("pause");
    return 0;
}
Program works fine for me.
Maybe I can have some testcases that generates wrong output.
Last edited on
3..2..1.. not fine.

The else on line 32 is an error. It is quite possible that both high and low have to be adjusted by the same score. The first given score, if it is neither 0 or 100, should do so.

Why are only the high and low confined within the line 25 test? Surely, the letter grading should also enjoy about the knowledge that score is within [0..100]. More importantly, what does happen to the average in a class of 10 students, if John Doe has a "failed" score of 9000? andy1992 did try to do it differently, but not neatly and you did not use that.

You could do:
1
2
3
4
5
6
7
8
9
if ( score < 0 || 100 < score )
{
  cout << "Invalid score. Discarded.\n";
}
else
{
 // test min, max, letter grade
 // add count and total
}

However, this is within a loop and therefore, we have one more keyword:
1
2
3
4
5
6
7
8
if ( score < 0 || 100 < score )
{
  cout << "Invalid score. Discarded.\n";
  continue;
}

// test min, max, letter grade
// add count and total 


PS. What are the valid scores? In your score the grade for 100 is "failed"? Is that correct?

Topic archived. No new replies allowed.