Predicting the outcome of an algorithm

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Predicting the outcome of an algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Before you follow an algorithm, what do you need to choose first?
A The final answer it will show
B Starting values, like score = 3
2. Which is a good way to predict what an algorithm will show?
A Guess from the first step only
B Follow each step in order and write down how the values change
3. Algorithm: SET total to 2. ADD 5 to total. ADD 1 to total. SHOW total. What does it show?
A 8
B 5
4. What is a tracking table used for?
A Recording how each variable changes after every step
B Making the algorithm run faster
5. Algorithm: IF age >= 10 THEN SHOW "Ticket" ELSE SHOW "Free". If age is 8, what does it show?
A Free
B Ticket

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1trace table AI predict: total will be 10. Now run it and compare.
2start with score = 3 BThe table must show how the value changes after every step, not just the final value.
3Predict first, then check: work out the answer on paper, then run the algorithm to see if you were right CA table where you write down the value of each variable after every step, so you can see how the values change
4Try different inputs: test the same algorithm with another starting value because the outcome might change DThe prediction was guessed instead of traced. Following each repeat gives 2, 4, 6, so the output is 6.
5count = 0 REPEAT 3 TIMES count = count + 2 Output: count Prediction: 3 (guessed without tracing) ETest 1: age = 8 Test 2: age = 14
6Trace table: only wrote the final value of score, skipped the steps in between FChoose a starting value to test, then follow each step from there to see what the algorithm shows at the end

Definitions

trace

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
SET total TO 4 ADD 3 TO total SHOW total # it shows _
2.
SET count TO 0 REPEAT 3 TIMES: ADD 2 TO count SHOW count # it shows _
3.
SET temp TO 20 IF temp > 25 THEN SHOW "Hot" ELSE SHOW "Cool" # 20 > 25 is _____
4.
SET n TO 10 REPEAT UNTIL n < 5: SUBTRACT 3 FROM n SHOW n # 10, 7, 4 so it shows _
5.
SET a TO 3 SET b TO a * 2 SHOW _ # table: a = 3, b = 6

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A INPUT start
B SET total ← start
C SET total = total + 2
D OUTPUT total
Question 2
A SET lives ← 3
B REPEAT 3
C SET lives ← lives - 1
D OUTPUT lives
Question 3
A INPUT age
B SET ticket ← 5
C IF age < 10
D SET ticket ← 2
E END IF
F OUTPUT ticket
Question 4
A SET n ← 4
B SET n * 2 ← n
C SET n ← n + 1
D OUTPUT n
Question 5
A SET x ← 10
B REPEAT UNTIL x ← 4
C SET x ← x - 3
D OUTPUT x

Answer Key

Predicting the outcome of an algorithm
Section A: Quick Questions
Q1
B — Starting values, like score = 3
Q2
B — Follow each step in order and write down how the values change
Q3
A — 8
Q4
A — Recording how each variable changes after every step
Q5
A — Free
Section B: Match the Pairs
Q1
trace table → A table where you write down the value of each variable after every step, so you can see how the values change
Q2
start with score = 3 → Choose a starting value to test, then follow each step from there to see what the algorithm shows at the end
Q3
Predict first, then check: work out the answer on paper, then run the algorithm to see if you were right → I predict: total will be 10. Now run it and compare.
Q4
Try different inputs: test the same algorithm with another starting value because the outcome might change → Test 1: age = 8 Test 2: age = 14
Q5
count = 0 REPEAT 3 TIMES count = count + 2 Output: count Prediction: 3 (guessed without tracing) → The prediction was guessed instead of traced. Following each repeat gives 2, 4, 6, so the output is 6.
Q6
Trace table: only wrote the final value of score, skipped the steps in between → The table must show how the value changes after every step, not just the final value.
Section C: Definitions
Q1
trace — To trace an algorithm, pick starting values, follow each step in order, write down how the values change, and say what it shows at the end.
Section D: Fill in the Blanks
Q1
SET total TO 4 ADD 3 TO total SHOW total # it shows 7
Q2
SET count TO 0 REPEAT 3 TIMES: ADD 2 TO count SHOW count # it shows 6
Q3
SET temp TO 20 IF temp > 25 THEN SHOW "Hot" ELSE SHOW "Cool" # 20 > 25 is false
Q4
SET n TO 10 REPEAT UNTIL n < 5: SUBTRACT 3 FROM n SHOW n # 10, 7, 4 so it shows 4
Q5
SET a TO 3 SET b TO a * 2 SHOW b # table: a = 3, b = 6
Section E: Spot the Error
Q1
C — = asks if two things are equal, so the total would never change when you trace it. To store a new value, use the assignment arrow: SET total ← total + 2.
Q2
B — The loop does not say what to repeat, so you cannot trace how many times lives goes down. It should be REPEAT 3 TIMES.
Q3
C — The IF line is missing THEN, so you cannot follow which step runs when the condition is true. It should be IF age < 10 THEN.
Q4
B — The arrow must point at the variable being changed, with the new value on the right. It should be SET n ← n * 2, so tracing gives 8 and then 9.
Q5
B — A REPEAT UNTIL needs a condition that is true or false, not an assignment. It should be REPEAT UNTIL x <= 4. Tracing then gives x = 10, 7, 4 and an output of 4.