Start learning free

LESSON 206 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 in Scala: Highest, Lowest and Average of Scores

Practice Read the full explanation

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

val scores = List(92, 78, 85, 67, 74, 88, 91, 58)

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum of all scores divided by how many there are, rounded down (integer division).
  • Print "Highest: ", "Lowest: " and "Average: " on three lines, in the order of the sample output (println(s"Highest: $highest")).

Use scores.max / scores.min on a List[Int]; sum with a loop.

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")

STEP 1 OF 9
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V6 in Scala, explained

    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

    1. Find the highest. val highest = scores.max.
    2. Find the lowest. val lowest = scores.min, the mirror of the previous line.
    3. Create the running total. A var total = 0. It must be a var, because it changes as scores are added.
    4. 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.
    5. Add each score. total += s inside the loop, which is short for total = total + s.
    6. Compute the average. After the loop, val average = total / scores.size.
    7. 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().max throws UnsupportedOperationException with the message empty.max. The average has a related problem: dividing the total 0 by a size of 0 throws ArithmeticException with the message / by zero. The lesson's list always has scores, but a program that might get none should check scores.isEmpty first, or use maxOption, which returns None for 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 total in the wrong place. If var total = 0 were 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

    • max and min on a collection replace a search loop.
    • A running total is a var that starts at 0 and grows inside a loop.
    • Integer division drops the fraction, and dividing by the size of an empty list fails.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free