Coding in the Classroom | Coding and Data (Students)

Calculating the mean of values stored in a list
By: Steven Floyd and Lisa Anne Floyd

-STUDENT PAGE-

Welcome!

In this activity, you are going to use the Scratch programming environment to create a program that will calculate the mean of ten heart rate values.

The mean is a type of average that represents a sample of values. It’s considered to be a measure of central tendency, along with median and mode.

The mean represents the value that each piece of data would have if the data were evenly distributed (Ministry of Education, 2020).

We can calculate the mean by adding up all of the values in a set and then dividing that result by the number of values in the set.

For example – If your data set has the following numbers:

10, 20, 60

Determine the sum:

10 + 20 + 60 = 90

Divide the sum by the number of values:

90 ÷ 3 = 30

So, the mean of the data set would be 30.


After you write the code for your program by following the instructions below, you will then be asked to alter the code in the program, so that your program can:

  • calculate the mean of 15 and 20 heart rate values
  • calculate the mean of values that are related to something that peaks your interest (maybe sports data, art data, or data related to recent temperatures)
  • obtain the values from the user of the program

The activity student is broken up into two sections:

A – Create the program by following steps in a video or step-by-step guide
B – Alter the program and complete some extension activities to apply your new knowledge and skills.

A – Creating the Scratch program

First, you can watch this video, which takes you through the steps of creating the program OR take a look at this step-by-step guide that takes you through the development process with explanations and screen prints.

As you watch the video, you may find it helpful to pause or to go back a few seconds, to make sure that you are capturing all of the instructions. This is a good strategy when learning from an instructional video.

B – Try the Extension Activities

You have written a program to calculate the mean heart rate based on ten heart rate readings.

But wait… there’s more!

Below are some extension activities to try, which are also introduced in the step-by-step guide. Some tips and hints are provided.

  1. Alter the code in this program to calculate the mean of 15 heart rate readings.

Hints:

  • You will have to add five more heart values to the list by clicking the “+” symbol and entering in five more values.
  • You will have to change the loop, so that it doesn’t loop 10 times, but instead loops 15 times.

2. Alter the code in this program to calculate the mean of 20 heart rate readings.

3. Start a new project and create a program that calculates the mean of some other data. You could use data from sports (like the points or goals per game), or from art (the price of recent paintings that have sold), or the weather (the daily high temperature from the last few days).

4. Start a new project and get the code below to work. With this code, the user types in the ten values for the heart rate, instead of the programmer setting them up at the start, like we did in the previous programs. This program will still require a list, and the same three variables:

5. Alter the code in your project so that instead of always typing in 10 heart rate readings, the user of the program can first enter in how many heart rate readings they are going to enter. The code below will help you. Notice now that we have a new variable called numberOfReadings and we ask the user for this first value right away, at the start of the program. This value then becomes the counter for the two loops (repeat blocks). COOL!

Good luck, and don’t worry if it takes you a little bit of time to write your program. It’s important to follow the instructions closely, so take your time. Also, don’t worry if your program doesn’t work the first time, – you will probably have to debug the code, just like professional programmers always have to do as well.