Problem overview
The score is now weighted. Homework counts for 20% of the final score, the midterm for 30% and the final exam for 50%. The program starts with homework = 90, midterm = 82 and finalExam = 88, and must print Score: 86.
The statement adds one rule that decides the exact result: each weighted part is rounded down to a whole number before the three parts are added. For the starting data the parts are 18, 24 and 44, and 18 + 24 + 44 is 86. Version 1 averaged three tests with equal weight; this version gives each mark its own weight.
Approach: a percentage with integers
A percentage of a mark is the mark times the weight, divided by 100. For homework that is 90 * 20 / 100. Swift evaluates * and / from left to right, so the multiplication happens first and the division second. The multiplication comes first on purpose: 90 * 20 is 1800, and 1800 divided by 100 is exactly 18.
The division of two Int values drops the remainder, so every part is already rounded down. The lesson relies on this: three expressions, three divisions, and the rounding rule is satisfied without any extra code.
The three weights, 20, 30 and 50, add up to 100, so the three parts together make up the whole score. A student with 100 in all three marks gets exactly 100.
Step by step
- Compute the score in one constant. Create
let scoreand write the three parts one after another: each mark times its weight, divided by 100, joined by+. Keeping the parts separate is what makes the rounding happen per part. - Print it. Use
printwith the textScore:followed by the value, inserted with string interpolation.
The solution in Swift
let homework = 90
let midterm = 82
let finalExam = 88
let score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100
print("Score: \(score)")
Points about the language:
- Operator precedence.
*and/share the same level and bind tighter than+, so no parentheses are needed. The expression reads as three separate products-then-quotients added together. - Naming. The third mark is called
finalExam, and the expression has to use that exact name. - Inferred integers. All the numbers are integer literals, so every operation is an
Intoperation and the result stays anInt.
Edge cases and common mistakes
- Rounding each part, not the total. With all three marks equal to 99, the parts are 19, 29 and 49, and the score is 97. If you add the unrounded products first and divide once,
(99 * 20 + 99 * 30 + 99 * 50) / 100, the answer is 99. Both are reasonable ways to compute a weighted mark, but the statement asks for rounding each part, so 97 is the answer here. - Dividing before multiplying.
homework / 100 * 20divides first: 90 / 100 is 0, so the part is 0. Keep the multiplication on the left of the division. - Weights that don't add up. If the weights were 20, 30 and 40, a perfect student would score 90. The three weights here add up to 100, and it is worth checking that when you change them.
- Mixing up the weights. The 50% weight belongs to
finalExamand the 20% weight tohomework. Swapping them gives a different score for most inputs, so check against values that are not all equal.
Testing
Each result comes from running the solution with different starting values.
- 90, 82 and 88 print
Score: 86. - 100, 100 and 100 print
Score: 100. - 99, 99 and 99 print
Score: 97. - 0, 0 and 0 print
Score: 0. - 55, 77 and 91 print
Score: 79.
Change the starting values in the editor above and compare with these numbers.
