typestar

grade_report.odin in Odin

Quiz scores fold into averages and letter grades, then a formatted class report.

// Average quiz scores into letter grades and print the class report.
package main

import "core:fmt"

Student :: struct {
    name:    string,
    quizzes: [3]int,
}

roster := [4]Student{
    {"Imani", {92, 88, 95}},
    {"Theo", {78, 83, 80}},
    {"Sofia", {85, 91, 89}},
    {"Miles", {62, 70, 68}},
}

average :: proc(quizzes: [3]int) -> f32 {
    sum := 0
    for q in quizzes do sum += q
    return f32(sum) / 3.0
}

letter :: proc(avg: f32) -> string {
    switch {
    case avg >= 90.0: return "A"
    case avg >= 80.0: return "B"
    case avg >= 70.0: return "C"
    }
    return "retake"
}

main :: proc() {
    fmt.println("student    avg  grade")
    fmt.println("---------------------")

    class_sum: f32 = 0
    for s in roster {
        avg := average(s.quizzes)
        class_sum += avg
        fmt.printfln("%-8s %5.1f  %s", s.name, avg, letter(avg))
    }

    fmt.println("---------------------")
    fmt.printfln("class avg %.1f", class_sum / len(roster))
    fmt.println("quizzes graded:", len(roster) * 3)
}

How it works

  1. The roster is an array of structs built with positional literals.
  2. average sums a [3]int and f32(sum) makes the division honest.
  3. %-8s and %5.1f pad the columns; a bare switch maps grade bands.

Keywords and builtins used here

The run, in numbers

Lines
47
Characters to type
917
Tokens
271
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 250 seconds.

Type this snippet

Step 2 of 3 in Encore, step 26 of 27 in Language basics.

← Previous Next →