Problem overview
Version 6 summarises a class from a list of scores. The starter code holds vector<int> scores = {92, 78, 85, 67, 74, 88, 91, 58};, and the program prints three lines: the highest score, the lowest score and the average, rounded down.
Highest: 92
Lowest: 58
Average: 79
The scores add up to 633, and 633 divided by 8 is 79.125. Integer division drops the .125, which gives 79.
Approach: two library calls and one running total
The highest and lowest scores come from the algorithms max_element and min_element. Each takes a range, here the whole vector from scores.begin() to scores.end(), and returns an iterator that points at the matching element: the biggest or the smallest. An iterator is not the number itself, so you put * in front to read the value it points to.
The average needs the sum of all scores, and for that you keep a running total: start a variable at 0, and add each score to it in a loop. Then divide by the number of scores.
All the values are int, so the division is an integer division, which is what the lesson asks for.
Step by step
- Find the highest score:
int highest = *max_element(scores.begin(), scores.end());. The*turns the result into the value. - Find the lowest score the same way with
min_element, inlowest. - Create
totaland set it to 0. It has to start at zero, before any score is counted. - Add every score with a loop:
for (int s : scores) total += s;. The variablestakes each score in turn. With one statement in the body, the loop fits on one line. - Compute
averageastotaldivided by(int) scores.size(). The cast turns the unsigned size into anint, so both operands are signed. - Print the three lines in order:
Highest:,Lowest:andAverage:, each with its value andendl.
The solution in C++
vector<int> scores = {92, 78, 85, 67, 74, 88, 91, 58};
int highest = *max_element(scores.begin(), scores.end());
int lowest = *min_element(scores.begin(), scores.end());
int total = 0;
for (int s : scores) total += s;
int average = total / (int) scores.size();
cout << "Highest: " << highest << endl;
cout << "Lowest: " << lowest << endl;
cout << "Average: " << average << endl;
The three results come from three separate passes over the vector: one for the maximum, one for the minimum and one for the sum. That is simple and clear. One loop could find all three at once, but with eight scores the clarity is worth more.
Details that matter
The cast on scores.size() matters for the type of the division. Without it you would divide a signed int by an unsigned size_t, and the signed value would be converted to unsigned first. For the positive total here the answer is the same, but for a negative total it would be wrong, so the cast is a good habit.
Integer division also decides the output. The exact average is 79.125, and the program prints 79. If you divide by a double instead, as in (double) total / scores.size(), the result is 79.125, but that is not what the lesson asks for.
The algorithms max_element and min_element assume the range has at least one element. For an empty vector they return end(), which doesn't point at a value, and dereferencing it is undefined behaviour. Dividing the total by zero, which is the size of an empty vector, is also undefined. The lesson's data is never empty. A vector with one score, {50}, gives 50 for both the highest and the lowest.
Edge cases and common mistakes
- Forgetting the
*.int highest = max_element(...)doesn't compile, because an iterator can't be converted to anint. - Starting
totalwith a score or leaving it uninitialised. An uninitialisedintholds an unpredictable value, so the average would be wrong. Start it at 0. - Dividing by a fixed 8. Use the size of the vector, so the program keeps working when the list changes.
- Expecting rounding. 633 / 8 is 79, not 79.125, and never 80.
- Wrong print order. The lines must read Highest, Lowest, Average.
Testing
With the starter scores the output is the three lines at the top. With a single score of 50 the highest and lowest are both 50, and the average is 50 as well.
