Grade by Grade
C. Algebra · Algebra: Expressions, Equations and Coding

Reading, Predicting and Altering Code

C2.3 C2.2 A1About 70 minutes
Learning goals
  • I can trace code with a trace table and predict its output.
  • I can alter a parameter in code to represent a similar situation with different numbers.
  • I can alter a constraint, such as a loop range or a condition, and explain how the outcome changes.
  • I can adapt code from one situation to a new one, including problems from other strands.

Words to know

trace table
A table with a column for each variable and a row for each step, used to follow code by hand.
parameter (in a model)
A number in the code that describes the situation, such as the amount of fencing or the weekly savings. Changing it gives a similar situation.
constraint
A limit or condition the code must respect, such as a loop range, or the condition in an if or while statement.
outcome
What the code produces: its output or the decision it makes.
debug
Find and fix mistakes in code.

Let's start with a fence

A family has 40 m of fencing for a rectangular garden. They want the largest possible area. Should the garden be long and thin, or closer to a square?

If the width is w, then two widths and two lengths use 40 m, so each length is 20 − w. The area is w(20 − w).

5 by 15area 75 m²10 by 10area 100 m²15 by 5area 75 m²Each rectangle uses 40 m of fence (perimeter 40 m).
Three gardens with the same 40 m perimeter. The 10 m by 10 m garden has the largest area of these three. Is it the largest of all?

Instead of drawing every rectangle, here is code that tries every whole-number width and keeps the best one. Before reading the output, predict it.

best_area = 0
best_w = 0
for w in range(1, 20):
area = w * (20 - w)
if area > best_area:
best_area = area
best_w = w
print(best_w, best_area)

Output: 10 100

Reading code: trace tables

To predict what code does, trace it. Make a column for each variable and a row each time something changes. Here is part of a trace for the fence code:

wareaarea > best_area?best_wbest_area
119yes119
236yes236
351yes351
...............
10100yes10100
1199no10100
A trace table for the fence code. Each row is one run of the loop.

After w = 10, the areas get smaller (99, 96, 91, ...), so best_w and best_area never change again. The output is 10 100: a 10 m by 10 m square.

246810121416182020406080100120(10, 100)area (m²)width w (m)
Graphing the loop's results: area against width. The points rise, peak at (10, 100), then fall. The pattern is not linear, which is why a loop that tries every width is handy.

Altering code

Once code works, you can alter it to model other situations:

  • Change a parameter: a different amount of fencing, a different price, a different savings goal.
  • Change a constraint: a different loop range, or a different condition in an if or while statement.
  • Change the model: a new expression for a new situation.

After every change, predict the new output first, then run or trace to check.

Worked example

Example 1: Change a parameter

Alter the fence code for 60 m of fencing. Predict the output.

  1. With 60 m, each length is 30 − w. Change the area line to area = w * (30 - w).
  2. Widths can now go up to 29, so change the loop to range(1, 30).
  3. Prediction: a square again, 15 m by 15 m, area 225 m².

Output: 15 225.

Worked example

Example 2: Change the model

The garden is now against a barn wall, so the fence covers only three sides: two widths and one length. There is still 40 m of fence. Alter the code.

  1. Two widths use 2w, so the length is 40 − 2w.
  2. Change the area line to area = w * (40 - 2 * w). The length must stay positive, so w goes up to 19: range(1, 20) still works.
  3. Trace a few: w = 5 → 5 × 30 = 150; w = 10 → 10 × 20 = 200; w = 11 → 11 × 18 = 198.

Output: 10 200. The best barn-side garden is 10 m wide and 20 m long: not a square this time!

Worked example

Example 3: Change a constraint

In the savings code from the last lesson (start $120, add $35 a week, while savings < 400:), what happens if the condition is changed to while savings <= 400:?

  1. After 8 weeks, savings = 400. With <, the condition 400 < 400 is False, so the loop stops.
  2. With <=, the condition 400 <= 400 is True, so the loop runs once more: savings = 435, weeks = 9.
  3. The output changes from 8 400 to 9 435.

One small symbol changed the outcome. When you alter a constraint, always check the boundary case.

Altering code like a pro

  • Before running altered code, write your prediction. Then compare.
  • Change one thing at a time, so you know what caused each change in the output.
  • Look for every place a parameter appears. In the fence code, “20” appears in the area line, and the loop range depends on it too.
  • Code from one strand often fits another: the same loop-and-keep-the-best pattern works for geometry, money and data problems.
