2
Predict a longer algorithm
Computing 2: Unit 10, pp. 134–148
GoalPredict the output of an algorithm with three steps by writing down the number after each step.
New words
step · one instruction in an algorithmkeep track · to write down what happens after each stepcheck · to run the algorithm and see if the prediction was right
Explanation
A longer algorithm has more steps. Do one step at a time. Write down the number after each step. The last number is the output. Then run the algorithm and check your prediction. If it is different, look for the step where you made a mistake.
| After step | Number |
|---|---|
| enter the number | 3 |
| add 2 | 5 |
| double | 10 |
| take away 1 | 9 |
Worked examples
input: 5→double→add 3→output: 13
Enter a number, double it, add 3 and show it. With the input 5 the number goes 5, 10, 13. The output is 13.| Order | Input | After step 1 | Output |
|---|---|---|---|
| double, then add 1 | 3 | 6 | 7 |
| add 1, then double | 3 | 4 | 8 |
Predicting is not guessing. We work through every step. If our prediction is wrong, we look for the step where we made the mistake.
Class activity
Prediction race: write an algorithm of three steps on a card, for example “enter a number, add 2, double”. One child predicts the output for the input 4 on paper. Another child uses 4 counters: add 2 more counters, then make a second pile the same size and count all. Compare. Did the prediction match?
Practice
1
The input is 4. The steps are: double, add 1, take away 3. Predict the output.
input: 4→double→add 1→take away 3→output: ?
4 becomes 8, then 9, then 6. The output is 6.
2
The input is 1. The steps are: add 3, take away 2, add 4. What is the output?
1 becomes 4, then 2, then 6. The output is 6.
3
The algorithm is “add 2, then double”. Malika gets 12 for the input 5. Is she right? What did she do?
| After step | Number |
|---|---|
| enter the number | 5 |
| add 2 | ? |
| double | ? |
No. The output is 14, because 5 + 2 = 7 and double 7 is 14. Malika doubled first (10) and then added 2 (12).
4
True or false: if the input changes, the output stays the same.
False. For “add 2”, the input 3 gives 5 and the input 4 gives 6.