C. Algebra · Algebra: Patterns, Rational Numbers, Equations, Inequalities, Coding and ModellingCoding to Analyse Data and Make Decisions
C3.1 A1About 50 minutes
Learning goals- I can write code that stores data in a list and loops through every item.
- I can write code to find a total, a mean, a highest value and a count of values that meet a condition.
- I can run my code by hand with a tracking table and use the output to make and explain a decision.
Words to know
list
A variable that holds many values in order, like SET temps = [−8, −12, −3].
FOR EACH loop
A loop that runs its indented lines once for every item in a list.
running total
A variable that starts at 0 and has each value added to it in turn.
mean
The total of a set of values divided by how many values there are.
condition
A true-or-false test in an IF statement, like t < −10.
tracking table
A table that shows the value of each variable after every step, so you can run code by hand.
Remember from Grade 7
In Grade 7 you wrote code with loops, variables, IF ... THEN ... ELSE, counters and sub-programs, and you ran it by hand with a tracking table.
In Grade 8 your code will analyse data: it will look through a set of numbers, calculate things like the mean or the highest value, and use the results to help make a decision. As always, no computer is needed. Write the code on paper and run it yourself, line by line.
Let's start at the outdoor rink
A town's rink crew decides when to flood the outdoor rink. Here are the daily high temperatures for the past week. (The data and the rule are made up for this lesson.)
The crew's rule: flood the rink when the mean daily high for the week is below −5°C and the highest daily high is 0°C or colder.
Should they flood it? Let's write code to decide.
Daily high temperatures for one week: −8, −12, −3, 1, −6, −14 and −7 °C. Only Thursday was above 0°C.New tool: lists and FOR EACH
A list stores many values in one variable, in square brackets. A FOR EACH loop runs the indented lines once for every item in the list. Each time, the loop variable (here t) holds the next item.
A FOR EACH loop asks "another item in the list?" If yes, it runs the indented lines and comes back. If no, the loop ends and the lines after it run once.SET temps = [−8, −12, −3, 1, −6, −14, −7]
SET total = 0
SET count = 0
FOR EACH t IN temps
ADD t to total
ADD 1 to count
SET mean = total ÷ count
SAY mean
Code 1: find the mean. Indented lines belong to the loop.
| Loop run | t | total | count |
|---|
| 1 | −8 | −8 | 1 |
| 2 | −12 | −20 | 2 |
| 3 | −3 | −23 | 3 |
| 4 | 1 | −22 | 4 |
| 5 | −6 | −28 | 5 |
| 6 | −14 | −42 | 6 |
| 7 | −7 | −49 | 7 |
Tracking table for Code 1. After the loop, mean = −49 ÷ 7 = −7.Code 1 says −7. The mean high is −7°C, which is below −5°C. ✓ Now the second part of the rule: the highest value.
SET highest = first item of temps
FOR EACH t IN temps
IF t > highest THEN
SET highest = t
SAY highest
Code 2: find the highest value. Start with the first item, then replace it whenever a bigger one comes along.
highest starts at −8. −12 is not bigger. −3 is bigger, so highest = −3. 1 is bigger, so highest = 1. Nothing after that is bigger than 1. Code 2 says 1.
Finally, the decision:
IF mean < −5 AND highest ≤ 0 THEN
SAY "Flood the rink"
ELSE
SAY "Wait"
Code 3: the decision. AND means both conditions must be true.
mean < −5 is true (−7 < −5), but highest ≤ 0 is false (1 is above 0). Both must be true, so the code says "Wait". The crew can explain the decision: "It was cold on average, but Thursday got above freezing."
Start values matter
A running total starts at 0. A counter starts at 0. But the highest value should start at the first item of the list, not 0. If every temperature were below 0 and highest started at 0, the code would wrongly say 0.
SET cold = 0
FOR EACH t IN temps
IF t < −10 THEN
ADD 1 to cold
SAY cold
Code 4: count the days with a high below −10°C.
Worked exampleExample 1: Count values that meet a condition
Run Code 4 on the rink data. What does it say?
- cold starts at 0. The loop checks each temperature: −8, −12, −3, 1, −6, −14, −7.
- Only −12 and −14 are below −10, so cold goes 0 → 1 → 2.
Worked exampleExample 2: Mean of decimal data
Run Code 1 with SET temps = [−2.5, 1.5, −4, 3].
- total: −2.5, then −1, then −5, then −2. count: 4.
- mean = −2 ÷ 4 = −0.5.
DEFINE MEAN(data)
SET total = 0
SET count = 0
FOR EACH v IN data
ADD v to total
ADD 1 to count
RETURN total ÷ count
END DEFINE
Code 5: a sub-program for the mean. RETURN sends the answer back to wherever MEAN was called.
Worked exampleExample 3: Using a sub-program
Use Code 5 to find the mean of the rink data and the mean of [−4, −6].
- SET m = MEAN(temps) runs the sub-program with data = temps. It returns −49 ÷ 7 = −7.
- MEAN([−4, −6]) returns (−4 + −6) ÷ 2 = −10 ÷ 2 = −5.
−7 and −5. One sub-program can find the mean of any list, so the crew can reuse it every week.
Hands-on activityAnalyse your own week
You need: a pencil, paper, and one week of real data. Ideas: each day's high temperature from a weather app, minutes you spent reading or outside, or the number of steps on a step counter.
- Write your data as a list, like SET minutes = [35, 20, 0, 45, 30, 60, 20].
- Write code to find the total, the mean, the highest value and a count of days that meet a goal you choose (for example, 30 minutes or more).
- Run your code by hand with a tracking table.
- Write a decision rule with IF ... THEN ... ELSE (for example, "IF mean ≥ 30 THEN SAY 'Goal met'") and run it.
- Explain your decision to someone at home using your outputs.
What to look for: total and count start at 0; highest starts at the first item; the mean is total ÷ count; the decision uses the outputs. For the example list: total 210, mean 30, highest 60, days with 30 or more: 4.
Just for funWhy does the FOR EACH loop get invited to every party?
Because it never skips anyone on the list.
Your turn
1Run Code 1 with SET temps = [7, 9, 4, 10, 5]. What does it say?
7. total = 35, count = 5, 35 ÷ 5 = 7.
2Run Code 2 on the same list [7, 9, 4, 10, 5]. What does it say?
10. highest: 7 → 9 → 9 → 10 → 10.
3Write code to count how many values in [7, 9, 4, 10, 5] are 8 or more. What does it say?
Model answer: SET big = 0 / FOR EACH v IN data / IF v ≥ 8 THEN / ADD 1 to big / SAY big. It says 2 (9 and 10).
4Write code to find the lowest value in a list.
Model answer: SET lowest = first item of data / FOR EACH v IN data / IF v < lowest THEN / SET lowest = v / SAY lowest.
5What does this code say? SET total = 0 / FOR EACH x IN [3, −5, 8, −1] / IF x < 0 THEN / ADD x to total / SAY total
−6. Only the negative values are added: −5 + (−1) = −6.
6Run Code 3 if mean = −9 and highest = −2.
"Flood the rink". −9 < −5 is true and −2 ≤ 0 is true.
More practice: warm-up
1What does a FOR EACH loop do?
It runs the indented lines once for every item in the list.
2SET total = 0 / FOR EACH n IN [2, 4, 6] / ADD n to total. What is total at the end?
3If Code 1 runs on [5, 5, 5, 5], what is count at the end?
4What would Code 1 say for [10, 20, 30]?
5What would Code 2 say for [−3, −9, −1]?
−1. It's the greatest (closest to 0).
More practice: on your own
1Run Code 1 on hours of sleep: [9, 8.5, 7.5, 9.5, 10, 8, 10.5].
9. total = 63, count = 7, 63 ÷ 7 = 9.
2Write code to count the days with 5 cm or more of snow in SET snow = [0, 12, 3, 5, 0, 8, 1]. What does it say?
Model answer: SET snowy = 0 / FOR EACH s IN snow / IF s ≥ 5 THEN / ADD 1 to snowy / SAY snowy. It says 3 (12, 5 and 8).
3A student's highest-value code starts with SET highest = 0. It is run on [−8, −3, −12]. What does it say, and how do you fix it?
It says 0, which is wrong (0 isn't even in the list). No value is greater than 0, so highest never changes. Fix: SET highest = first item of the list. Then it says −3.
4Describe code to find the range (highest − lowest) of a list. What is the range of [4, 11, −2, 7]?
Find highest (as in Code 2) and lowest (start at the first item, replace when smaller), then SET range = highest − lowest. For this list: 11 − (−2) = 13.
5A class will choose the fundraiser with the greater mean daily sales. Bake sale: [45, 60, 38, 57]. Popcorn: [52, 41, 49, 50]. Which one, and why?
The bake sale. Bake sale mean: 200 ÷ 4 = $50. Popcorn mean: 192 ÷ 4 = $48. Running MEAN on both lists gives 50 and 48.
Challenge
1Write code that counts how many values in a list are above the mean. Run it on [3, 8, 5, 12, 7].
Model answer: SET m = MEAN(data) / SET above = 0 / FOR EACH v IN data / IF v > m THEN / ADD 1 to above / SAY above. The mean is 35 ÷ 5 = 7, so the code says 2 (8 and 12). It needs two passes through the list: one for the mean and one to compare.
2Write code to find the longest streak of days below 0 in a list. Run it on [2, −1, −4, 3, −2, −5, −7, 1].
Model answer: SET streak = 0 / SET longest = 0 / FOR EACH t IN temps / IF t < 0 THEN / ADD 1 to streak / IF streak > longest THEN SET longest = streak / ELSE / SET streak = 0 / SAY longest. Trace: streak 0, 1, 2, 0, 1, 2, 3, 0. It says 3.
3Code 1 is run on a list with no items: SET temps = []. What goes wrong? How could you fix it?
The loop never runs, so count stays 0, and total ÷ count means dividing by 0, which is impossible. Fix: IF count = 0 THEN SAY "No data" ELSE SET mean = total ÷ count and SAY mean.
Check your understandingQuiz
1. SET total = 0 / FOR EACH n IN [4, −1, 6] / ADD n to total. What is total?
2. To find the mean, the code divides...
3. What should a highest-value code use as its starting value?
4. SET cold = 0 / FOR EACH t IN [−3, 2, −7, 0, −1] / IF t < 0 THEN ADD 1 to cold. What is cold?
5. Why use a FOR EACH loop instead of one line for each item?
6. Code 3 says "Flood the rink" only IF mean < −5 AND highest ≤ 0. If mean = −7 and highest = 1, what does it say?
For parents and teachersBig idea: C3.1 asks Grade 8 students to solve problems and create computational representations by writing and executing code, including code that involves the analysis of data in order to inform and communicate decisions. Here, students store data in lists and write loops to find totals, means, highest values and counts.
Common difficulties:
- Forgetting to start a total or counter at 0, or starting "highest" at 0 instead of the first item.
- Indentation. Lines inside the loop run every time; lines after it run once.
- Losing track while tracing. A tracking table with one row per loop run keeps things organized.
- Dividing by the wrong thing (count by total).
How to help: Do the "Analyse your own week" activity together with real family data. Ask your child to explain the decision using the outputs, as if reporting to the family. No computer is needed. This lesson builds A1 skills: managing frustration while debugging, and communicating.