Start learning free

LESSON 202 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Report Card V1 in Swift: One Subject Average

Practice Read the full explanation

Compute a student's average score across three tests using integer division so the result is a whole number.

You start with:

let test1 = 85
let test2 = 92
let test3 = 78

Build:

Average: 85

Rules:

  • Compute the integer average of test1, test2, and test3 using integer division (Int divided by Int already drops the remainder).
  • Print "Average: " followed by the average, for example print("Average: \(average)")

Swift's Int / Int truncates, so (test1 + test2 + test3) / 3 already gives an integer.

let test1 = 85
let test2 = 92
let test3 = 78

let average = (test1 + test2 + test3) / 3
print("Average: \(average)")

STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V1 in Swift, explained

    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

    1. Create average. Use let, because the value is calculated once and never changes. Put the three additions inside parentheses so the whole sum is divided by 3.
    2. Print the result. Write the text Average: and the value of average in 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 / 3 divides 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 Int for the three tests and for average. You never write the type, yet it is fixed: average can'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.0 is a compile error: cannot convert value of type 'Double' to expected argument type 'Int'. Swift never mixes Int and Double in one operation by itself. Keep both sides integers for this lesson.
    • Negative numbers. Integer division truncates toward zero, so -7 / 2 is -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.

    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free