C3.1 Solve problems and create computational representations of mathematical situations by writing and executing code, including code that involves sequential, concurrent, repeating, and nested events.

Skill: Computational Problem Solving


Coding can be used to automate tasks and visualize mathematics to facilitate problem solving. By its very nature, coding lends itself well to trial-and-error learning, giving students the opportunity to solve problems by learning from their mistakes. Therefore, the student can use the questioning "What will happen if…?"

The student, for example, might ask the question, "What will happen if I have to represent a sequence with a repeating and growing pattern?" In order to solve this problem, the student will need to use one loop to represent the repeating pattern of the sequence and a second loop to represent the growing element.

Example

Code (Scratch)

Result

Image Blocks of code: Events block stating “start on when green flag is clicked”. Looks block stating “show”. Pen extension stating “erase all”. Motions blocks stating “go to x:’-200’ y: ‘-160’.” Controls blocking “repeat 5”. Inside 3 nested blocks. Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Motions blocks stating “go to x:’-200’ y: ‘-160’. Motions blocks stating “change x by ‘40’.” Controls blocking “repeat 2”. Inside 3 nested blocks. Controls blocking “repeat 5”. Inside 3 nested blocks. Controls blocking “repeat 1”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Motions blocks stating “go to x:’-160’ y: ‘-160’. Motions blocks stating “change x by ‘20’.” Looks block stating “hide”.

Source : Scratch Jr.

Spirte in the shape of diamonds. The diamonds are placed on 3 columns of 5 diamonds.

In this example, there is a loop nested within another loop. The outer loop shows the increasing element (two repeats at the second term in the pattern), while the inner loop represents the repeating pattern, that is, the five diamonds one over the other.

Here is the code enlarged to better see the loops:

Image Blocks of code: Controls blocking “repeat 5”. Inside 3 nested blocks. Controls blocking “repeat 1”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Motions blocks stating “go to x:’-160’ y: ‘-160’. Motions blocks stating “change x by ‘20’.” Looks block stating “hide”.

Skill: Representing Mathematical Situations Computationally


Coding can be used as a representational tool in the same way as manipulatives. By using written blocks or commands, very complex mathematical situations can be modeled and manipulated visually, which can help make very abstract concepts concrete.

The student, for example, could code the properties of geometric shapes to represent them on the screen. The use of loops makes it easier to represent dimensions that will be repeated, and a second outer loop can create geometric patterns that consider math and art. Here is an example of such code for a pattern that represents the properties of a rectangle:

Image Blocks of code: Events block stating “start on when green flag is clicked”. Pen extension stating “erase all”. Pen extrension stating “pen down”. Motions block stating “go to x: ‘0’ y” ‘0’”. Control block stating “repeat 10”. Inside 2 nested blocks. Control blocks stating “repeat 2”. Inside 4 nested blocks. Motions block stating ‘move 100’ steps”. Motions block stating “turn left ‘90’ degrees”. Motions block stating ‘move 150’ steps”. Motions block stating “turn left ‘90’ degrees”. Motions block stating “turn right ‘15’ degrees”. Pen extension stating “pen up”.
Image This is a series of ten rectangles. The starting point of each rectangle is the same. The lower left corner of each rectangle is joined to the center point. This formation gives the appearance of a flower.

Skill: Writing Code


Writing or editing a code consists of putting instructions in a specific order, following the syntax of a programming language. Writing code can be similar to writing text. Pseudocode is writing the instructions for a code in the familiar language. Block coding can make it easier to follow the syntax by using blocks of different shapes and colours.

The pseudocode below, for example, describes a sprite that will give the answer to a mathematical problem, namely the calculation of the area and perimeter of a rectangle.

Sprite

Variables

[base] = base of the rectangle

[height] = height of the rectangle

[area] = area of the rectangle

[perimeter] = perimeter of the rectangle

Starting condition = true

Ask - "What is the base of the rectangle?"

Value of [base] = response

Ask - "What is the height of the rectangle?"

Value of [height] = response

Value of [area] = [base] * [height]

Value of [perimeter] = [base]+[height]+[base]+[height]

Say - "The area of the rectangle is [area]. "

