Dry-running an algorithm and giving its output

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

Dry-running an algorithm and giving its output

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A dry run of an algorithm is carried out using...
A A trace table and given test data, line by line
B A compiler that runs the program automatically
2. Which row of a trace table is created by the line Total ← Total + 5 when Total was 10 before the line?
A Total stays at 10 and 5 is output
B Total changes to 15
3. Dry run: Count ← 0, then FOR i ← 1 TO 4 loops with Count ← Count + 1. What is the final value of Count?
A 4
B 5
4. Dry run with input 6 and 9: INPUT A, INPUT B, OUTPUT A + B. What is the OUTPUT?
A 69
B 15
5. When you dry run an algorithm, a new row in the trace table should be written...
A Whenever a variable's value changes or something is output
B Only once at the very end of the algorithm

Match the Pairs

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

Left columnRight column
1dry run AThe student did not record each variable's value line by line, so mistakes cannot be found and method marks are lost.
2Total | Count | OUTPUT BOUTPUT Total
3Tracing with given input values: the data 4, 7 and 9 is entered and the total is updated each time round the loop CTotal ← Total + Number
4Stating the final OUTPUT: it is written only once the trace reaches the end of the loop, using the last values in the table DThe column headings of a trace table: one column for each variable and one for each OUTPUT
5Trace table row for Count: 0, 1, 2, 3 when the loop only ran twice EThe trace has an extra row, so the loop was traced one more time than it really runs. Check the loop condition against the data.
6Dry run that only writes the final OUTPUT and skips the trace table rows FFollowing an algorithm line by line with given data, recording each variable's value, to find its output or purpose

Definitions

dry run

Fill in the Blanks

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

1.
X ← 2 X ← X * 3 OUTPUT X // trace shows _
2.
FOR N ← 1 __ 3 OUTPUT N NEXT N
3.
IF Mark < 50 THEN OUTPUT "Fail" _____
4.
Total ← 10 Total ← Total _ 4 OUTPUT Total // trace shows 6
5.
Total ← 3 Total ← Total + 2 ______ Total

Spot the Error

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

Question 1
A Total ← 0
B FOR i ← 1 TO 3
C Total ← Total + i
D ENDFOR i
E OUTPUT Total
Question 2
A N ← 3
B WHILE N > 0 DO
C OUTPUT N
D N ← N + 1
E ENDWHILE
Question 3
A Max ← 4
B Num ← 9
C IF Num < Max THEN
D Max ← Num
E ENDIF
F OUTPUT Max
Question 4
A Nums[1] ← 5
B Nums(2) ← 8
C Total ← Nums[1] + Nums[2]
D OUTPUT Total
Question 5
A Age ← 16
B IF Age >= 16
C OUTPUT "Adult"
D ENDIF

Answer Key

Dry-running an algorithm and giving its output
Section A: Quick Questions
Q1
A — A trace table and given test data, line by line
Q2
B — Total changes to 15
Q3
A — 4
Q4
B — 15
Q5
A — Whenever a variable's value changes or something is output
Section B: Match the Pairs
Q1
dry run → Following an algorithm line by line with given data, recording each variable's value, to find its output or purpose
Q2
Total | Count | OUTPUT → The column headings of a trace table: one column for each variable and one for each OUTPUT
Q3
Tracing with given input values: the data 4, 7 and 9 is entered and the total is updated each time round the loop → Total ← Total + Number
Q4
Stating the final OUTPUT: it is written only once the trace reaches the end of the loop, using the last values in the table → OUTPUT Total
Q5
Trace table row for Count: 0, 1, 2, 3 when the loop only ran twice → The trace has an extra row, so the loop was traced one more time than it really runs. Check the loop condition against the data.
Q6
Dry run that only writes the final OUTPUT and skips the trace table rows → The student did not record each variable's value line by line, so mistakes cannot be found and method marks are lost.
Section C: Definitions
Q1
dry run — Following an algorithm line by line with given data, recording each variable's value in a trace table, to find its output or purpose.
Section D: Fill in the Blanks
Q1
X ← 2 X ← X * 3 OUTPUT X // trace shows 6
Q2
FOR N ← 1 TO 3 OUTPUT N NEXT N
Q3
IF Mark < 50 THEN OUTPUT "Fail" ENDIF
Q4
Total ← 10 Total ← Total - 4 OUTPUT Total // trace shows 6
Q5
Total ← 3 Total ← Total + 2 OUTPUT Total
Section E: Spot the Error
Q1
D — A FOR loop in Cambridge pseudocode is closed with NEXT i, not ENDFOR. Without NEXT i you cannot trace the loop, but with it the dry run gives Total = 1, 3, 6 and OUTPUT 6.
Q2
D — N must go down so the condition N > 0 eventually becomes false. With N + 1 the trace table shows 3, 4, 5... forever. It should be N ← N - 1, giving OUTPUT 3, 2, 1.
Q3
C — The test is the wrong way round. The dry run checks 9 < 4, which is false, so Max stays 4 and the output is wrong. It should be Num > Max, so Max becomes 9.
Q4
B — Array elements use square brackets in Cambridge pseudocode, so it should be Nums[2] ← 8. A dry run then gives Total = 5 + 8 = 13.
Q5
B — The IF line is missing THEN. It should be IF Age >= 16 THEN. When you dry run with Age = 16, the condition is true and the OUTPUT is Adult.