Start learning free

LESSON 202 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in Go: Weighted Score with Integer Division

Practice Read the full explanation

Combine variables and arithmetic to compute a student's weighted final score.

Weights: Homework 20%, Midterm 30%, Final 50%. Each weighted part is rounded down to a whole number before the parts are added.

You start with:

homework := 90
midterm := 82
finalExam := 88

score := homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100
fmt.Println("Score: " + strconv.Itoa(score))

Build:

Score: 86

Rules:

  • Compute score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100.
  • Each part is rounded down because int divided by int drops the remainder (the third variable is named finalExam).
  • For the starting data: 18 + 24 + 44 = 86.
  • Print "Score: " followed by the score; use strconv.Itoa(...) to join an integer to a string.
STEP 1 OF 2
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in Go, explained

    Problem overview

    Version 1 averaged three tests with equal weight. Version 2 computes a weighted final score, where some marks count for more than others. The weights are homework 20%, midterm 30% and final exam 50%. The starting code holds the three marks in variables created with :=:

    homework := 90
    midterm := 82
    finalExam := 88
    

    Each weighted part is rounded down to a whole number before the parts are added. For the starting data the parts are 18, 24 and 44, so the program prints:

    Score: 86
    

    Approach: weight each part, then add

    A percentage of a mark is the mark times the weight, divided by 100. So the score is homework * 20 / 100 plus midterm * 30 / 100 plus finalExam * 50 / 100. The weights 20, 30 and 50 add up to 100, so the three parts together make up the whole score.

    Because every operand is an int, each division drops its remainder, and the lesson wants exactly that: each part is truncated on its own. The starting data gives these parts:

    PartCalculationResult
    Homework90 * 20 / 10018
    Midterm82 * 30 / 10024
    Final exam88 * 50 / 10044

    The midterm part is really 24.6, and it becomes 24. The sum 18 + 24 + 44 is 86.

    Step by step

    1. Create score with :=. The right-hand side has three terms. Each term is a mark times its weight divided by 100. Go evaluates * and / from left to right, so homework * 20 / 100 multiplies first and then divides, which is the order you want.
    2. Print the line. fmt.Println prints the text Score: joined to the score. A number can't be joined to a string with + in Go, so the score goes through strconv.Itoa first.

    The solution in Go

    homework := 90
    midterm := 82
    finalExam := 88
    
    score := homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100
    fmt.Println("Score: " + strconv.Itoa(score))
    

    The code uses the fmt and strconv packages. A few details are worth knowing.

    := infers the type

    homework := 90 declares a variable and lets Go pick its type from the value. A whole-number literal gives an int. The same goes for score: the type of the expression on the right is int, so score is an int and strconv.Itoa accepts it. The short form works only inside a function.

    Rounding each part is not the same as rounding the total

    The lesson's rule is to truncate every part, then add. Computing the total in one division gives a different answer for some marks. With every mark equal to 99:

    MethodCalculationResult
    Each part first19 + 29 + 4997
    One division at the end(99 * 20 + 99 * 30 + 99 * 50) / 10099

    With every mark equal to 55, the parts are 11, 16 and 27, which add up to 54, while the single division gives 55. The form in the solution loses a little with each part. The lesson's tests expect that form, so don't merge the three terms.

    Multiply before you divide

    Integer division throws away the fraction, so the order matters. Writing the weight as 20 / 100 gives 0, because 20 divided by 100 is below 1 and the remainder is dropped. A version like homework * (20 / 100) therefore always contributes 0. Multiply the mark by the percentage first and divide by 100 last. Floating-point numbers would avoid this problem, but then you would have to round yourself. Integers keep the lesson simple.

    Edge cases and common mistakes

    • Parenthesised weights. homework * (20 / 100) is 0 for every mark.
    • Weights that don't add up. If you mistype a weight, say 20, 30 and 40, a perfect set of marks no longer gives 100. The three weights must sum to 100.
    • Naming. The third variable is called finalExam, and the code must use exactly that name.
    • Using the wrong mark with a weight. It is easy to pair 30 with homework. Keep the pairs in order: homework 20, midterm 30, final exam 50.
    • Forgetting strconv.Itoa. "Score: " + score doesn't compile, because a string and an int can't be added.

    Testing

    • Marks 90, 82, 88 print Score: 86.
    • Marks 100, 100, 100 give 100.
    • Marks 0, 0, 0 give 0.
    • Marks 55, 55, 55 give 54, and marks 99, 99, 99 give 97, because each part is cut before the parts are added.
    • Marks 51, 51, 51 give 50.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free