lowest and highest number of a series

I'm having trouble, I want the inner loop to display the lowest and highest score. If I assign initial values for the high and low score, it won't always work (because if no/ all scores are above/ below my initial values...)
But without assigning initial values for highscore and lowscore, One of them will always be undefined during the first runthrough.

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
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace std;

const int AGRADE = 90;
const int BGRADE = 80;
const int CGRADE = 70;
const int DGRADE = 60;

int main()
{
   int numscores;
   int score;
   int count;
   int Acount, Bcount, Ccount, Dcount, Fcount;
   int sectioncount;
   int scoretotal;
   int highscore;
   int lowscore;
   int highest = 0;
   int lowest = -1;
   sectioncount = 1;
    
   cin >> numscores;
   while ( cin )
   {
      count = 1;
      Acount = 0;
      Bcount = 0;
      Ccount = 0;
      Dcount = 0;
      Fcount = 0;
      
      while ( numscores >= count )
      {
         cin >> score;
         if ( score >= AGRADE )   
            Acount++;
         else if ( score < AGRADE && score >= BGRADE )
            Bcount++;
         else if ( score < BGRADE && score >= CGRADE )
            Ccount++;
         else if ( score < CGRADE && score >= DGRADE )
            Dcount++;
         else
            Fcount++;
         
       
               
         if (score >= highest)         
            highscore = score;
         else if (score <= lowest)
            score = lowscore;
         
         
            
         
         
         
         count++;
      }
      cout << "Scores for section " << sectioncount << endl;
      cout << "A's: " << Acount << endl;
      cout << "B's: " << Bcount << endl;
      cout << "C's: " << Ccount << endl;
      cout << "D's: " << Dcount << endl;
      cout << "F's: " << Fcount << endl;
      cout << "Lowest Score: " << lowscore << endl;
      cout << "Highest Score: " << highscore << endl;
      highest = highscore;
      lowscore = lowest;
      cin >> numscores;
      sectioncount++;   
   }// outer while loop
   
   return 0;
}


how do i set this up so it stores a low and high score, and then compares those to each next number in the series?
Last edited on
You should set them to certain values during the runthrough. So, highest should be "-1" (ie, below the lowest accepted value) and "lowest" should be ABOVE the highest accepted value, ie maybe 101. This will make the first entered number the lowest

Also, for your lowest thing, inside the loop you have reversed the if (score <= lowest) part.
It should be
1
2
3
 lowscore = score;
// Instead of
score = lowscore;


You also need to get rid of the else in front of the else if statement for the lowscore test.

You don't need a "highscore" AND a "highest" variable: one will suffice.

I've gone through and made the suggested changes: find them below
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;

const int AGRADE = 90;
const int BGRADE = 80;
const int CGRADE = 70;
const int DGRADE = 60;

int main()
{
   int numscores;
   int score;
   int count;
   int Acount, Bcount, Ccount, Dcount, Fcount;
   int sectioncount;
   int scoretotal;
   int highscore = 0;
   int lowscore = 1000;
   sectioncount = 1;
    
   cin >> numscores;
   while ( cin )
   {
      count = 1;
      Acount = 0;
      Bcount = 0;
      Ccount = 0;
      Dcount = 0;
      Fcount = 0;
      highscore = -1;
      lowscore = 1000;
      
      while ( numscores >= count )
      {
         cin >> score;
         if ( score >= AGRADE )   
            Acount++;
         else if ( score < AGRADE && score >= BGRADE )
            Bcount++;
         else if ( score < BGRADE && score >= CGRADE )
            Ccount++;
         else if ( score < CGRADE && score >= DGRADE )
            Dcount++;
         else
            Fcount++;
               
         if (score >= highscore)         
            highscore = score;
         if (score <= lowscore)
            lowscore = score;

         count++;
      }
      cout << "Scores for section " << sectioncount << endl;
      cout << "A's: " << Acount << endl;
      cout << "B's: " << Bcount << endl;
      cout << "C's: " << Ccount << endl;
      cout << "D's: " << Dcount << endl;
      cout << "F's: " << Fcount << endl;
      cout << "Lowest Score: " << lowscore << endl;
      cout << "Highest Score: " << highscore << endl;
      cin >> numscores;
      sectioncount++;   
   }// outer while loop
   
   return 0;
}


As a suggestion, try having some output as the user prompt (ie "Please enter the next number" or "Enter the number of scores you will enter"
Last edited on
thanks, i fixed those things, but i am still getting an error that "lowscore hasn't been assigned a value" and it wont run.
gotcha, thanks. i had to think about that logic for awhile but it does make sense. i noticed you used two if statements in a row, i think that was also confusing me as i was trying to use if/else
The new code I've posted should have fixed that problem. The original issue was that you were never assigning a value to lowscore, as can be seen on line 52 of your posted code: you were trying to assign lowscore to score, rather than the other way around.
If you were using if/else, it may not have captured the possibility of that number being the lowest AND the highest number.

Consider the following score inputs: (with 5 scores being entered)

12
24
30
45
89

In the original code, it would have assigned 12 to be the highest number, then skipped the possibility of it being the lowest number as well. When 24 was entered, it would set that to be the highest number as well. Continuing this trend, the program would never recognise that there was a lowest number.

No problems, have fun :)

If it works as well, can you mark the question as "solved" so others know that it has been solved
Last edited on
Topic archived. No new replies allowed.