Start learning free

LESSON 205 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in Rust: Weighted Score

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:

// Rust: the editor starts empty - declare the data yourself, for example:
let homework = 90;
let midterm = 82;
let finalExam = 88;

Build:

Score: 86

Rules:

  • Compute score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100.
  • Each part is rounded down because i32 divided by i32 drops the remainder (the third variable is named finalExam).
  • For the starting data: 18 + 24 + 44 = 86.
  • Print "Score: " followed by the score, for example println!("Score: {}", score);

The third variable is named finalExam. i32 / i32 truncates, so each weight already rounds down.

let homework = 90;
let midterm = 82;
let finalExam = 88;
let score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100;
println!("Score: {}", score);

STEP 1 OF 5
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in Rust, explained

    Problem overview

    The final score of a course is not a plain average: homework counts for 20%, the midterm for 30% and the final exam for 50%. The program takes the marks 90, 82 and 88 and prints Score: 86.

    Each weighted part is rounded down to a whole number before the parts are added. Homework gives 90 * 20 / 100 = 18, the midterm gives 82 * 30 / 100 = 24 (24.6 rounded down), and the final exam gives 88 * 50 / 100 = 44. The parts add up to 86.

    The previous version averaged three tests with integer division. This one keeps the same whole-number arithmetic and adds weights.

    Approach: mark times weight, divided by 100

    A percentage is a number out of 100, so a part is the mark multiplied by its weight and divided by 100. The weights 20, 30 and 50 add up to 100, so the three parts together make up the whole score.

    The order of the operations matters. Multiplying first and dividing second keeps as much precision as integers allow: 90 * 20 / 100 evaluates left to right as 1800 / 100. Dividing 20 / 100 first would give 0, because integer division drops the fraction, and every part would become 0.

    Each division happens separately, which is what the lesson asks for. Because every part is truncated on its own, the total can be lower than if you added everything and divided once. With marks 91, 81 and 89 the three rounded parts are 18, 24 and 44, which add up to 86, while (91 * 20 + 81 * 30 + 89 * 50) / 100 gives 87.

    Step by step

    1. Declare homework with let, set to 90.
    2. Declare midterm the same way, set to 82.
    3. Declare finalExam the same way, set to 88. The name is camel case, as the lesson specifies.
    4. Compute score as the sum of three parts. Each part is its mark times its weight, divided by 100.
    5. Print the result with println! and a {} placeholder.

    The solution in Rust

    let homework = 90;
    let midterm = 82;
    let finalExam = 88;
    let score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100;
    println!("Score: {}", score);
    

    Rust details worth knowing:

    • No annotations needed. The literals 90, 82 and 88 have no type written next to them, so Rust picks i32, the default for integer literals. All the arithmetic is therefore integer arithmetic.
    • Operator precedence. * and / bind tighter than +, and * and / are evaluated left to right. That is why homework * 20 / 100 needs no parentheses.
    • Naming style. Rust convention is snake_case for variables, so the compiler prints a warning that finalExam should be named final_exam. It is only a warning: the program still compiles and prints Score: 86. This lesson uses the name finalExam on purpose.

    Edge cases and common mistakes

    • Dividing the weight first. 90 * (20 / 100) is 0, because 20 / 100 is 0 in integer arithmetic.
    • Rounding after adding. One division at the end can give a higher result than three separate ones, as in the 86 against 87 example above. The lesson's rule is to round each part.
    • Small marks. With three marks of 9, the parts are 1, 2 and 4, and the score is 7, not 9. Each part loses its fraction.
    • Perfect and near-perfect marks. Marks of 100, 100 and 100 give 100. Marks of 99, 99 and 99 give 97, because each part is cut down.
    • Wrong variable name. The third variable is finalExam. Rust names are case-sensitive, so finalexam would be a different, undefined name and the code would not compile.

    Testing

    • Marks 90, 82 and 88 print Score: 86.
    • Marks 91, 81 and 89 give 86.
    • Marks 100, 100 and 100 give 100.
    • Marks 99, 99 and 99 give 97.
    • Marks 9, 9 and 9 give 7.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free