Problem overview
Three test scores are stored in the constants test1, test2 and test3. The program must compute their average as a whole number and print it after the text Average: . With the starting data, 85, 92 and 78, the sum is 255, and 255 divided by 3 is exactly 85, so the program prints Average: 85.
This is the first version of a small report-card program. Later versions build on it, so the habits it teaches matter: keep the data in named constants, compute a result in one place, then print it.
Approach: add first, then divide
An average is the sum of the values divided by how many there are. Here there are three values, so the plan is a single expression: add the three tests, divide the total by 3, store the result in a constant called average.
The statement asks for a whole number, and Swift gives you one for free. When both operands of / are Int, the result is an Int too, and the fractional part is dropped. The quotient is cut toward zero, which for positive scores means it is rounded down. That is why the lesson needs no rounding function.
Step by step
- Create
average. Uselet, because the value is calculated once and never changes. Put the three additions inside parentheses so the whole sum is divided by 3. - Print the result. Write the text
Average:and the value ofaveragein one string with interpolation: a backslash followed by the name in parentheses. Swift converts the integer to text for you.
The solution in Swift
let test1 = 85
let test2 = 92
let test3 = 78
let average = (test1 + test2 + test3) / 3
print("Average: \(average)")
Three details are worth knowing.
- The parentheses are required. Division binds tighter than addition, exactly as in arithmetic class. Without them,
test1 + test2 + test3 / 3divides only the last test: with this data it gives 85 + 92 + 26, which is 203, not 85. - Type inference. Every literal here is an integer, so Swift infers
Intfor the three tests and foraverage. You never write the type, yet it is fixed:averagecan't later hold a decimal number. - Interpolation. Inside a string literal,
\(average)is replaced by the value's text. The expression can be any Swift expression, but a plain name keeps the line easy to read.
Edge cases and common mistakes
- Dropping the remainder. With scores 70, 71 and 72 the sum is 213 and the program prints 71. With 100, 100 and 99 the sum is 299, the exact average is 99.67, and the program prints 99. A score set of 0, 0 and 1 prints 0. The value is never rounded up.
- Dividing by 3.0. Writing
let average: Int = total / 3.0is a compile error:cannot convert value of type 'Double' to expected argument type 'Int'. Swift never mixesIntandDoublein one operation by itself. Keep both sides integers for this lesson. - Negative numbers. Integer division truncates toward zero, so
-7 / 2is -3, not -4. Test scores are not negative, but the rule is worth remembering. - Printing something else. The lesson's tests read the output text, so the line must be exactly
Average: 85, with the space after the colon and no extra characters.
Testing
Change the three starting values and run the program again. Each of these results comes from the solution above.
- 85, 92 and 78 print
Average: 85. - 100, 100 and 99 print
Average: 99. - 0, 0 and 1 print
Average: 0. - 70, 71 and 72 print
Average: 71.
Try the same in the editor above, and watch for the cases where the exact average has a fraction.
