Totalling in pseudocode

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

Totalling in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line should come BEFORE the loop to start a running total?
A Total ← Total + Value
B Total ← 0
2. Which line adds each new number to the running total inside the loop?
A Total ← Total + Value
B Total ← Value
3. Where should OUTPUT Total go in a totalling algorithm?
A After the loop has finished
B Before the loop starts
4. What goes wrong if Total ← 0 is missing before the loop?
A The loop will run one extra time
B Total has no proper starting value, so the final total may be wrong
5. Which placement of Total ← 0 is correct?
A Total ← 0 FOR Count ← 1 TO 5 INPUT Value Total ← Total + Value NEXT Count
B FOR Count ← 1 TO 5 Total ← 0 INPUT Value Total ← Total + Value NEXT Count

Match the Pairs

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

Left columnRight column
1Total ← 0 AAdds the current value to the running total, replacing Total with the new, larger total
2Total ← Total + Value BInitialises the running total to zero before the loop starts, so nothing is left over from earlier
3Add each number typed in by the user to the total inside a loop CFOR Count ← 1 TO 5 INPUT Number Total ← Total + Number NEXT Count
4Show the final total only once, after the loop has finished DTotal is assigned each array element instead of adding to itself, so it only holds the last element. It should be Total ← Total + Number[Index].
5FOR Count ← 1 TO 5 Total ← 0 INPUT Number Total ← Total + Number NEXT Count ENEXT Count OUTPUT Total
6FOR Index ← 1 TO 5 Total ← Number[Index] NEXT Index FTotal is set to 0 inside the loop, so it is reset on every pass and the running total is lost. Move Total ← 0 before the loop.

Definitions

Total ← Total + Value

Fill in the Blanks

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

1.
Total ← _ FOR Count ← 1 TO 5 INPUT Value
2.
FOR Count ← 1 TO 5 INPUT Value Total ← _____ + Value
3.
FOR Index ← 1 TO 5 Total ← Total + Marks[_____] NEXT Index
4.
Total ← Total + Value NEXT Count ______ "Total is ", Total
5.
REPEAT INPUT Value Total ← Total _ Value

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 N ← 1 TO 5
D Total ← Total + N
E NEXT N
F OUTPUT Total
Question 2
A Total ← 0
B FOR N ← 1 TO 5
C Total ← Mark[N]
D NEXT N
E OUTPUT Total
Question 3
A OUTPUT "Enter 3 numbers"
B Total = 0
C FOR Count ← 1 TO 3
D INPUT Value
E Total ← Total + Value
F NEXT Count
Question 4
A Total ← 0
B FOR Count ← 1 TO 5
C INPUT Total
D Total ← Total + Value
E NEXT Count
F OUTPUT Total
Question 5
A Total ← 0
B FOR N ← 1 TO 4
C Total ← Total + N
D NEXT N
E OUTPUT Total

Answer Key

Totalling in pseudocode
Section A: Quick Questions
Q1
B — Total ← 0
Q2
A — Total ← Total + Value
Q3
A — After the loop has finished
Q4
B — Total has no proper starting value, so the final total may be wrong
Q5
A — Total ← 0 FOR Count ← 1 TO 5 INPUT Value Total ← Total + Value NEXT Count
Section B: Match the Pairs
Q1
Total ← 0 → Initialises the running total to zero before the loop starts, so nothing is left over from earlier
Q2
Total ← Total + Value → Adds the current value to the running total, replacing Total with the new, larger total
Q3
Add each number typed in by the user to the total inside a loop → FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number NEXT Count
Q4
Show the final total only once, after the loop has finished → NEXT Count OUTPUT Total
Q5
FOR Count ← 1 TO 5 Total ← 0 INPUT Number Total ← Total + Number NEXT Count → Total is set to 0 inside the loop, so it is reset on every pass and the running total is lost. Move Total ← 0 before the loop.
Q6
FOR Index ← 1 TO 5 Total ← Number[Index] NEXT Index → Total is assigned each array element instead of adding to itself, so it only holds the last element. It should be Total ← Total + Number[Index].
Section C: Definitions
Q1
Total ← Total + Value — Totalling builds a running total. Set Total ← 0 before the loop, add each Value to Total inside the loop, then OUTPUT Total after the loop has finished.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Count ← 1 TO 5 INPUT Value
Q2
FOR Count ← 1 TO 5 INPUT Value Total ← Total + Value
Q3
FOR Index ← 1 TO 5 Total ← Total + Marks[Index] NEXT Index
Q4
Total ← Total + Value NEXT Count OUTPUT "Total is ", Total
Q5
REPEAT INPUT Value Total ← Total + Value
Section E: Spot the Error
Q1
B — A running total must start at 0. Starting at 1 makes every answer 1 too big. It should be Total ← 0.
Q2
C — This replaces Total each time instead of adding to it. It should be Total ← Total + Mark[N].
Q3
B — Assignment uses ←, not =. It should be Total ← 0.
Q4
C — INPUT Total overwrites the running total with the user's number. The loop should read into Value: INPUT Value.
Q5
C — This adds the index number, not the array element. It should be Total ← Total + Price[N].