Start learning free

LESSON 201 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Report Card V1 in Go: Integer Average with strconv

Practice Read the full explanation

Compute a student's average score across three tests using integer division so the result is a whole number.

You start with:

var test1 int = 85
var test2 int = 92
var test3 int = 78

Build:

Average: 85

Rules:

  • Compute the integer average of test1, test2, and test3 using integer division (int divided by int already drops the remainder).
  • Print "Average: " followed by the average; use strconv.Itoa(...) to join an integer to a string.

Go's int / int truncates, so (test1 + test2 + test3) / 3 already gives an integer.

var test1 int = 85
var test2 int = 92
var test3 int = 78

var average int = (test1 + test2 + test3) / 3
fmt.Println("Average: " + strconv.Itoa(average))

STEP 1 OF 2
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Report Card V1 in Go, explained

    Problem overview

    Report Card is a small project that grows over six versions. Version 1 does the simplest useful thing: it averages one student's three test scores and prints the result. The starting code declares the scores as int variables:

    var test1 int = 85
    var test2 int = 92
    var test3 int = 78
    

    The program must add the three scores, divide by 3 and print the average as a whole number. For these scores the sum is 255, and 255 divided by 3 is 85, so the output is:

    Average: 85
    

    Approach: sum first, then divide

    An average is the sum divided by the count, so the program needs two operations in the right order: add, then divide. Because every value is an int, the division is integer division. Go throws away the fractional part and keeps the whole number, and it truncates toward zero. With scores 80, 81 and 81 the sum is 242, and 242 divided by 3 is 80.67 in ordinary arithmetic, but the program gets 80. For the non-negative scores of a report card, this is the same as rounding down.

    The second job is printing. The result has to be joined to the text Average: , and Go is strict about that. The next sections show the details.

    Step by step

    1. Declare average as an int with var. Its value is the expression (test1 + test2 + test3) / 3. The parentheses make the addition happen before the division.
    2. Print the line. fmt.Println prints its argument and a line break. The text "Average: " (with the space inside the quotes) is joined to the number after the number has been converted to a string with strconv.Itoa.

    The solution in Go

    var test1 int = 85
    var test2 int = 92
    var test3 int = 78
    
    var average int = (test1 + test2 + test3) / 3
    fmt.Println("Average: " + strconv.Itoa(average))
    

    The code uses the fmt and strconv packages. Here are the details worth a closer look.

    Parentheses decide the answer

    Division binds tighter than addition, as in school arithmetic. Without the parentheses, test1 + test2 + test3 / 3 divides only test3: it computes 85 + 92 + 26 and prints 203, which is not an average. The parentheses are not optional here.

    Integer division truncates

    When both operands of / are int, the result is an int and the remainder is dropped. Three more cases with the same expression:

    ScoresSumAverage printed
    80, 81, 8124280
    100, 99, 9929899
    0, 0, 220

    The result is never rounded to the nearest whole number: 99.33 becomes 99 and 80.67 becomes 80. If you wanted the exact average, you would convert to float64 first, which is a different task from this lesson's.

    Why strconv.Itoa

    Go does not convert numbers to text on its own, so the + operator can't join a string and an int. This code does not compile:

    var average int = 85
    fmt.Println("Average: " + average)
    

    The compiler reports invalid operation: "Average: " + average (mismatched types untyped string and int). strconv.Itoa(average) ("integer to ASCII") returns the decimal digits of the number as a string, so both sides of the + are strings.

    Don't reach for string(average) instead. Converting an integer to string in Go treats the number as a character code, not as digits. With average equal to 85, the conversion gives the character with code 85, which is U. The code compiles, but the output is wrong. (Go's vet tool warns about this conversion.)

    Variables must be used

    Go refuses to compile a program that declares a local variable and never uses it. In this lesson all four variables are read, so that is fine, but if you declared average and forgot the print line, you would get a declared and not used error instead of a silent mistake.

    Edge cases and common mistakes

    • Dividing before adding. Missing parentheses give 203 for the starting scores.
    • Dividing by the wrong number. There are three scores, so the divisor is 3.
    • Expecting a decimal. The lesson asks for a whole number, and int / int gives one: the output is Average: 85, not 85.0.
    • Using string(average). It prints the character U, not the digits.
    • Forgetting the space. The expected output is Average: with a space before the number. "Average:" without it prints Average:85.
    • Leaving out the print. The lesson's tests read what the program prints, so a correct average variable alone is not enough.

    Testing

    • Scores 85, 92, 78 print Average: 85.
    • Scores 80, 81, 81 give 80.
    • Scores 100, 99, 99 give 99.
    • Scores 0, 0, 2 give 0.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free