Finding errors in an algorithm

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

Finding errors in an algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program should output the total of 3 numbers but outputs 0 every time. What does this suggest?
A A presentation error, because keywords are in lower case
B A logic error, such as Total not being set to 0 or not being updated
2. Which is a presentation error in pseudocode?
A IF Score > 50 THEN, when the pass mark should be 60
B if Score > 50 then (keywords in lower case)
3. You trace an algorithm with the test value 5. Expected output is 10 but the trace table shows 25. What should you do?
A Find the line where the values first differ from what you expect
B Ignore it, because one test value is enough to prove it works
4. Which statement about test values is correct?
A Test values with known expected outputs help show whether an algorithm is wrong
B Test values are only used to check that keywords are in upper case
5. A loop should run 10 times but FOR Count ← 1 TO 9 is written. What type of error is this?
A A logic error, because the loop bound is wrong
B A presentation error, because the loop is not indented

Match the Pairs

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

Left columnRight column
1Total = Total + Mark AA logic error: Total starts with no value, so the final total will be wrong. Set Total ← 0 before the loop.
2Expected 15, actual output 10 BA presentation error: = is used for assignment instead of ←, so the pseudocode rules are broken
3A logic error gives wrong results, such as a loop that stops one step too early CFOR Count ← 1 TO 4 // should repeat 5 times
4A presentation error breaks the pseudocode rules, such as a missing keyword to close a structure DIF Score > 50 THEN OUTPUT "Pass"
5Total ← Total + Mark (Total never set to 0 first) EA logic error: Count never changes, so the loop never ends. Add Count ← Count + 1 inside the loop.
6WHILE Count <= 5 DO OUTPUT Count ENDWHILE FA sign of a logic error: the actual output does not match the expected output for the test values

Definitions

error in an algorithm

Fill in the Blanks

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

1.
IF Mark >= 50 THEN OUTPUT "Pass" _____
2.
// Test value 50 should output Pass IF Mark __ 50 THEN OUTPUT "Pass"
3.
Total ← _ INPUT Num Total ← Total + Num
4.
// Expected output: 1 2 3 4 5 FOR Count ← 1 TO _ OUTPUT Count
5.
IF Actual __ Expected THEN OUTPUT "Error found" ENDIF

Spot the Error

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

Question 1
A DECLARE Total : INTEGER
B Total = 0
C FOR Count ← 1 TO 5
D Total ← Total + Count
E NEXT Count
F OUTPUT Total
Question 2
A input Number
B IF Number > 0 THEN
C OUTPUT "Positive"
D ENDIF
Question 3
A Total ← 1
B FOR Count ← 1 TO 5
C INPUT Mark
D Total ← Total + Mark
E NEXT Count
F OUTPUT Total / 5
Question 4
A Count ← 0
B WHILE Count < 3 DO
C Count ← Count - 1
D OUTPUT Count
E ENDWHILE
Question 5
A DECLARE M : INTEGER
B INPUT M
C IF M >= 0 OR M <= 100 THEN
D OUTPUT "Valid"
E ENDIF

Answer Key

Finding errors in an algorithm
Section A: Quick Questions
Q1
B — A logic error, such as Total not being set to 0 or not being updated
Q2
B — if Score > 50 then (keywords in lower case)
Q3
A — Find the line where the values first differ from what you expect
Q4
A — Test values with known expected outputs help show whether an algorithm is wrong
Q5
A — A logic error, because the loop bound is wrong
Section B: Match the Pairs
Q1
Total = Total + Mark → A presentation error: = is used for assignment instead of ←, so the pseudocode rules are broken
Q2
Expected 15, actual output 10 → A sign of a logic error: the actual output does not match the expected output for the test values
Q3
A logic error gives wrong results, such as a loop that stops one step too early → FOR Count ← 1 TO 4 // should repeat 5 times
Q4
A presentation error breaks the pseudocode rules, such as a missing keyword to close a structure → IF Score > 50 THEN OUTPUT "Pass"
Q5
Total ← Total + Mark (Total never set to 0 first) → A logic error: Total starts with no value, so the final total will be wrong. Set Total ← 0 before the loop.
Q6
WHILE Count <= 5 DO OUTPUT Count ENDWHILE → A logic error: Count never changes, so the loop never ends. Add Count ← Count + 1 inside the loop.
Section C: Definitions
Q1
error in an algorithm — A mistake that breaks pseudocode rules or gives wrong results. Find it by reading the code, then trace it with test values and compare the actual output with the expected output.
Section D: Fill in the Blanks
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ENDIF
Q2
// Test value 50 should output Pass IF Mark >= 50 THEN OUTPUT "Pass"
Q3
Total ← 0 INPUT Num Total ← Total + Num
Q4
// Expected output: 1 2 3 4 5 FOR Count ← 1 TO 5 OUTPUT Count
Q5
IF Actual <> Expected THEN OUTPUT "Error found" ENDIF
Section E: Spot the Error
Q1
B — Presentation error: assignment must use the ← arrow, not =. In pseudocode = is for comparison. It should be Total ← 0.
Q2
A — Presentation error: pseudocode keywords must be in upper case. It should be INPUT Number.
Q3
A — Logic error: the total must start at 0. Tracing with marks 10, 10, 10, 10, 10 gives 51 instead of the expected 50. It should be Total ← 0.
Q4
C — Logic error: Count goes down so it never reaches 3 and the loop never ends. A trace table shows Count stuck at -1, -2 and so on. It should be Count ← Count + 1.
Q5
C — Logic error: with OR every number passes, so the test value 150 outputs Valid when it should not. A range check needs AND.