Grade by Grade
C. Algebra · Algebra: Patterns, Rational Numbers, Equations, Inequalities, Coding and Modelling

Reading and Altering Data Code: Outcomes and Efficiency

C3.2 A1About 50 minutes
Learning goals
  • I can read code that analyses data and predict its output.
  • I can alter the data, the conditions or the calculations in code to answer a new question.
  • I can describe how a change affects the outcome and the efficiency of the code.

Words to know

outcome
What the code produces: its outputs and the decision it makes.
efficiency
How few steps and lines the code needs, and how easy it is to change and reuse.
alter
To change code on purpose, to fix it or make it do something new.
hard-coded
Written directly into the code in a way that only works for one situation, like dividing by 6 when there are exactly 6 values.
bug
A mistake in code that makes it give a wrong or unexpected outcome.

Remember from Grade 7 and the last lesson

In Grade 7 you read and altered code with counters and sub-programs, and described how each change affected the outcome and the efficiency. In the last lesson you wrote code that analyses data with lists and FOR EACH loops.

Today you'll read someone else's data code, predict what it does, and alter it to answer new questions. For every change, ask:

  1. How does the outcome change?
  2. How does the efficiency change?

Let's start with the eco-club

A school eco-club weighs the recycling it collects each week. Their goal: a mean of at least 13 kg per week. Last year's club left this code. Read it and predict the outputs before reading on.

Blue recycling bins full of paper, cardboard and plastic bottles stand in a school hallway next to a flat floor scale.
An eco-club can weigh its recycling on a scale each week. Each weekly mass becomes one value in the data list that the code reads. AI-generated image
SET recycling = [14.5, 12, 16.5, 11, 15, 9]
SET total = 0
SET count = 0
SET low = 0
FOR EACH kg IN recycling
ADD kg to total
ADD 1 to count
IF kg < 12 THEN
ADD 1 to low
SET mean = total ÷ count
SAY mean
SAY low
IF mean ≥ 13 THEN
SAY "Goal met"
ELSE
SAY "Keep trying"

Code B: the eco-club's code. It finds the mean, counts low weeks (under 12 kg), and makes a decision.

kgtotalcountlow
14.514.510
1226.520
16.54330
115441
156951
97862
Tracking table. After the loop: mean = 78 ÷ 6 = 13.

Outputs: 13, then 2, then "Goal met" (13 ≥ 13 is true).

Notice that 12 is not counted as low, because 12 < 12 is false.

05101520recycling (kg)14.5week 112week 216.5week 311week 415week 59week 6mean 13 = goallow: under 12
The two yellow bars (11 kg and 9 kg) are under 12 kg, so low = 2. Week 2 is exactly 12 kg, which is not under 12. The mean is exactly 13 kg, so the goal is just met.
AlterationNew outcomeEffect on efficiency
IF mean ≥ 13 → IF mean ≥ 14mean is still 13, but now says "Keep trying"No change in lines or steps.
Add a 7th week, 6 kg, to the listtotal 84, count 7, mean 12, low 3, "Keep trying"Only the list changes. One more loop run.
IF kg < 12 → IF kg ≤ 12low becomes 3 (12, 11 and 9)No change in lines or steps.
Find the mean in one loop and the low weeks in a second loopSame outputsLess efficient: the list is looped through twice (12 runs instead of 6).
How four alterations change Code B.

Hard-coded code is less flexible

Here is a different version, Code A, that does the same job for the mean:

SET w1 = 14.5
SET w2 = 12
SET w3 = 16.5
SET w4 = 11
SET w5 = 15
SET w6 = 9
SET mean = (w1 + w2 + w3 + w4 + w5 + w6) ÷ 6
SAY mean

Code A: hard-coded for exactly 6 weeks.

Code A works today. But to add week 7, you must add a line (SET w7 = 6), add w7 to the sum, and change ÷ 6 to ÷ 7. Forget any one of those and the mean is wrong. In Code B you only change the list: the loop and the counter adjust by themselves. Code B is more efficient to change and reuse, even though both give 13 today.

Code A (hard-coded)Code B (list + loop)To add week 7 (6 kg):1. add the line SET w7 = 62. add + w7 to the sum3. change ÷ 6 to ÷ 71. add 6 to the listThe loop and the counteradjust by themselves.3 edits: forget one and the mean is wrong1 edit
Adding one week to Code A needs 3 edits, and forgetting any one gives a wrong mean. Code B needs only 1 edit: add the value to the list.

Predict, alter, trace

Before you run altered code, predict the new output. Then trace it with a tracking table. If your prediction was wrong, find out why. That's how you learn what each line really does.

Worked example

Example 1: Alter a condition

Change Code B so that low counts weeks under 15 kg. What is the new value of low?

  1. Change the line IF kg < 12 THEN to IF kg < 15 THEN.
  2. Weeks under 15: 14.5, 12, 11, 9. (15 is not under 15.)

low = 4. The mean and the decision don't change.

Worked example

