Working out the purpose from a trace

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

Working out the purpose from a trace

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An algorithm is dry run with the inputs 4, 7 and 2. It outputs 13. What is its most likely purpose?
A Finding the largest number
B Totalling the numbers
2. Test 1: inputs 5, 9, 3 give output 9. Test 2: inputs 12, 6, 8 give output 12. What does the algorithm do?
A Finds the largest input
B Totals the inputs
3. Why should you try two or three different sets of test values in a dry run?
A More than one test helps rule out other possible purposes
B One test always proves the purpose
4. Inputs 12, 3, 15, 8, 20 give the output 3. Which purpose fits best?
A Counting how many inputs are greater than 10
B Totalling the inputs that are greater than 10
5. Which is the better statement of an algorithm's purpose?
A Sets Total to 0 and adds Num to Total in a loop
B Reads 5 numbers and outputs their total

Match the Pairs

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

Left columnRight column
1Inputs 4, 7, 2 give Total = 13 AThe output is the sum of the inputs, so the purpose is totalling
2Inputs 4, 7, 2 give Max = 7 BIF Mark >= 50 THEN Count ← Count + 1
3A counting algorithm adds 1 to a counter only when a condition is true, so the output is how many values passed COne set of test values is not enough as 9 could also be the middle value or the last input checked, so try two or three different inputs
4A search algorithm compares each item with the target and outputs whether it was found DThe output is the biggest input, so the purpose is finding the largest
5Inputs 3, 5, 8 give Total = 16, so it counts the values EThe output 16 is the sum of the inputs, not how many there were (3), so the purpose is totalling, not counting
6One test of 2, 9, 5 gives 9, so it must find the largest FIF Item[Index] = Target THEN Found ← TRUE

Definitions

dry run

Fill in the Blanks

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

1.
Total ← 0 Total ← Total _ Number OUTPUT Total // inputs 4, 6 output 10
2.
IF Mark > 50 THEN Count ← _____ + 1 ENDIF
3.
IF Number > Largest THEN Largest ← ______ ENDIF
4.
IF Names[Index] = Target THEN Found ← ____ ENDIF
5.
FOR Count ← 1 TO 3 Total ← Total + Count ____ Count

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 1 TO 3
C INPUT Total
D Total ← Total + Number
E NEXT Count
F OUTPUT Total
Question 2
A Count ← 0
B OUTPUT Mark
C IF Mark >= 50 THEN
D Count ← Count + 1
E ENDIF
F OUTPUT Count
Question 3
A Largest ← 100
B INPUT Number
C IF Number > Largest THEN
D Largest ← Number
E ENDIF
F OUTPUT Largest
Question 4
A INPUT Target
B FOR I ← 1 TO 5
C IF List[I] > Target THEN
D OUTPUT "Found"
E ENDIF
F NEXT I
Question 5
A Count ← 0
B REPEAT
C INPUT Number
D Count ← Count + Number
E UNTIL Number = -1
F OUTPUT Count

Answer Key

Working out the purpose from a trace
Section A: Quick Questions
Q1
B — Totalling the numbers
Q2
A — Finds the largest input
Q3
A — More than one test helps rule out other possible purposes
Q4
A — Counting how many inputs are greater than 10
Q5
B — Reads 5 numbers and outputs their total
Section B: Match the Pairs
Q1
Inputs 4, 7, 2 give Total = 13 → The output is the sum of the inputs, so the purpose is totalling
Q2
Inputs 4, 7, 2 give Max = 7 → The output is the biggest input, so the purpose is finding the largest
Q3
A counting algorithm adds 1 to a counter only when a condition is true, so the output is how many values passed → IF Mark >= 50 THEN Count ← Count + 1
Q4
A search algorithm compares each item with the target and outputs whether it was found → IF Item[Index] = Target THEN Found ← TRUE
Q5
Inputs 3, 5, 8 give Total = 16, so it counts the values → The output 16 is the sum of the inputs, not how many there were (3), so the purpose is totalling, not counting
Q6
One test of 2, 9, 5 gives 9, so it must find the largest → One set of test values is not enough as 9 could also be the middle value or the last input checked, so try two or three different inputs
Section C: Definitions
Q1
dry run — Running an algorithm by hand with test values, then comparing the inputs with the final outputs to work out its purpose, such as totalling, counting or finding the largest.
Section D: Fill in the Blanks
Q1
Total ← 0 Total ← Total + Number OUTPUT Total // inputs 4, 6 output 10
Q2
IF Mark > 50 THEN Count ← Count + 1 ENDIF
Q3
IF Number > Largest THEN Largest ← Number ENDIF
Q4
IF Names[Index] = Target THEN Found ← TRUE ENDIF
Q5
FOR Count ← 1 TO 3 Total ← Total + Count NEXT Count
Section E: Spot the Error
Q1
C — Each input is stored in Total instead of Number, so the old total is overwritten. With inputs 2, 4, 6 a dry run does not give 12. It should be INPUT Number so the algorithm totals the values.
Q2
B — Mark is never read in, so there is no test value to trace. It should be INPUT Mark. With a mark of 60 the count goes 0 to 1, which shows the algorithm counts passes.
Q3
A — Starting at 100 means no small test value can ever beat it. Input 8 still outputs 100, so the trace does not show a largest-value algorithm. Largest should start at 0.
Q4
C — The test uses > so it reports any value bigger than Target, not a match. A dry run with Target 7 would print Found for the wrong items, so the purpose is not a search. It should be IF List[I] = Target THEN.
Q5
D — This adds the value typed in instead of 1, so it totals the numbers instead of counting them. Inputs 3, 5, -1 would give 7, not 3. It should be Count ← Count + 1.