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

Coding 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.

−16−12−8−404−8Mon−12Tue−3Wed1Thu−6Fri−14Sat−7Sundaily high (°C)
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.

total = 0count = 0another itemin the list?yest = next itemADD t to totalADD 1 to countrepeat for every itemnomean = total ÷ countSAY mean
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 runttotalcount
1−8−81
2−12−202
3−3−233
41−224
5−6−285
6−14−426
7−7−497
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 example

Example 1: Count values that meet a condition

Run Code 4 on the rink data. What does it say?

  1. cold starts at 0. The loop checks each temperature: −8, −12, −3, 1, −6, −14, −7.
  2. Only −12 and −14 are below −10, so cold goes 0 → 1 → 2.

The code says 2.

Worked example

Example 2: Mean of decimal data

Run Code 1 with SET temps = [−2.5, 1.5, −4, 3].

  1. total: −2.5, then −1, then −5, then −2. count: 4.
  2. mean = −2 ÷ 4 = −0.5.

The code says −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 example

Example 3: Using a sub-program

Use Code 5 to find the mean of the rink data and the mean of [−4, −6].

  1. SET m = MEAN(temps) runs the sub-program with data = temps. It returns −49 ÷ 7 = −7.
  2. 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 activity

Analyse 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.

  1. Write your data as a list, like SET minutes = [35, 20, 0, 45, 30, 60, 20].
  2. 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).
  3. Run your code by hand with a tracking table.
  4. Write a decision rule with IF ... THEN ... ELSE (for example, "IF mean ≥ 30 THEN SAY 'Goal met'") and run it.
  5. 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 fun

Why does the FOR EACH loop get invited to every party?

Because it never skips anyone on the list.

Your turn

1

Run Code 1 with SET temps = [7, 9, 4, 10, 5]. What does it say?

2

Run Code 2 on the same list [7, 9, 4, 10, 5]. What does it say?

3

Write code to count how many values in [7, 9, 4, 10, 5] are 8 or more. What does it say?

4

Write code to find the lowest value in a list.

5

What 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

Run Code 3 if mean = −9 and highest = −2.

More practice: warm-up

1

What does a FOR EACH loop do?

2

SET total = 0 / FOR EACH n IN [2, 4, 6] / ADD n to total. What is total at the end?

3

If Code 1 runs on [5, 5, 5, 5], what is count at the end?

4

What would Code 1 say for [10, 20, 30]?

5

What would Code 2 say for [−3, −9, −1]?

More practice: on your own

1

Run Code 1 on hours of sleep: [9, 8.5, 7.5, 9.5, 10, 8, 10.5].

2

Write 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?

3

A 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?

4

Describe code to find the range (highest − lowest) of a list. What is the range of [4, 11, −2, 7]?

5

A 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?

Challenge

1

Write code that counts how many values in a list are above the mean. Run it on [3, 8, 5, 12, 7].

2

Write code to find the longest streak of days below 0 in a list. Run it on [2, −1, −4, 3, −2, −5, −7, 1].

3

Code 1 is run on a list with no items: SET temps = []. What goes wrong? How could you fix it?

Check your understanding

Quiz

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 teachers

Big 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.