# 2.b.4. modeling with code

#### MATERIALS

• Use the Computer Coding handout (PDF) to record your answers to question 3, or use your notebook.
• Use the Computer Coding Practice handout (PDF) to record your answers to question 4, or use your notebook.

[Note: The coding apps below are quick first drafts. We are in the process of redesigning them.]

#### 1. BUILDING GROWING PATTERNS WITH CODE

Complete the following in your notebook, or use the Coding Practice - Growing Patterns handout.

a. Let's take a close look at the pattern below. Notice that two things are happening as the pattern grows from stage 1 to stage 4.

• the blue blocks increase by 3 - we have 3, 6, 9, 16
• the red blocks stay the same - we have 3, 3, 3, 3

Here is how we can create this pattern with computer code. Use the yellow buttons to change the code and see the effect.

[html5 version]

[Flash version]

b. Explore the Growing Patterns app below, which builds bar graphs of growing patterns you have explored. Predict the outcome when the yellow buttons are clicked.

Plan how you to change the code to have the following effect.

• make the bar graph steeper
• make the bar graph less steep
• make the bar graph taller
• make the bar graph shorter
• make all bars the same height

Use the app to test your ideas and then answer the following.

• What colour makes the graph steeper of less steep?
• Is it the blue or the red blocks? Or is it both?

a. The app below show how computer code can be used to print the word "math" a number of times. Click on the yellow buttons to explore.

This code uses what is called a For/Next loop.

for x = 1 to 5

print "math"

next x

For/Next loops are used in computer coding for repetitive tasks.

b. Let's compare the two apps below. Click on the yellow button to explore.

Notice that the first app prints the same number over and over, while the second app prints a growing pattern of numbers. Can you see what part of the computer code makes the difference?

c. Explore For/Next loops in the app below. (Note: * means multiply)

#### 3. CODING IN BASIC

Write your own computer code, using the BASIC computer language: • the screen on the left is for the computer code
• the screen on the right is for the output

Add three more lines to the code screen, as shown below.

PRINT "Hello World"
for x = 1 to 10

.....print x
next x

Click on Execute Script to see the output. It should look like this: Change line 4 to read as follows. The symbol "*" means "multiply".

print 2*x

Predict what the output will be. Click on Execute Script to check your answer. It should be a list of the first 10 even numbers and it should look like this: Try different options for the expression in line 4, such as:

• 3*x
• 2*x-1
• x*x
• x/2
• x-1

#### 4. PRACTICE

Complete the following in your notebook, or use the Coding Practice - Growing Patterns handout. Use the BASIC editor to check your answers.

a. What will the following programming code print?

for x = 1 to 8

print "hello"

next x

b. What will the following programming code print?

for counter = 1 to 5

print counter

next counter

c. What will the following programming code print?

for p = 1 to 10

print p + 1

next p

d. What will the following programming code print?

for x = 10 to 20

print x

next x

e. What will the following programming code print?

for d = 1 to 10

print d, 2*d

next d

f. Write the programming code that would do each of the following:

1. print the phrase "I love math" 20 times
2. print the numbers 1 to 100
3. print the numbers 5 to 15
4. print the even numbers 4 to 20