Say - "The perimeter of the rectangle is [perimeter]. "

End

This pseudocode can act as the planning or prewriting step. The resulting code might look like this:

Image Blocks of code: Sensing block stating “ask ‘what is the base of the rectangle?’ and wait”. Variables block stating “set BASE to” Motion block stating “answer”.” Sensing block stating “ask ‘what is the height of the rectangle?’ and wait”. Variables block stating “set HEIGHT to” Motion block stating “answer”.” Variables block stating “set AREA, multiplied, HEIGHT”. Variables block stating “set perimeter to BASE, addition, HEIGHT, addition, BASE, addition, HEIGHT.” Looks block “say “join the area of the rectangle is” variable block stating “AREA”. Looks block stating “say join the perimeter of the rectangle is perimeter.”

Skill: Executing Code


Execution of code is the step where the code is read and compiled by the computer. It is at this stage that a functional code will give the desired result, or output (and a non-functional code will give a different output or no output). In block coding, the execution of a code is often done by means of a button in the interface, while text-based programming languages require precise compilation software that essentially translates the code from the programming language to the machine language (for example, binary code) so that it can execute the code. This also applies to robotic devices.

Some examples of run buttons for block coding software:

Examples of coding execution button.

Source : Makecode.

Examples of coding execution button.

Source : Scratch Jr.

Examples of coding execution button ‘run’.

Source : Programiz

Knowledge: Nested Events


Nested events are control structures placed inside other control structures (for example, loops placed inside loops).

A "control structure" is any line or block of code that influences the way the code is executed. Control structures affect the flow of the program. They include repeating lines of code or selecting whether to execute specific lines of code. Sequence and repetition (loops) are control structures.

To understand the impact of changing the number of repetitions in each loop of a code with nested loops, the student could experiment with a variety of number combinations and record the results. Then the student could create a code and share it with the class, who would then predict the outcome of the code. For example, the student could use the code below and see the effect of a variety of numbers on the arrangement of apples.

The representation of sprites in an array becomes even more obvious when using nested loops to define the rows and columns of the layout.

Here is an example of code, for an array, that has nested events:

Example of code (Scratch Jr)

Image Blocks of code: Events block stating “start on whengreen flag is clicked.” Motions block stating “go to x: -190 y:-130.” Variable block “set row to ‘0’.” Control block stating “repeat ‘3’.” Inside 6 nesting blocks. Looks blocks “show”. Control block stating “repeat ‘6’.” Inside 2 nested blocks. Control block stating “create clone of myself.” Motions block stating “move 60 steps”. Motions block stating “go to x: -190 y:-130.” Motions block stating “change by ‘60’ variable block stating “row”. Variable block stating “change row by ‘1’.” Looks block stating “hide”.

Result of the execution

Apple sprite. The apples are placed in 3 rows of 6 apples.

The code now represents a multiplication using the rectangular layout by using a loop nested within another loop. The outer loop represents the number of rows, while the inner loop represents the number of apples per row. The multiplication can therefore be written as follows:

(outer loop repeats) × (inner loop repeats) or, in this example, 3 × 6.

Relevant Questions:

  • What is the maximum number of apples that can be displayed on the screen?
  • Is it possible to represent a prime number in a rectangular arrangement?
  • How many different ways can the number 36 be represented?
  • What would happen if a third loop was embedded in the code? Where would you place it?

Knowledge: Events and Structures (Prior Knowledge)


Sequential Events

A set of instructions executed one after the other, usually from top to bottom or from left to right on a screen.

Example

Image on a balance with number of people on each side. The equation states ‘p’ on the right side, less than or equal to, 9 people on the left time.

Simultaneous Events

Several events that occur at the same time.

Example

Image Blocks of code: Start on green flag, move right 6 times, move up two times, send message. Start on message, move right 4 times. Start on message, move right 4 times.

Recurring Events

Repeating events. In coding activities, loops are used in code to repeat instructions.

Example

Image Blocks of code: Events block stating “start on when green flag clicked.” Control block stating “repeat 4”. Inside 2 nested blocks. Motions block stating “move 60 steps”. Control block stating “create clone of myself”.