## C3. Coding

### Specific Expectations

#### Coding Skills

C3.1

solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code that involves conditional statements and other control structures

**mathematical situations:**- creating enlarging and shrinking shapes
- creating repeating, growing, and shrinking patterns
- identifying whether a growing pattern is linear
- grouping numbers using the divisibility rules
- determining the cost of a purchase
- simulating probability situations
- solving optimization problems

**code using conditional statements to convert between centimetres and metres:**

set metres = 0.00 |

set centimetres = 0.00 |

set convertMetres = 0.00 |

set convertCentimetres = 0.00 |

keyPressed = “blank” |

repeat until keyPressed = “A” or “B” |

output “Type A to convert from metres (m) to centimetres (cm). Type B to convert from cm to m.” |

store user input as keyPressed |

if keyPressed == “A” |

output “What is your measurement in metres?” |

store user input as metres |

convertCentimetres = metres * 100 |

output metres, “ metres is ”, convertCentimetres, “ centimetres.” |

else |

output “What is your measurement in centimetres?” |

store user input as centimetres |

convertMetres = centimetres * 0.01 |

output centimetres, “ centimetres is ”, convertMetres, “ metres.” |

*Note:*

- Assignment statements assign a value to a variable and use a single equal sign (=), while comparison statements are used to compare two values and use a double equal sign (==) for equal to, < for less than, > for greater than, <= for less than or equal to, and >= for greater than or equal to.
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.
**other control structures:**

- A flow chart can be used to plan and organize thinking. The symbols used in flow charts have specific meanings, including those that represent a process, a decision, and program input/output.
- Efficient code may involve using the instructions to solve a problem, using the smallest amount of space to store program data, and/or executing as fast as possible.
- Using loops whenever possible is one way to make code more efficient.
- Conditional statements are a representation of binary logic (yes or no, true or false, 1 or 0).
- A conditional statement evaluates a Boolean condition, something that can either be true or false.
- Conditional statements are usually implemented as “if…then” statements, or “if…then…else” statements. If a conditional statement is true, then there is an interruption in the current flow of the program being executed and a new direction is taken or the program will end.
- Conditional statements, like loops, can be nested to allow for a range of possible outcomes or to implement decision trees.

*Note*

- Coding can support students in developing a deeper understanding of mathematical concepts.
- The reverse flow chart that is used to solve equations is not the same as the flow chart used in coding.
- More efficient code can reduce execution time and reduce computer storage space.
- Coding can be used to learn how to automate simple processes and enhance mathematical thinking. For example, students can code expressions to recall previously stored information (defined variables), then input values (e.g., from a sensor, count, or user input) and redefine the value of the variable. For examples of these, refer to the notes in C2.2 and C2.3.
- The construction of the code should become increasingly complex and align with other developmentally and grade-appropriate learning.

Conditional statements in code are used to compare items and can be used to sort data into categories for probability simulations. Have students first adapt the following flow chart for a simulation so that the coins are flipped multiple times. Next, have them write and execute the program. Afterwards, have them compare their flow chart with the program and make connections between the two.

Have students write code using conditional statements to convert between metric units:

set metres = 0.00 |

set centimetres = 0.00 |

set convertMetres = 0.00 |

set convertCentimetres = 0.00 |

keyPressed = “blank” |

repeat until keyPressed = “A” or “B” |

output “Type A to convert from metres (m) to centimetres (cm). Type B to convert from cm to m.” |

store user input as keyPressed |

if keyPressed == “A” |

output “What is your measurement in metres?” |

store user input as metres |

convertCentimetres = metres * 100 |

output metres, " metres is ", convertCentimetres, " centimetres." |

else |

output “What is your measurement in centimetres?” |

store user input as centimetres |

convertMetres = centimetres * 0.01 |

output centimetres, " centimetres is ", convertMetres, " metres." |

C3.2

read and alter existing code, including code that involves conditional statements and other control structures, and describe how changes to the code affect the outcomes and the efficiency of the code

**situations that involve altering code:**- enhancing mathematical learning
- simplifying code
- reinforcing that there is more than one way to generate a given outcome
- debugging code to get the desired outcome
- remixing (altering) a program and using it for another purpose
- remixing (altering) a program in order to add additional components

- Reading code is done to make predictions as to what the expected outcome will be. Based on that prediction, one can determine if the code needs to be altered prior to its execution.
- Reading code helps with troubleshooting why a program is not able to execute.
- Code is altered so that an expected outcome can be achieved.
- Code can be altered to be used for a new situation.
- Altering code to make it more efficient often involves refining algorithms so that there are no unnecessary steps and using control structures effectively.
- Loops can be used to create efficient code.

*Note*

- When students are reading code, they are exercising problem-solving skills related to predicting and estimating.
- When code is altered with the aim of reaching an expected outcome, students get instant feedback when it is executed. Students exercise problem-solving strategies to further alter the program if they did not get the expected outcome. If the outcome is as expected, but it gives the wrong answer mathematically, students will need to alter their thinking.
- Efficient code can be altered more easily than inefficient code to adapt to new mathematical situations. For example, in a probability simulation, the number of trials can be increased by changing the number of repeats rather than writing additional lines of code for each of the new trials.

Provide students with code that needs to be altered and/or expanded upon. For example, the following pseudocode can be used to simulate flipping a coin twice, storing the occurrences, and calculating the percentage for each result. Have students alter this code to simulate a greater number of flips while keeping the code efficient.

heads = 0 |

tails = 0 |

headsPercent = 0 |

tailsPercent = 0 |

coin1 = random number 0 and 1 |

if coin1 == 0 then |

heads = heads + 1 |

else |

tails = tails + 1 |

headsPercent = (heads / 2) * 100 |

tailsPercent = (tails / 2) * 100 |

output “Number of heads: ”, heads |

output “% heads: ”, headsPercent |

output “Number of tails: ”, tails |

output “% tails: ”, tailsPercent |

*Note:*

- Assignment statements assign a value to a variable and use a single equal sign (=), while comparison statements are used to compare two values and use a double equal sign (==) for equal to, < for less than, > for greater than, <= for less than or equal to, and >= for greater than or equal to.
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.

After the code for the probability simulation in Sample Task 1 has been altered to be more efficient, have students alter it again to compare the percentages of the coin flips for increased numbers of trials, such as 50 times or 3000 times.

Provide students with efficient code for a probability simulation for flipping a coin 10 times, and have them keep track of the theoretical and experimental probabilities. Ask them to alter the code so that the program flips the coin 100, 1000, and 10 000 times. Ask them to alter the code so that the program simulates the rolling of one die 10 times. Ask them to alter the code again to simulate the rolling of two dice.

**initial program to flip one coin, 10 times:**

Provide students with code to simulate rolling one die, 10 times. Ask them to alter the code to simulate rolling two dice:

**program to simulate the rolling of one die:**

**altered program to simulate the rolling of two dice:**