Tracing counting and totalling

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

Tracing counting and totalling

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program starts with Count ← 0 and Total ← 0. What should the first row of the trace table show?
A Count = 0 and Total = 0 (the initialisation row)
B Count = 1 and Total = 1
2. The loop contains IF Mark >= 50 THEN Count ← Count + 1 ENDIF. Mark is 40 on this pass and Count was 2. What does Count show in this row?
A 3 (Count always goes up on every pass)
B 2 (the IF is false, so Count does not change)
3. Total is 10 and Number is 5. The line Total ← Total + Number runs. What is the new value of Total?
A 15
B 5
4. The OUTPUT Count and OUTPUT Total lines come after the loop has ended. When do the values appear in the OUTPUT column?
A Once, after the final pass of the loop
B On every pass of the loop
5. Marks entered are 60, 45 and 72. The loop has IF Mark >= 50 THEN Count ← Count + 1 ENDIF. Count starts at 0. What is the final Count?
A 3
B 2

Match the Pairs

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

Left columnRight column
1Count ← 0 and Total ← 0 AIF Number > 50 THEN Count ← Count + 1 ENDIF
2Total ← Total + Number BThe first row of the trace table: both columns start at 0 before the loop runs
3The counter only changes on a pass when the IF condition is true, so its column stays the same on other rows CThe counter and total must start at 0 in the initialisation row. Starting at 1 gives every later row the wrong value.
4The final Count and Total are written in the OUTPUT column after the loop has finished, not during it DA new Total value is written in the table on every pass of the loop
5Row 1 of the trace table: Count = 1, Total = 1 (before any numbers are read) ENEXT Index OUTPUT Count, Total
6Number = 40, IF Number > 50 is false, but Count goes from 2 to 3 FCount must not change when the IF condition is false. It should stay at 2 on this row.

Definitions

Count ← Count + 1

Fill in the Blanks

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

1.
Count ← 0 Total ← _
2.
IF Mark >= 50 THEN Count ← Count + _ ENDIF
3.
INPUT Mark Total ← Total + ____ OUTPUT Total
4.
IF Mark >= 50 THEN Count ← Count + 1 _____
5.
NEXT Index OUTPUT "Count: ", Count OUTPUT "Total: ", _____

Spot the Error

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

Question 1
A Count ← 0
B FOR Index ← 1 TO 5
C Count ← Count - 1
D NEXT Index
E OUTPUT Count
Question 2
A Total ← 0
B FOR Index ← 1 TO 4
C INPUT Num
D Total ← Num
E NEXT Index
F OUTPUT Total
Question 3
A Index ← 1
B Total ← 1
C WHILE Index <= 3 DO
D Total ← Total + Index
E Index ← Index + 1
F ENDWHILE
Question 4
A Count ← 0
B FOR Index ← 1 TO 6
C IF Index DIV 2 = 0 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 5
A Total ← 0
B FOR Index ← 5 TO 1
C Total ← Total + Index
D NEXT Index
E OUTPUT Total

Answer Key

Tracing counting and totalling
Section A: Quick Questions
Q1
A — Count = 0 and Total = 0 (the initialisation row)
Q2
B — 2 (the IF is false, so Count does not change)
Q3
A — 15
Q4
A — Once, after the final pass of the loop
Q5
B — 2
Section B: Match the Pairs
Q1
Count ← 0 and Total ← 0 → The first row of the trace table: both columns start at 0 before the loop runs
Q2
Total ← Total + Number → A new Total value is written in the table on every pass of the loop
Q3
The counter only changes on a pass when the IF condition is true, so its column stays the same on other rows → IF Number > 50 THEN Count ← Count + 1 ENDIF
Q4
The final Count and Total are written in the OUTPUT column after the loop has finished, not during it → NEXT Index OUTPUT Count, Total
Q5
Row 1 of the trace table: Count = 1, Total = 1 (before any numbers are read) → The counter and total must start at 0 in the initialisation row. Starting at 1 gives every later row the wrong value.
Q6
Number = 40, IF Number > 50 is false, but Count goes from 2 to 3 → Count must not change when the IF condition is false. It should stay at 2 on this row.
Section C: Definitions
Q1
Count ← Count + 1 — Trace table for counting and totalling: Count and Total start at 0 in the first row, change each pass (Count only if the IF is true), and the final values are output after the loop.
Section D: Fill in the Blanks
Q1
Count ← 0 Total ← 0
Q2
IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q3
INPUT Mark Total ← Total + Mark OUTPUT Total
Q4
IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q5
NEXT Index OUTPUT "Count: ", Count OUTPUT "Total: ", Total
Section E: Spot the Error
Q1
C — Counting goes up by 1 on each pass, so the line must be Count ← Count + 1. Subtracting would make the Count column in the trace table go 0, -1, -2 and so on.
Q2
D — A running total must add each new value to the old total: Total ← Total + Num. Writing Total ← Num overwrites the total every pass, so the trace table would only show the last input.
Q3
B — The initialisation row of the trace table must show Total starting at 0. Starting at 1 makes every value in the Total column, including the final total, too big by 1. It should be Total ← 0.
Q4
C — To test for an even number you need MOD, which gives the remainder: Index MOD 2 = 0. DIV gives the whole-number quotient, so the condition is true for the wrong values and Count changes on the wrong passes.
Q5
B — A FOR loop counting up must run from the smaller value to the larger: FOR Index ← 1 TO 5. Starting at 5 and ending at 1 means the loop body never runs, so Total stays at 0 in the trace table.