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 events influenced by a defined count and/or subprogram and other control structures
- control structures:
- mathematical situations:
- creating, enlarging, and shrinking shapes (e.g., fractals)
- performing transformations
- determining the cost of a purchase
- determining the surface area and volume of prisms
- solving optimization problems
- performing probability simulations
- Sub-programs are used to assemble a complex program by writing portions of the code that can be modularized. This helps to create efficient code.
- Sub-programs can be used to run specific sequences of code that are only needed or activated in response to specific inputs from the main program.
- Sub-programs can be reused for multiple programs or can be called upon more than once from one main program. For example, a sub-program to determine the area of a rectangle can be used in a program to optimize area, determine the surface area of a rectangle-based prism, and calculate the volume of a rectangle-based prism.
Note
- Coding can support students in developing a deeper understanding of mathematical concepts.
- 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. (See C2.2 and C2.3.)
- One way to introduce the idea of a sub-program is to use a defined count. A defined count is used to repeat an instruction either for a predefined number of times (e.g., 10 repeats) or until a condition has been met (e.g., Number <= 100).
- Students can curate their code from previous learning and use pieces of it as sub-programs for more complex programs.
- If students program a formula for the circle, they may need to use an approximation of pi (3.14 or $$\frac{22}{7}$$), depending on the programming language they are using.
Have students write a program with subprograms to transform a point on the Cartesian plane. For example, the following is a main program with a sub-program to reflect a point on the x-axis and a sub-program to reflect a point on the y-axis.
ReflectOnX subprogram
subprogram reflectOnX |
plot point at (coordX, (−1) * coordY) |
ReflectOnY subprogram
subprogram reflectOnY |
plot point at ((−1) * coordX, coordY) |
Main program
output “Please enter the x coordinate.” |
coordX = user input |
output “Please enter the y coordinate.” |
coordY = user input |
plot point at (coordX, coordY) |
run reflectOnX subprogram |
run reflectOnY subprogram |
Note:
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.
Have students write a program to create a growing or shrinking pattern using a defined count. The following is an example of a program to create a shrinking pattern that generate the term numbers and values until the term value becomes less than or equal to −100. It also reports the number of positions in the pattern.
termValue = 200 |
termNum = 0 |
repeat until termValue <= −100 |
termValue = termValue − 50 |
termNum = termNum + 1 |
output termNum |
output termValue |
output “There are ”, termNum, “ terms in this shrinking pattern.” |
Note:
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.
C3.2
read and alter existing code, including code that involves events influenced by a defined count and/or subprogram 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 in order 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.
- Using sub-programs makes it easier to debug programs, since each sub-program can be tested individually.
Note
- When students are reading code, they are exercising problem-solving skills related to predicting and estimating.
- By reading code and describing the algorithms, students can begin to communicate their ideas around efficiencies and can begin to compare various “correct” solutions.
- By becoming familiar with pre-existing sub-programs, students can better communicate tools that they might use to solve future, more ambiguous real-life problems.
- 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.
Have students read given code, predict expected outcomes, then execute the code to verify their predictions. For example, provide students with the following code, which was created to determine the effect on the circumference and area of a circle when the radius doubles repeatedly.
findCircum subprogram
findCircum subprogram |
circumCircle = 2 * 3.14 * radius |
findArea subprogram
findArea subprogram |
areaCircle = 3.14 * radius * radius |
Main program
output “Please enter the radius of the circle.” |
radius = user input |
repeat 5 times |
run findCircum subprogram |
run findArea subprogram |
output radius |
output circumCircle |
output areaCircle |
radius = 2 * radius |
Note:
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.
Provide students with code that can be used to compare the graphs of linear growing patterns, similar to the sample below. Ask students to alter the code to compare other linear growing patterns.
plotPattern subprogram
subprogram plotPattern (numTiles, change, totalTerms) |
pick random colour |
termNum = 0 |
repeat totalTerms times |
plot point (termNum, numTiles) |
termNum = termNum + 1 |
numTiles = numTiles + change |
Main program
numTiles = 1 |
change = 3 |
totalTerms = 10 |
run plotPattern subprogram (numTiles, change, totalTerms) |
numTiles = 5 |
change = 2 |
totalTerms = 10 |
run plotPattern subprogram (numTiles, change, totalTerms) |
Note:
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.