DoAssignment study guide
C2.2 · Create code to represent relationships and solve problems
Learn to create code to represent relationships and solve problems through clear examples and targeted practice.
Ontario Grade 9 Mathematics
Algebra
Ontario MTH1W – Strand C: Coding, Expectation C2.2
You already use relationships every day without thinking about it. If a movie ticket costs CAD 12 and you buy several tickets, the total cost depends on how many you buy. That connection — total cost equals price per ticket times the number of tickets — is a mathematical relationship. In this lesson you will learn how to take that kind of relationship, express it clearly in plain language, and then write it as working code. Code is just a precise set of instructions that a computer can follow. When you write code that captures a mathematical relationship, the computer can solve the same problem for any input value in a fraction of a second. By the end of this lesson you will be able to read a problem, spot the relationship hiding inside it, and write a short program that represents and solves it.
What you will learn
- Identify a mathematical relationship in a real-world problem and translate it into code.
- Use variables, expressions, and conditional statements in code to represent relationships.
- Trace through code step by step to predict its output.
- Modify or write a short program to solve a new but related problem.
What Is a Mathematical Relationship?
A mathematical relationship describes how two or more quantities are connected. One quantity changes, and the other quantity changes in response. For example, the total cost of buying apples depends on how many kilograms you buy. The distance a cyclist travels depends on how long they ride.
Before writing any code, you need to identify two things: the input (the value you are given or that changes) and the output (the value you want to find). In the apple example, the number of kilograms is the input and the total cost is the output.
The rule that connects input to output is called the relationship or formula. For the apple example, if apples cost CAD 3 per kilogram, the relationship is: total cost equals 3 times the number of kilograms. In symbols: . Your job as a programmer is to write code that carries out this rule for any input the user supplies.
output = rule applied to input
- A relationship connects an input quantity to an output quantity.
- Identify the input and output before writing any code.
- The rule connecting them is written as a formula or equation.
- The same formula works for every valid input value.
Representing Relationships in Code: Variables and Expressions
In code, a variable is a named storage location that holds a value. Think of it like a labelled box. You can put a number into the box, use the number in a calculation, and update the number later. Variable names should be short but meaningful. For example, use 'kg' for kilograms and 'cost' for total cost.
An expression is a combination of variables, numbers, and operations that the computer evaluates to produce a single value. The expression '3 * kg' means 'multiply the current value stored in kg by 3'. In mathematics you write ; in most programming languages you write '3 * kg' because the asterisk (*) is the multiplication symbol.
An assignment statement stores the result of an expression into a variable. It looks like this: 'cost = 3 * kg'. Read this as 'calculate 3 times kg, then store the result in cost'. This is different from a math equation: in code, the right-hand side is calculated first, and the answer is placed into the variable on the left-hand side.
A print or output statement displays a variable's value to the user. For example, 'print(cost)' shows the value stored in cost. Together — input, assignment, and output — these three ideas let you represent almost any simple relationship in code.
- A variable is a named storage location for a value.
- An expression combines variables, numbers, and operators to produce a value.
- An assignment statement stores a calculated value into a variable.
- Use * for multiplication, / for division, + for addition, and - for subtraction in code.
- A print statement displays a result to the user.
Using Conditional Statements to Represent Branching Relationships
Some relationships have more than one rule, depending on the situation. For example, a parking lot charges CAD 5 for the first hour and CAD 2 for each additional hour. The rule changes based on how long you park. In code, you handle this with a conditional statement — an if/else block.
A conditional statement checks whether a condition is true or false. A condition is a comparison, such as 'hours <= 1' (hours is less than or equal to 1) or 'hours > 1' (hours is greater than 1). If the condition is true, the computer runs one set of instructions. If it is false, the computer runs a different set. This mirrors the way the relationship itself branches.
Here is the parking example in plain pseudocode (pseudocode is code-like writing that shows the logic without worrying about exact programming language rules): IF hours <= 1 THEN fee = 5, ELSE fee = 5 + 2 * (hours - 1). In math, the two rules are: when , and when .
Trace through a specific value to check your code makes sense. If hours = 3: the condition 'hours <= 1' is false, so the computer uses the else rule: . The fee is CAD 9. Always trace through at least one example to verify your code matches the intended relationship.
- A conditional statement (if/else) lets code follow different rules depending on a condition.
- A condition is a comparison that is either true or false.
- Trace through your code with a specific number to check it gives the right answer.
- Pseudocode lets you plan logic without worrying about exact syntax.
Planning Your Code: From Problem to Program
Good coders do not jump straight to typing. They follow a planning process. Step 1: Read the problem carefully and underline the quantities. Step 2: Decide which quantity is the input and which is the output. Step 3: Write the mathematical relationship as a formula. Step 4: Decide whether the relationship is always the same rule (use a simple expression) or depends on a condition (use if/else). Step 5: Write the code, using meaningful variable names. Step 6: Trace through the code with at least two test values to check it is correct.
Choosing good variable names is important. A name like 'h' for hours or 't' for total saves typing but can be confusing later. A name like 'hours' or 'total_fee' makes the code easier to read and check. Spaces are not allowed inside variable names in most languages; use an underscore (_) instead.
Testing your code is not optional — it is a required part of coding. Choose test values where you already know the correct answer, then compare what your code produces. If the output is wrong, re-read the relationship and check each assignment statement one at a time. This process is called tracing or debugging.
output variable = f(input variable)
- Plan before you code: identify input, output, and the relationship.
- Use meaningful variable names to make code readable.
- Always test with values whose correct answers you already know.
- Debugging means finding and fixing errors by tracing through the code step by step.
Tracing the Phone Bill Code for Different Input Values
| texts (input) | 0.05 × texts | bill = 20 + 0.05 × texts (output, CAD) |
|---|---|---|
| 0 | ||
| 40 | ||
| 60 | ||
| 100 | ||
| 200 |
Worked example
Writing Code to Calculate a Phone Bill
A phone plan charges a flat monthly fee of CAD 20 plus CAD 0.05 for every text message sent. Write pseudocode that takes the number of text messages as input, calculates the monthly bill, and displays the result. Then trace through the code for 60 text messages.
- Step 1 – Identify input and outputThe number of text messages is what changes each month — that is the input. The monthly bill is what we want to find — that is the output.
- Step 2 – Write the mathematical relationshipThe bill is always the flat fee plus the cost per text times the number of texts. There is only one rule, so no conditional is needed.
- Step 3 – Write the pseudocodeLine 1 receives the input from the user and stores it in a variable called 'texts'. Line 2 calculates the bill using the formula and stores the result in 'bill'. Line 3 displays the result.
- Step 4 – Trace for texts = 60Substitute 60 for 'texts' in the formula and calculate. First multiply 0.05 by 60, then add 20.
- Step 5 – Verify the answer makes senseThe flat fee alone is CAD 20. Sixty texts add CAD 3.00, giving CAD 23. This is reasonable: the bill is slightly above the base fee, which matches what we expect for a moderate number of texts.
Answer: The monthly bill for 60 text messages is CAD 23.00.
Check: Test with 0 texts: . The bill equals the flat fee only — correct. Test with 200 texts: . CAD 30 is reasonable for a heavy texter.
Common mistakes and how to avoid them
Writing the assignment backwards, e.g. writing 'bill = 20 + 0.05 * texts' but reading it as 'bill equals texts times 0.05 plus 20 stored back into texts'. Students sometimes confuse which side of the '=' is the output.
Correction: In an assignment statement, the computer always evaluates the right-hand side first and stores the result in the variable on the left-hand side. The variable on the left receives the value; it is never used in the calculation on the right at the same time.
Forgetting to use * for multiplication in code and writing '3kg' or '3(kg)' instead of '3 * kg', which causes a syntax error or unexpected result.
Correction: In almost all programming languages, multiplication must be written explicitly with the * symbol. Write '3 * kg', not '3kg' or '3(kg)'.
Using the same variable name for two different quantities, e.g. using 'cost' for both the price per kilogram and the total cost, which overwrites the first value.
Correction: Give each quantity its own unique, meaningful variable name. For example, use 'price_per_kg' and 'total_cost' so the code is clear and each value is stored separately.
Writing a conditional with the wrong comparison operator, e.g. writing 'hours < 1' instead of 'hours <= 1', so the boundary value (exactly 1 hour) is handled by the wrong rule.
Correction: Check carefully whether the boundary value belongs to the first rule or the second. Use <= (less than or equal to) or >= (greater than or equal to) when the boundary is included in that case.
Skipping the tracing step and assuming the code is correct without testing it against known values.
Correction: Always trace through your code with at least two test values — one straightforward case and one boundary case. Compare the code's output to the answer you calculated by hand.
Lesson summary
- A mathematical relationship connects an input quantity to an output quantity through a rule or formula.
- In code, variables store values, expressions calculate new values, and assignment statements save results.
- Use a simple expression when one rule covers all cases; use an if/else conditional when the rule changes depending on a condition.
- Plan before coding: identify the input, output, and relationship, then choose the right code structure.
- Always trace through your code with known test values to verify it produces correct results.
- Meaningful variable names and careful tracing are habits that prevent most common coding errors.
Check your understanding
Question 1
A store sells notebooks for CAD 4 each. Which line of code correctly calculates the total cost for a given number of notebooks stored in the variable 'n'?
- total = 4 + n
- total = 4 * n
- total = n / 4
- total = n - 4
Show answer and explanation
total = 4 * n
The total cost is the price per notebook multiplied by the number of notebooks. In code this is 'total = 4 * n', which represents . Adding, dividing, or subtracting 4 does not match the relationship described.
Question 2
A programmer writes: 'fee = 5 + 2 * (hours - 1)' for the case when hours > 1. What does the code produce when hours = 4?
- CAD 9
- CAD 11
- CAD 13
- CAD 7
Show answer and explanation
CAD 11
Substitute hours = 4: . The fee is CAD 11.
Question 3
A student writes the following pseudocode to find the perimeter of a square with side length s: 'perimeter = s + 4'. What is wrong with this code?
- The variable name 'perimeter' is too long.
- The formula uses addition instead of multiplication; it should be 'perimeter = 4 * s'.
- The variable s should be named 'side_length'.
- There is nothing wrong; the code is correct.
Show answer and explanation
The formula uses addition instead of multiplication; it should be 'perimeter = 4 * s'.
The perimeter of a square is , not . Adding 4 to s gives the wrong relationship. The correct assignment is 'perimeter = 4 * s'.
Question 4
Which planning step should come directly BEFORE writing the code for a new problem?
- Run the code and see what output appears.
- Test the code with several values.
- Identify the input, output, and the mathematical relationship.
- Print the output variable.
Show answer and explanation
Identify the input, output, and the mathematical relationship.
Good coding practice requires you to understand the problem fully before writing any code. You must identify what the input is, what the output is, and what rule connects them. Only then can you write code that correctly represents the relationship.
Key terms
- Variable
- A named storage location in a program that holds a value. The value can be used in calculations or changed as the program runs.
- Expression
- A combination of variables, numbers, and operators (such as +, -, *, /) that the computer calculates to produce a single value.
- Assignment statement
- A line of code that calculates the value of an expression on the right-hand side and stores the result in the variable on the left-hand side.
- Input
- The value or values that are given to a program or formula. The input is what changes from one use of the program to the next.
- Output
- The value that a program or formula produces after applying the rule to the input.
- Conditional statement
- An if/else block in code that checks a condition and runs different instructions depending on whether the condition is true or false.
- Pseudocode
- An informal, plain-language description of code logic that shows the steps and structure without following the exact rules of any one programming language.
- Tracing
- The process of following a program step by step, substituting specific values, to predict or check the output. Also used to find and fix errors.
Continue through MTH1W
View the complete MTH1W Ontario Grade 9 Mathematics curriculum and lessons
Use the free MTH1W test and exam review guide
- C2.1 · Use coding to represent algebraic concepts
- C2.3 · Predict and modify the outcome of code
- C1.1 · Explore the development and use of an algebraic concept
- C1.2 · Create expressions that generalize relationships
- C1.3 · Compare expressions and identify equivalent forms
- C1.4 · Simplify algebraic expressions
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 9 Mathematics (MTH1W), expectation C2.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.