Problem overview
The last version of the Report Card computes statistics for a class. The starter code has a List of eight scores: 92, 78, 85, 67, 74, 88, 91 and 58. The program prints the highest, the lowest and the average, where the average is the sum divided by the number of scores with the fraction dropped:
Highest: 92
Lowest: 58
Average: 79
Approach
Each statistic has its own tool. The highest and lowest come straight from the collection's methods, scores.max and scores.min, with no loop. The average needs a total first, so the program adds the scores one by one in a loop, keeping a running sum in a var. Then it divides by scores.size.
The sum of these scores is 633, and 633 divided by 8 is 79.125 in exact arithmetic. Both operands are Int, so the division gives 79 and drops the .125, which is the rounded-down average the statement asks for.
Step by step
- Find the highest.
val highest = scores.max. - Find the lowest.
val lowest = scores.min, the mirror of the previous line. - Create the running total. A
var total = 0. It must be avar, because it changes as scores are added. - Loop over the scores.
for (s <- scores)gives one score at a time. Unlike the earlier versions, there is no index: the loop needs the values only. - Add each score.
total += sinside the loop, which is short fortotal = total + s. - Compute the average. After the loop,
val average = total / scores.size. - Print three lines, one each for the highest, the lowest and the average, in that order.
The solution in Scala
val scores = List(92, 78, 85, 67, 74, 88, 91, 58)
val highest = scores.max
val lowest = scores.min
var total = 0
for (s <- scores) {
total += s
}
val average = total / scores.size
println(s"Highest: $highest")
println(s"Lowest: $lowest")
println(s"Average: $average")
Details that matter in Scala
A List in Scala is immutable. scores.max and scores.min only read it, and the loop does too: only total changes. The loop can be replaced by scores.sum, which gives the same total of 633. The lesson uses the loop to practise the running-total pattern, which carries over to calculations that no built-in method covers.
scores.size is an Int, so total / scores.size is integer division. To keep the fraction, convert one side to Double: scores.sum.toDouble / 8 gives 79.125.
Edge cases and common mistakes
- An empty list.
List().maxthrowsUnsupportedOperationExceptionwith the messageempty.max. The average has a related problem: dividing the total 0 by a size of 0 throwsArithmeticExceptionwith the message/ by zero. The lesson's list always has scores, but a program that might get none should checkscores.isEmptyfirst, or usemaxOption, which returnsNonefor an empty list. - A single score. For
List(70)the highest, the lowest and the average are all 70. - Negative values. For
List(-5, 5, 0)the program prints a highest of 5, a lowest of -5 and an average of 0. - Starting
totalin the wrong place. Ifvar total = 0were inside the loop, it would reset on every pass. - Dividing inside the loop. The division belongs after the loop, once the total is complete.
- Rounding. Scores of 100 and 99 average 99.5 exactly, and the integer division prints 99.
Key takeaways
maxandminon a collection replace a search loop.- A running total is a
varthat starts at 0 and grows inside a loop. - Integer division drops the fraction, and dividing by the size of an empty list fails.