Hands-on activity

String rectangles

You need: a piece of string exactly 40 cm long (cut with scissors carefully), grid paper or a ruler, a pencil.

  1. Tie or tape the string into a loop. Its perimeter is 40 cm.
  2. Shape it into rectangles on the grid paper with widths 2, 5, 8, 10, 12 and 15 cm. Record each width, length and area in a table.
  3. Which rectangle has the biggest area? Does it match the code's output for 40 m of fencing (10 100)?
  4. Now pretend one side is a wall (only three sides need string). Use the full 40 cm for three sides and try widths 5, 8, 10 and 12. Which is biggest? Compare with Example 2.
  5. Write which parameter or line of the code you would change to model a 30 cm string.

Model answer: for the full loop, 10 × 10 = 100 cm² is biggest. Against a wall: w = 10 gives 10 × 20 = 200 cm², bigger than w = 8 (8 × 24 = 192) or w = 12 (12 × 16 = 192). For 30 cm, change the area line to area = w * (15 - w) and the loop to range(1, 15).

Just for fun

There are 10 kinds of people in the world: those who understand binary, and those who don't.

(In binary, 10 means two.)

Your turn

1

Trace and predict:

x = 2

for i in range(3): x = x * 3

print(x)

2

Alter the fence code for 32 m of fencing. Which two lines change, and what is the new output?

3

The toothpick-table code is for n in range(1, 6): print(n, 3 * n + 1). Alter it to print the triangles pattern 2n + 1 instead. What is printed?

4

Predict the output:

count = 0

for n in range(1, 11): with this line indented inside: if 2 * n + 1 > 12: count = count + 1

print(count)

5

In the savings code, the goal 400 is changed to 500. Predict the output.

6

In the barn code, which part is the model and which is the constraint?

7

How could you alter the snow-company code (two functions and an if) to compare three companies?

More practice: warm-up

1

How many times does for n in range(1, 6): repeat?

2

The code says print(cost(3)). How do you alter it to find the cost for 4 hours?

3

Predict:

total = 0

for k in range(1, 4): total = total + k * k

print(total)

4

In the fence code, what does the number 20 represent?

5

Predict print(10 * (20 - 10)).

More practice: on your own

1

Predict:

x = 100

while x > 10: x = x / 2

print(x)

2

The inequality code for x in range(-5, 6): with if 2 * x + 1 > 5: print(x) is altered to test 3x − 2 ≤ 7. Write the new condition and predict the output.

3

Predict: for n in range(1, 4): print(n, n ** 2 - 1)

4

Someone wrote the savings loop with while savings <= 400: and got 9 weeks. Explain the problem and fix it.

5

Write a function best_rect(P) that uses the fence loop for any perimeter P (an even number). What does best_rect(36) give?

Challenge

1

Look at the outputs of best_rect for P = 32, 36, 40 and 60: (8, 64), (9, 81), (10, 100), (15, 225). What general rule do you notice? Explain why it makes sense.

2

Alter the compound-interest loop (amount multiplied by 1.04 each year) to find how many years it takes $1000 to double at 6% per year. Predict the output.

3

Predict the output and describe the pattern: for n in range(1, 6): print(n * (n + 1) / 2)

Check your understanding

Quiz

1.

What is a trace table used for?

2.

Predict: x = 1, then for i in range(4): x = x + 2, then print(x).

3.

To make the fence code work for 60 m of fencing, you should change:

4.

Changing a parameter in a program usually lets you:

5.

What does for n in range(2, 5): print(n * 10) print?

6.

In the fence code, why is there an if area > best_area: line?

For parents and teachers

Big idea: C2.3 asks students to read code to predict its outcome, and to alter code to adjust constraints, parameters and outcomes to represent a similar or new mathematical situation. This lesson uses the same short programs in several situations (fencing, savings, interest), connecting algebra coding to geometry and financial literacy, as the curriculum intends (C2 applies coding across the strands).

Common difficulties:

  • Running code without predicting first. The prediction is where the thinking happens.
  • Changing a parameter in one place but not another (the 20 in the fence code also controls the loop range).
  • Boundary errors: < versus <=.

How to help: Do the string-rectangle activity and compare with the code's output. Ask: “If we had 50 m of fence, what would you change in the code?” and have your teen point to the exact lines.