Example 2: Alter the data

The club finds that week 4 was recorded wrong: it was 17 kg, not 11. Predict the new outputs.

  1. The total goes up by 6: 78 + 6 = 84.
  2. mean = 84 ÷ 6 = 14.
  3. low weeks: only 9 is under 12 now, so low = 1.

Outputs: 14, 1, "Goal met".

Worked example

Example 3: Add a new feature

Alter Code B so it also says the best week's mass.

  1. Before the loop, add: SET best = first item of recycling.
  2. Inside the loop, add: IF kg > best THEN SET best = kg.
  3. After the loop, add: SAY best.

It also says 16.5. It adds 3 lines but still loops through the list only once, so it stays efficient.

Worked example

Example 4: Find the bug

A student moved SET total = 0 inside the loop, just above ADD kg to total. What happens?

  1. Each time through the loop, total is reset to 0, then the current kg is added.
  2. After the last run, total = 9 (only the last week).
  3. mean = 9 ÷ 6 = 1.5, so the code says "Keep trying".

The outcome is wrong (mean 1.5). Starting values must be set once, before the loop.

Hands-on activity

Code detective

You need: pencil, paper, and Code B (copy it out).

  1. Collect your own household data for 5 to 7 days. Ideas: kilograms or bags of garbage, litres of milk used, minutes of screen time.
  2. Alter Code B for your data: change the list, the goal and the "low" condition. Rename the variables so they make sense (for example, minutes instead of kg).
  3. Predict the outputs, then trace the code with a tracking table.
  4. Make two more alterations of your own. For each, write down the new outcome and whether the code became more or less efficient.
  5. Tell someone at home what decision your code supports and why.

What to look for: only the list, the goal and the condition needed to change; the loop works for any number of days; each alteration is predicted before tracing.

Just for fun: a real bug

In 1947, people working on the Harvard Mark II computer found a real moth stuck inside it. They taped it into their logbook with the note "First actual case of bug being found." The word "bug" for a problem is older than that, but this moth made it famous.

Your turn

1

What does this code say? SET count = 0 / FOR EACH x IN [5, −2, 8, −6, 0] / IF x > 0 THEN / ADD 1 to count / SAY count

2

Alter that code to IF x ≥ 0. What does it say now?

3

Predict Code B's outputs if the list is [14, 12, 13].

4

In Code B, change IF mean ≥ 13 to IF mean > 13. With the original data, what is the decision?

5

List the changes Code A needs to include a 7th week. How many changes does Code B need?

6

A list has 50 values. How many loop runs does it take to find the mean and the highest value with two separate loops? With one combined loop?

More practice: warm-up

1

With the original data, what does Code B say for low?

2

If you change SAY mean to SAY total in Code B, what is said instead of 13?

3

What is count at the end of Code B with the original list?

4

Which line would you change to make the goal 15 kg?

5

True or false: in Code B, adding more values to the list changes the outcome but you don't need to change any other line.

More practice: on your own

1

Alter Code B to count the weeks with more than 15 kg. Which lines change, and what is the result?

2

The club wants the mean of just the last 3 weeks. Alter the data and give the outcome.

3

A student's code to find the best week starts with SET best = 0. Why does it work for recycling data but not for winter temperature data?

4

Every value in the list goes up by 2 kg. Predict the new mean, low and decision.

5

Code B finds the mean and the low count in one loop. Explain why that is more efficient than two loops.

Challenge

1

Alter Code B so it also says which week number had the lowest mass. Give the output for the original data.

2

Last year's data was [12, 13.5, 10, 14, 12.5, 11]. Use a MEAN sub-program to compare the two years. Which year was better, and by how much?

3

A student "improves" Code B by deleting the count lines and writing SET mean = total ÷ 6. Is the outcome the same today? Is the code better or worse? Explain.

Check your understanding

Quiz

1.

With the original data, what is Code B's decision?

2.

If IF kg < 12 is changed to IF kg ≤ 12, the value of low becomes...

3.

Why is Code B easier to update than Code A?

4.

A list has 30 values. One loop finds both the total and the highest value. How many loop runs is that?

5.

A student puts SET total = 0 inside the loop. What happens?

6.

Week 4 changes from 11 kg to 17 kg. What is the new mean?

For parents and teachers

Big idea: C3.2 asks Grade 8 students to read and alter existing code involving the analysis of data in order to inform and communicate decisions, and to describe how changes affect the outcomes and the efficiency of the code.

Common difficulties:

  • Not predicting before tracing. Prediction is where the learning happens.
  • Boundary conditions: < versus ≤, and > versus ≥, change counts and decisions when a value lands exactly on the line.
  • Thinking shorter always means more efficient. Hard-coded code can be shorter but harder to update correctly.
  • Starting values in the wrong place (inside the loop).

How to help: Use real family data in the "Code detective" activity, and ask "If we change this one line, what will happen?" before tracing. This lesson builds A1 skills: critical thinking and communicating decisions clearly.