Spotting common mistakes in standard algorithms

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

Spotting common mistakes in standard algorithms

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is correct for a totalling algorithm?
A Total ← 0 is written BEFORE the loop
B Total ← 0 is written INSIDE the loop
2. An array Marks has 10 elements, numbered 1 to 10. Which loop header is correct?
A FOR i ← 1 TO 11
B FOR i ← 1 TO 10
3. Which comparison correctly finds the MINIMUM value in a loop?
A IF Num[i] < Min THEN
B IF Num[i] > Min THEN
4. A student sets Max ← 0 before looping through temperatures that are all below zero. What happens?
A Max stays 0, which is wrong because no temperature is larger than 0
B Max correctly ends up as the highest temperature
5. Where should the average be calculated?
A Average ← Total / Count inside the loop, on every repeat
B Average ← Total / Count after the loop has finished

Match the Pairs

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

Left columnRight column
1Total ← 0 AWrong comparison: > is used when finding the minimum. It should be < so Min is replaced only by a smaller value.
2FOR Index ← 1 TO 10 (array has 10 items) BSets the running total to zero before the loop, so old or unknown values are not added to the answer
3To find the maximum, start Max with the first array item, not 0, so negative numbers or low values still work CThe Found flag is never set to FALSE before the loop and the position is not stored, so the result may be wrong or the location is lost.
4To calculate an average, divide once after the loop has finished, not inside the loop DThe loop bounds match the array size, so every item is visited and the loop never runs past the end
5IF Numbers[Index] > Min THEN Min ← Numbers[Index] ENDIF EMax ← Numbers[1]
6IF Names[Index] = Target THEN Found ← TRUE ENDIF (Index never updated, Found never set to FALSE first) FNEXT Index Average ← Total / Count

Definitions

logic error

Fill in the Blanks

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

1.
Total ← _ FOR Index ← 1 TO 5 Total ← Total + Mark[Index]
2.
DECLARE Mark : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO _ Total ← Total + Mark[Index]
3.
Min ← Score[1] FOR i ← 2 TO 5 IF Score[i] _ Min THEN Min ← Score[i]
4.
Total ← Total + Score[i] NEXT i Average ← Total _ 5
5.
IF Name[i] = Target THEN _____ ← TRUE ENDIF

Spot the Error

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

Question 1
A Total ← 1
B FOR i ← 1 TO 5
C Total ← Total + Price[i]
D NEXT i
E OUTPUT Total
Question 2
A Count ← 0
B FOR i ← 1 TO 8
C IF Temp[i] > 30 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 3
A Found ← FALSE
B FOR i ← 1 TO 5
C IF Names[i] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT i
Question 4
A Total ← 0
B Count ← 1
C FOR i ← 1 TO 5
D Total ← Total + Marks[i]
E Count ← Count + 1
F NEXT i
Question 5
A Count ← 0
B FOR i ← 1 TO 5
C IF Marks[i] >= 50 THEN
D Count ← Count + Marks[i]
E ENDIF
F NEXT i

Answer Key

Spotting common mistakes in standard algorithms
Section A: Quick Questions
Q1
A — Total ← 0 is written BEFORE the loop
Q2
B — FOR i ← 1 TO 10
Q3
A — IF Num[i] < Min THEN
Q4
A — Max stays 0, which is wrong because no temperature is larger than 0
Q5
B — Average ← Total / Count after the loop has finished
Section B: Match the Pairs
Q1
Total ← 0 → Sets the running total to zero before the loop, so old or unknown values are not added to the answer
Q2
FOR Index ← 1 TO 10 (array has 10 items) → The loop bounds match the array size, so every item is visited and the loop never runs past the end
Q3
To find the maximum, start Max with the first array item, not 0, so negative numbers or low values still work → Max ← Numbers[1]
Q4
To calculate an average, divide once after the loop has finished, not inside the loop → NEXT Index Average ← Total / Count
Q5
IF Numbers[Index] > Min THEN Min ← Numbers[Index] ENDIF → Wrong comparison: > is used when finding the minimum. It should be < so Min is replaced only by a smaller value.
Q6
IF Names[Index] = Target THEN Found ← TRUE ENDIF (Index never updated, Found never set to FALSE first) → The Found flag is never set to FALSE before the loop and the position is not stored, so the result may be wrong or the location is lost.
Section C: Definitions
Q1
logic error — A mistake in an algorithm's steps that lets it run but gives wrong results, e.g. a total not set to 0, a loop going past the array end, or using > instead of <.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Mark[Index]
Q2
DECLARE Mark : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 5 Total ← Total + Mark[Index]
Q3
Min ← Score[1] FOR i ← 2 TO 5 IF Score[i] < Min THEN Min ← Score[i]
Q4
Total ← Total + Score[i] NEXT i Average ← Total / 5
Q5
IF Name[i] = Target THEN Found ← TRUE ENDIF
Section E: Spot the Error
Q1
A — Total must start at 0. Starting at 1 adds an extra 1 to the answer every time. It should be Total ← 0.
Q2
B — Temp holds 7 values (one per day), so the loop must stop at 7. Going to 8 reads past the end of the array. It should be FOR i ← 1 TO 7.
Q3
D — When the target is found, the flag must be updated to TRUE. Setting it to FALSE again means the search never reports a match. It should be Found ← TRUE.
Q4
B — The counter must start at 0. Starting at 1 makes the count one too high, so any average using it will be wrong. It should be Count ← 0.
Q5
D — Counting adds 1 each time the condition is true. Adding the mark turns the count into a total. It should be Count ← Count + 1.