DoAssignment study guide
B3.2 · Apply mathematical formulas in algorithms
Learn to apply mathematical formulas in algorithms through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Software Development
ICS3U study topic B3.2: turning a mathematical rule into clear program steps
A mathematical formula describes how quantities are related. An algorithm is a set of steps for solving a problem. In this lesson, you will connect the two: identify the values a formula needs, express its calculation in a programming language, and trace the values to find the output. The focus is the ICS3U study topic B3.2, applying mathematical formulas in algorithms. Python 3 is used for the short examples; classroom languages and their syntax may differ.
What you will learn
- Explain how a mathematical formula can become part of an algorithm.
- Identify the inputs, calculation, and output in a formula-based program.
- Translate a formula into Python 3 while respecting the order of operations.
- Trace changing variable values to determine an expected result.
From a formula to input, process, and output
Suppose you want a program to calculate the area of a rectangle. The mathematical rule uses the rectangle's length and width. In a program, those values are inputs: they are the information the calculation needs. The process is multiplying the values, and the output is the calculated area.
Before writing code, name each quantity and its unit. If length and width are measured in centimetres, their product is an area in square centimetres. Clear names such as length and width make it easier to check that the code matches the intended formula.
A formula can be applied directly when its required values are known. If a task requires several calculations, write down the intended relationship first, then translate each operation into the language's syntax. This keeps the program connected to the mathematical rule rather than relying on guesswork.
- Inputs are the values a calculation needs.
- The process applies the formula to those values.
- The output is the result, with units that fit the quantity.
Translate the formula carefully
In Python 3, multiplication is written with an asterisk, such as length * width. The equals sign in an assignment statement stores a value in a variable; it does not mean that two mathematical expressions are being compared. A variable can hold an input or a result.
The calculation must preserve the formula's meaning. Parentheses can make a grouped operation clear, especially when a formula includes addition or subtraction as part of a numerator or denominator. Follow the formula as written rather than changing its grouping.
A useful check is to substitute the input values into the formula by hand and compare that calculation with a trace of the program. A trace lists the important variable values as the instructions are followed. It lets you derive the expected output without claiming that the code was run.
- Use the programming language's operators to represent the formula's operations.
- Use parentheses to preserve grouping when needed.
- Trace values from the stated inputs to derive the expected result.
Check quantities and reasonable results
A formula-based algorithm is easier to review when you ask what each variable represents and what units it should have. For example, adding two lengths gives a length, while multiplying two lengths gives an area. Units do not replace the calculation, but they can reveal that a result has been described incorrectly.
Check the result by estimating its size or repeating the arithmetic independently. If the calculation is for a rectangle with a length of 8 centimetres and a width of 3 centimetres, an area of 24 square centimetres is consistent with the inputs. An answer such as 11 would suggest that the values were added instead of multiplied.
The examples that follow use simple formulas and small inputs so each step can be traced. The goal is to connect a stated mathematical relationship to program variables and an expected output.
- Give variables meaningful names and track units in your explanation.
- Check both the arithmetic and whether the result fits the quantity.
- A trace is a reasoned prediction from code and inputs, not evidence of an actual run.
A repeatable method
For a new formula-based task, first identify the requested output and the values needed to calculate it. Next, write the formula and substitute the given or input values. Then translate the operations into the chosen language, trace the variables, and state the expected output with an appropriate label or unit.
If a task supplies an expression rather than a named formula, use the same approach: preserve the grouping and operations, and make clear which value is stored at each step. Do not add extra calculations that the task does not require.
- Identify the output, required inputs, and formula.
- Translate the formula without changing its meaning.
- Trace the calculation and check the result.
Trace for the rectangle-area example
| Instruction | length | width | area |
|---|---|---|---|
| Inputs assigned | 8 | 3 | not assigned |
| Calculate area | 8 | 3 | 24 |
| Expected printed value | 8 | 3 | 24 |
Trace for the mean example
| Calculation stage | Value |
|---|---|
| score1 + score2 + score3 | 72 + 81 + 87 = 240 |
| Number of scores | 3 |
| mean | 240 / 3 = 80 |
Worked example
Rectangle area
A program receives a rectangle's length and width in centimetres. Use the area formula to determine the expected area when the inputs are 8 and 3.
Python 3
length = 8
width = 3
area = length * width
print(area)- Identify the relationshipThe inputs are length and width. The area of a rectangle is their product, so the result will be measured in square centimetres.
- Trace the calculationSubstitute the inputs: 8 multiplied by 3 gives 24. The assignment stores that product in area.
- State the outputThe expected output is an area of 24 square centimetres. This result follows from tracing the code and arithmetic; the code has not been run.
Answer: Expected area: 24 square centimetres.
Check: The product of two lengths in centimetres has units of square centimetres.
Worked example
Mean of three values
A learner records three quiz scores: 72, 81, and 87. Use the formula for the mean to determine the expected result.
Python 3
score1 = 72
score2 = 81
score3 = 87
mean = (score1 + score2 + score3) / 3
print(mean)- Identify inputs and formulaThe three scores are the inputs. To find their mean, add them and divide the total by the number of scores, which is three.
- Calculate the total and meanThe scores add to 240. Dividing 240 by 3 gives 80.
- Interpret the resultThe expected mean score is 80. It is a score on the same scale as the three inputs.
Answer: Expected mean: 80.
Check: The result lies between the lowest score, 72, and the highest score, 87, which is consistent with these inputs.
Worked example
Convert minutes to seconds
A program receives a duration of 4 minutes. Use the relationship between minutes and seconds to determine the expected number of seconds.
Python 3
minutes = 4
seconds = minutes * 60
print(seconds)- Choose the conversion relationshipThere are 60 seconds in one minute. Multiply the number of minutes by 60 to express the same duration in seconds.
- Substitute the inputThe input is 4 minutes, so calculate 4 multiplied by 60. The result is 240.
- Give the expected outputThe program should report 240 seconds. The input and output describe the same duration using different units.
Answer: Expected duration: 240 seconds.
Check: Because each minute contains 60 seconds, four minutes must contain four groups of 60 seconds.
Common mistakes and how to avoid them
Adding length and width to calculate a rectangle's area.
Correction: Use the area formula, which multiplies length by width. Addition would not apply the required relationship.
Dividing a total by the wrong number when finding a mean.
Correction: Divide by the number of values included in the total. In the example, there are three scores.
Leaving out parentheses around the sum in a mean calculation.
Correction: Group the values being added before dividing. In Python 3, `(score1 + score2 + score3) / 3` makes that grouping explicit.
Reporting a numerical result without identifying what it measures.
Correction: Name the output and use appropriate units when relevant, such as square centimetres for area or seconds for a duration.
Lesson summary
- A formula gives a mathematical relationship that an algorithm can apply to input values.
- Identify the inputs, calculation, output, and any relevant units before writing code.
- Translate the formula into language syntax without changing its operations or grouping.
- Trace variable values and check the expected result rather than claiming code was executed.
Check your understanding
Question 1
A program calculates the area of a rectangle with length 9 cm and width 4 cm. What result should it report?
- 13 square centimetres
- 36 square centimetres
- 26 square centimetres
- 36 centimetres
Show answer and explanation
36 square centimetres
Area is length multiplied by width: 9 multiplied by 4 is 36. Multiplying two centimetre lengths gives square centimetres.
Question 2
A program finds the mean of 6, 9, and 12. Which calculation applies the formula correctly?
- (6 + 9 + 12) / 3 = 9
- (6 + 9 + 12) / 2 = 13.5
- 6 + 9 + 12 / 3 = 19
- (6 + 9 + 12) * 3 = 81
Show answer and explanation
(6 + 9 + 12) / 3 = 9
There are three values, so add all three and divide their total, 27, by 3. The mean is 9.
Question 3
A duration is 7 minutes. Using 60 seconds per minute, what is the equivalent duration in seconds?
- 67 seconds
- 420 seconds
- 420 minutes
- 13 seconds
Show answer and explanation
420 seconds
Multiply the number of minutes by 60: 7 multiplied by 60 is 420 seconds.
Key terms
- Algorithm
- A set of steps for solving a problem.
- Formula
- A mathematical rule that describes a relationship between quantities.
- Input
- A value supplied to a calculation or program.
- Trace
- A record of variable values as program instructions are followed.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
- B3.1 · Design simple array algorithms to a specification
- B4.1 · Describe development phases, milestones, and products
- B1.1 · Choose problem-solving strategies for different tasks
- B1.2 · Solve problems alone and in a team
- B1.3 · Plan a solution using an input–process–output model
- B2.1 · Adapt a program template to a specification
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation B3.2. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.