Totalling in Cambridge pseudocode

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

Totalling in Cambridge pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly adds Number to the running total?
A Total ← Number
B Total ← Total + Number
2. Where should Total ← 0 be placed in a totalling algorithm?
A Inside the loop, before the addition
B Before the loop starts
3. Which declaration is correct for a variable that stores a whole-number total?
A DECLARE Total : INTEGER
B DECLARE Total ← INTEGER
4. What happens if you forget to set Total ← 0 before the loop?
A The total may start from an unknown value, so the answer can be wrong
B The total automatically starts at 0 so nothing goes wrong
5. Which loop correctly totals 5 numbers typed in by the user?
A FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number NEXT Count
B FOR Count ← 1 TO 5 INPUT Number Total ← Number 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 ATotal ← 0 is inside the loop, so the total is reset to zero on every repeat and only the last number is kept.
2Total ← Total + Number BSets the running total to zero before the loop starts
3Declare an integer variable to hold the running total CTotal ← Number replaces the total instead of adding to it, so the earlier values are lost.
4Total every element in an array called Marks with 5 items DFOR Index ← 1 TO 5 Total ← Total + Marks[Index] NEXT Index
5FOR Count ← 1 TO 5 INPUT Number Total ← 0 Total ← Total + Number NEXT Count EAdds the new Number to the running total each time the loop repeats
6Total ← 0 FOR Count ← 1 TO 5 INPUT Number Total ← Number NEXT Count FDECLARE Total : INTEGER

Definitions

Total ← Total + Number

Fill in the Blanks

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

1.
DECLARE Total : _______
2.
DECLARE Total : INTEGER Total ← _ FOR Count ← 1 TO 5
3.
FOR Count ← 1 TO 5 INPUT Number Total ← Total + ______
4.
FOR Index ← 1 TO 10 Total ← Total + Scores[_____] NEXT Index
5.
FOR Count ← 1 TO 5 Total ← _____ + Number NEXT Count

Spot the Error

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

Question 1
A DECLARE Total : INTEGER
B Total ← 0
C FOR Count ← 1 UNTIL 5
D INPUT Number
E Total ← Total + Number
F NEXT Count
Question 2
A DECLARE Total INTEGER
B Total ← 0
C FOR Count ← 1 TO 5
D INPUT Number
E Total ← Total + Number
F NEXT Count
Question 3
A DECLARE Total : STRING
B Total ← 0
C FOR Count ← 1 TO 4
D INPUT Price
E Total ← Total + Price
F NEXT Count
Question 4
A Total ← 0
B FOR Index ← 1 TO 5
C Total ← Total + Marks[Count]
D NEXT Index
E OUTPUT Total
Question 5
A DECLARE Total : INTEGER
B Total ← 1
C FOR Count ← 1 TO 10
D INPUT Number
E Total ← Total + Number
F NEXT Count

Answer Key

Totalling in Cambridge pseudocode
Section A: Quick Questions
Q1
B — Total ← Total + Number
Q2
B — Before the loop starts
Q3
A — DECLARE Total : INTEGER
Q4
A — The total may start from an unknown value, so the answer can be wrong
Q5
A — FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number NEXT Count
Section B: Match the Pairs
Q1
Total ← 0 → Sets the running total to zero before the loop starts
Q2
Total ← Total + Number → Adds the new Number to the running total each time the loop repeats
Q3
Declare an integer variable to hold the running total → DECLARE Total : INTEGER
Q4
Total every element in an array called Marks with 5 items → FOR Index ← 1 TO 5 Total ← Total + Marks[Index] NEXT Index
Q5
FOR Count ← 1 TO 5 INPUT Number Total ← 0 Total ← Total + Number NEXT Count → Total ← 0 is inside the loop, so the total is reset to zero on every repeat and only the last number is kept.
Q6
Total ← 0 FOR Count ← 1 TO 5 INPUT Number Total ← Number NEXT Count → Total ← Number replaces the total instead of adding to it, so the earlier values are lost.
Section C: Definitions
Q1
Total ← Total + Number — Adds each new Number to the running Total every time the loop repeats. Set Total ← 0 before the loop so the total starts from zero.
Section D: Fill in the Blanks
Q1
DECLARE Total : INTEGER
Q2
DECLARE Total : INTEGER Total ← 0 FOR Count ← 1 TO 5
Q3
FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number
Q4
FOR Index ← 1 TO 10 Total ← Total + Scores[Index] NEXT Index
Q5
FOR Count ← 1 TO 5 Total ← Total + Number NEXT Count
Section E: Spot the Error
Q1
C — A FOR loop uses TO to give its end value, not UNTIL. It should be FOR Count ← 1 TO 5.
Q2
A — A DECLARE statement needs a colon between the variable name and its data type. It should be DECLARE Total : INTEGER.
Q3
A — A total is a number that is added to, so it cannot be a STRING. Prices with decimals need REAL, so it should be DECLARE Total : REAL.
Q4
C — The loop counter is Index, so the array must be read with Marks[Index]. Count does not exist in this program, so it should be Total ← Total + Marks[Index].
Q5
B — The running total must start at 0 before the loop. Starting at 1 makes the final total 1 too big. It should be Total ← 0.