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
- Declare
homeworkwithlet, set to 90. - Declare
midtermthe same way, set to 82. - Declare
finalExamthe same way, set to 88. The name is camel case, as the lesson specifies. - Compute
scoreas the sum of three parts. Each part is its mark times its weight, divided by 100. - 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 whyhomework * 20 / 100needs no parentheses. - Naming style. Rust convention is
snake_casefor variables, so the compiler prints a warning thatfinalExamshould be namedfinal_exam. It is only a warning: the program still compiles and printsScore: 86. This lesson uses the namefinalExamon purpose.
Edge cases and common mistakes
- Dividing the weight first.
90 * (20 / 100)is 0, because20 / 100is 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, sofinalexamwould 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.
