Tracing a totalling algorithm

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

Tracing a totalling algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace table for a totalling algorithm has a Total column. What should the Total column show after each pass of the loop?
A Only the latest value that was read in
B The running total so far, which has grown by the latest value
2. Numbers input are 4, 6 and 2. Total starts at 0. Which Total column is correct after each pass?
A 4, 10, 12
B 4, 6, 2
3. Which line must come BEFORE the loop so the first row of the trace table is right?
A Total ← 0
B Total ← Total + Number
4. A student forgets Total ← 0 and the algorithm still runs. What does the Total column show on the first row of the trace table?
A The first value is added to a starting value that was never set, so the total is wrong
B The total is always right because it starts at 0 by magic
5. Where should Total ← Total + Number be placed in a totalling algorithm?
A After the loop, so it runs once at the end
B Inside the loop, so it runs once for every value

Match the Pairs

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

Left columnRight column
1Total ← Total + Number ATotal was never set to 0 before the loop, so the first row of the trace table has no valid starting value and the totals will be wrong.
2Total ← 0 BFOR Count ← 1 TO 3 INPUT Number Total ← Total + Number NEXT Count
3For the values 4, 6 and 5, the Total column reads 0, 4, 10, 15 because the total grows after each value is added CTotal ← 0 FOR Count ← 1 TO 5 Total ← Total + Number[Count] NEXT Count
4The total must be set to 0 before the loop starts so the first row of the trace table has a correct starting value DThe total is added after the loop, so the Total column only changes once at the end and does not grow on each pass. The line should be inside the loop.
5FOR Count ← 1 TO 3 INPUT Number NEXT Count Total ← Total + Number ESets the Total column to 0 in the first row of the trace table, before any values are added
6FOR Count ← 1 TO 3 INPUT Number Total ← Total + Number NEXT Count FOn each pass, the Total column shows the old total plus the new Number, so it grows row by row in the trace table

Definitions

Total ← Total + Number

Fill in the Blanks

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

1.
Total ← _ FOR Count ← 1 TO 4 INPUT Number
2.
FOR Count ← 1 TO 4 Total ← Total _ Number NEXT Count
3.
FOR Count ← 1 TO 4 Total ← _____ + Number NEXT Count
4.
FOR Count ← 1 TO 4 Total ← Total + Number ____ Count
5.
NEXT Count OUTPUT _____

Spot the Error

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

Question 1
A DECLARE Total : INTEGER
B Total ← 1
C FOR Count ← 1 TO 3
D Total ← Total + Count
E NEXT Count
F OUTPUT Total
Question 2
A 0 ← Total
B FOR Count ← 1 TO 4
C INPUT Number
D Total ← Total + Number
E NEXT Count
F OUTPUT Total
Question 3
A Total ← Number
B REPEAT
C INPUT Number
D Total ← Total + Number
E UNTIL Number = 0
F OUTPUT Total
Question 4
A Total ← 0
B FOR Day ← 1 TO 7
C INPUT Steps
D Total ← Steps
E NEXT Day
Question 5
A DECLARE Total : INTEGER
B DECLARE Number : INTEGER
C Count ← 0
D FOR Count ← 1 TO 3
E INPUT Number
F Total ← Total + Number

Answer Key

Tracing a totalling algorithm
Section A: Quick Questions
Q1
B — The running total so far, which has grown by the latest value
Q2
A — 4, 10, 12
Q3
A — Total ← 0
Q4
A — The first value is added to a starting value that was never set, so the total is wrong
Q5
B — Inside the loop, so it runs once for every value
Section B: Match the Pairs
Q1
Total ← Total + Number → On each pass, the Total column shows the old total plus the new Number, so it grows row by row in the trace table
Q2
Total ← 0 → Sets the Total column to 0 in the first row of the trace table, before any values are added
Q3
For the values 4, 6 and 5, the Total column reads 0, 4, 10, 15 because the total grows after each value is added → FOR Count ← 1 TO 3 INPUT Number Total ← Total + Number NEXT Count
Q4
The total must be set to 0 before the loop starts so the first row of the trace table has a correct starting value → Total ← 0 FOR Count ← 1 TO 5 Total ← Total + Number[Count] NEXT Count
Q5
FOR Count ← 1 TO 3 INPUT Number NEXT Count Total ← Total + Number → The total is added after the loop, so the Total column only changes once at the end and does not grow on each pass. The line should be inside the loop.
Q6
FOR Count ← 1 TO 3 INPUT Number Total ← Total + Number NEXT Count → Total was never set to 0 before the loop, so the first row of the trace table has no valid starting value and the totals will be wrong.
Section C: Definitions
Q1
Total ← Total + Number — In a trace table, this line makes the Total column grow by the new Number on every pass. Total must start at 0 before the loop, and this line must sit inside the loop.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Count ← 1 TO 4 INPUT Number
Q2
FOR Count ← 1 TO 4 Total ← Total + Number NEXT Count
Q3
FOR Count ← 1 TO 4 Total ← Total + Number NEXT Count
Q4
FOR Count ← 1 TO 4 Total ← Total + Number NEXT Count
Q5
NEXT Count OUTPUT Total
Section E: Spot the Error
Q1
B — Total must start at 0. Starting at 1 makes every row of the trace table (and the final output) 1 too big. The line should be Total ← 0.
Q2
A — The assignment arrow points to the variable being given a value. The variable goes on the left, so it should be Total ← 0, which sets the first Total value in the trace table to 0.
Q3
A — Number has no value before the loop, so Total starts with nothing useful in the trace table. Total must be set to 0 before the loop: Total ← 0.
Q4
D — This replaces Total with the latest Steps value instead of adding to it. The Total column would not grow. It should be Total ← Total + Steps.
Q5
C — This sets the wrong variable to 0. Total is never given a starting value, so the first trace table row is wrong. The line should be Total ← 0.