Start learning free

LESSON 179 OF 404 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 - Class Statistics in Go

Practice

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

You start with:

scores := []int{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 (fmt.Println("Highest: " + strconv.Itoa(highest))).

Iterate over the slice once, tracking max and min manually, and accumulate the sum.

scores := []int{92, 78, 85, 67, 74, 88, 91, 58}

highest := scores[0]
lowest := scores[0]
total := 0
for _, s := range scores {
    if s > highest {
        highest = s
    }
    if s < lowest {
        lowest = s
    }
    total += s
}
average := total / len(scores)

fmt.Println("Highest: " + strconv.Itoa(highest))
fmt.Println("Lowest: " + strconv.Itoa(lowest))
fmt.Println("Average: " + strconv.Itoa(average))

STEP 1 OF 11
    Ctrl + Enter to check

    Report Card V6 in Go

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free