Simplest way to find Max/Min from list?

I'm fairly new and looking for some suggestions for a lab I have to complete for class. The program asks for 5 test scores all entered at once, finds the maximum and minimum score for each student, and then accumulates those 2 scores in totalHighScores and totalLowScores. The program is designed to find the average high score and average low score for x amount of students (program is terminated when a negative ID is entered). I am used to Python, and I know that Python has a built-in max/min function which made things fairly simple. I've looked around at the forum for suggestions and most of the methods used we have not even covered yet. I'm just curious what the simplest and easiest method could be to identify the max and min number after the 5 scores for each student are entered. I have posted my code below without the max/min section (obviously) thanks for any help !!
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
 /*--------------------------------------------------------------*/
/*                                                              */
/* includes                                                     */
/*                                                              */
/*--------------------------------------------------------------*/

#include <iostream>
#include <iomanip>
using namespace std;

/*--------------------------------------------------------------*/
/*                                                              */
/* main                                                         */
/*                                                              */
/*--------------------------------------------------------------*/

main ()

{

/*--------------------------------------------------------------*/
/*                                                              */
/* Define memory locations                                      */
/*                                                              */
/*--------------------------------------------------------------*/

int studentId,
     
    labOne,
    labTwo,
    labThree,
    labFour
    labFive,

    highScore,
    lowScore;

int studentCount    = 0;
int totalHighScores = 0;
int totalLowScores  = 0;

float averageHigh
      averageLow;

/*--------------------------------------------------------------*/
/*                                                              */
/* Set the float formatting                                     */
/*                                                              */
/*--------------------------------------------------------------*/

cout << setiosflags (ios :: fixed)
     << setprecision (1);

/*--------------------------------------------------------------*/
/*                                                              */
/* Get the Student ID                                           */
/*                                                              */
/*--------------------------------------------------------------*/

cout << "Please enter the student's ID "
     << endl;

cin >> studentID;

/*--------------------------------------------------------------*/
/*                                                              */
/* Get the loop                                                 */
/*                                                              */
/*--------------------------------------------------------------*/

while (studentID > 0)

    {

    /*--------------------------------------------------------------*/
    /*                                                              */
    /* Gather Input                                                 */
    /*                                                              */
    /*--------------------------------------------------------------*/

     cout << "Please enter the five lab scores "
          << endl;

     cin >> labOne
         >> labTwo
         >> labThree
         >> labFour
         >> labFive;

     /*--------------------------------------------------------------*/
     /*                                                              */
     /* Find the high/low Score                                      */
     /*                                                              */
     /*--------------------------------------------------------------*/











     /*--------------------------------------------------------------*/
     /*                                                              */
     /* Find the Averages                                            */
     /*                                                              */
     /*--------------------------------------------------------------*/
      
      averageHigh = float (totalHighScores) / float (studentCount);

      averageLow  = float (totalLowScores)  / floar (studentCount);
     
     /*--------------------------------------------------------------*/
     /*                                                              */
     /* Display output                                               */
     /*                                                              */
     /*--------------------------------------------------------------*/

     cout << "Lab 4 Averages"
          << endl
          << endl

          << "The average best lab score is "
          << averageHigh

          << endl
          << endl

          << The average low lab score is "
          << averageLow
       
          << endl
          << endl;
     
     /*--------------------------------------------------------------*/
     /*                                                              */
     /* Get another Student ID                                       */
     /*                                                              */
     /*--------------------------------------------------------------*/

     cout << "Please enter another student's ID"
          << endl;

     cin >> studentID;
 
     }

/*--------------------------------------------------------------*/
/*                                                              */
/* End                                                          */
/*                                                              */
/*--------------------------------------------------------------*/

} 
See std::max_element
What about for the minimum? Most of the examples I were able to find shows methods on how to do it when numbers are entered individually (ex. Setting the max at 0 so every time a new number is entered that is greater than the previous max becomes the new max) but I'm a little confused on how to find max/min from a list of inputted numbers all at once
Minimum is no different from maximum; the condition and premise are simply mirrors. std::min_element

Initializing "max" to 0 works only in the case when every input greater or equal to 0. If you do validate input to prevent negative values, then the 0 is a good initial value. If you cannot do so (negative values are valid), then std::numeric_limits<float>::min() is the logical initial value for "max".

The other approach is to initialize the "max" with the first input, but then you have to test each input for whether it is the first input.


Note: your code seems to calculate the average within the input loop. You should delay that to after the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>    
#include <list> 

int main () {
	
	std::list<int> l{1,2,3,0,-2,9};
	std::list<int>::iterator it, min, max;
	it = l.begin();
	min= l.begin();
	max= l.begin();

	while(it != l.end()){
		if(*it>*max) max=it;
		if(*it<*min) min=it;
		it++;
	}
	
	std::cout << *min << " " << *max;

}
Topic archived. No new replies allowed.