Tracing a counting algorithm

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

Tracing a counting algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace table row shows Mark = 40 and the condition Mark >= 50 is FALSE. What happens to Count on that row?
A Count stays the same
B Count goes up by 1
2. On a row where the condition is TRUE, Count was 2 on the row before. What is Count on this row?
A 2
B 3
3. Count ← 0. The marks 60, 45, 70 are checked with the condition Mark >= 50. What does OUTPUT Count show at the end?
A 2
B 3
4. Count is 1 on the previous row. Which trace table row is correct for Mark = 45 with the condition Mark >= 50?
A Mark 45 | Condition FALSE | Count 2
B Mark 45 | Condition FALSE | Count 1
5. The OUTPUT Count statement comes after the loop has ended. Which statement is correct?
A It shows the last value of Count in the trace table
B It shows the number of rows in the trace table

Match the Pairs

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

Left columnRight column
1Count ← Count + 1 AIF Mark >= 50 THEN Count ← Count + 1 ENDIF
2OUTPUT Count BCount ← 0
3When the condition is FALSE, the counter stays the same, so the Count column repeats the previous value CThe OUTPUT must show the final value of Count from the last row of the trace table, which is 3
4The counter must start at 0 so the first row of the trace table shows Count as 0 before any items are checked DShows the final value of Count after the loop has finished, not the value on any one row
5Mark = 40 → condition (Mark >= 50) = TRUE, Count goes from 2 to 3 E40 is not greater than or equal to 50, so the condition is FALSE and Count should stay at 2
6Final row of trace table: Count = 3, but OUTPUT Count shows 2 FIn the trace table, Count goes up by 1 only on rows where the condition is TRUE

Definitions

Count ← Count + 1

Fill in the Blanks

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

1.
IF Mark >= 50 THEN Count ← _____ + 1 ENDIF
2.
IF Mark >= 50 THEN Count ← Count _ 1 ENDIF
3.
IF Mark __ 50 THEN Count ← Count + 1 ENDIF
4.
NEXT Index ______ Count
5.
Count ← _ FOR Index ← 1 TO 5

Spot the Error

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

Question 1
A Pass ← 0
B FOR i ← 1 TO 5
C IF Mark[i] >= 50
D Pass ← Pass + 1
E ENDIF
F NEXT i
Question 2
A Count ← 0
B FOR i = 1 TO 6
C IF Score[i] > 10 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 3
A Count ← 0
B FOR i ← 1 TO 10
C IF Colour[i] ← "Red" THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 4
A Count ← 1
B FOR i ← 1 TO 5
C Count ← Count + 1
D NEXT i
E OUTPUT Count
Question 5
A Count ← 0
B FOR i ← 1 TO 5
C IF Mark[i] >= 50 THEN
D Count ← Count + Mark[i]
E ENDIF
F NEXT i

Answer Key

Tracing a counting algorithm
Section A: Quick Questions
Q1
A — Count stays the same
Q2
B — 3
Q3
A — 2
Q4
B — Mark 45 | Condition FALSE | Count 1
Q5
A — It shows the last value of Count in the trace table
Section B: Match the Pairs
Q1
Count ← Count + 1 → In the trace table, Count goes up by 1 only on rows where the condition is TRUE
Q2
OUTPUT Count → Shows the final value of Count after the loop has finished, not the value on any one row
Q3
When the condition is FALSE, the counter stays the same, so the Count column repeats the previous value → IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q4
The counter must start at 0 so the first row of the trace table shows Count as 0 before any items are checked → Count ← 0
Q5
Mark = 40 → condition (Mark >= 50) = TRUE, Count goes from 2 to 3 → 40 is not greater than or equal to 50, so the condition is FALSE and Count should stay at 2
Q6
Final row of trace table: Count = 3, but OUTPUT Count shows 2 → The OUTPUT must show the final value of Count from the last row of the trace table, which is 3
Section C: Definitions
Q1
Count ← Count + 1 — In a trace table, Count goes up by 1 only on rows where the condition is TRUE. If it is FALSE, Count stays the same. The final value of Count is what the OUTPUT shows.
Section D: Fill in the Blanks
Q1
IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q2
IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q3
IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q4
NEXT Index OUTPUT Count
Q5
Count ← 0 FOR Index ← 1 TO 5
Section E: Spot the Error
Q1
C — The IF line has no THEN. The condition is tested, and Pass only goes up when it is TRUE, so it must be written IF Mark[i] >= 50 THEN.
Q2
B — A FOR loop sets its start value with the assignment arrow, not =. It should be FOR i ← 1 TO 6.
Q3
C — The condition column in a trace table needs a comparison that is TRUE or FALSE. ← assigns a value instead. It should be IF Colour[i] = "Red" THEN.
Q4
A — Count must start at 0 before any items are counted. Starting at 1 makes every trace row and the final OUTPUT one too high. It should be Count ← 0.
Q5
D — This adds the mark to Count, which is totalling. Counting adds 1 each time the condition is TRUE. It should be Count ← Count + 1.