Totalling the elements of an array

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

Totalling the elements of an array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line should come BEFORE the loop when totalling the elements of an array?
A Total ← Marks[1]
B Total ← 0
2. Which line inside the loop adds each element to the running total?
A Total ← Total + Marks[Index]
B Total ← Marks[Index]
3. True or false: Total ← 0 must be written before the FOR loop starts.
A True
B False
4. Which pseudocode correctly totals a 10-element array Marks[1:10]?
A FOR Index ← 1 TO 10 Total ← 0 Total ← Total + Marks[Index] NEXT Index
B Total ← 0 FOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index
5. A student forgets to set Total ← 0 before the loop. What is the problem?
A Total may start with a leftover value, so the final answer is wrong
B The loop will not run at all

Match the Pairs

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

Left columnRight column
1Total ← 0 AFOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index
2Total ← Total + Marks[Index] BTotal was never set to 0 before the loop, so it starts with an unknown value and the final answer will be wrong. Add Total ← 0 before the FOR.
3Work out the average of the elements after the loop has finished adding them all up CAdds the element at the current index to the running total on each pass of the loop
4Total all 10 elements of an array using a count-controlled loop DSets the running total to zero before the loop starts, so the sum begins from a clean value
5FOR Index ← 1 TO 10 Total ← 0 Total ← Total + Marks[Index] NEXT Index EAverage ← Total / 10
6FOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index OUTPUT Total FTotal is reset to 0 inside the loop, so it is wiped on every pass and only the last mark is kept. Set Total ← 0 once before the loop.

Definitions

Total ← Total + Marks[Index]

Fill in the Blanks

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

1.
Total ← _ FOR Index ← 1 TO 5 Total ← Total + Marks[Index]
2.
Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Marks[_____]
3.
FOR Index ← 1 TO 5 Total ← Total + Marks[Index] ____ Index
4.
OUTPUT Total Average ← Total _ 5 OUTPUT Average
5.
DECLARE Marks : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO __ Total ← Total + Marks[Index]

Spot the Error

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

Question 1
A Total ← 0
B Total ← 1
C Total ← Total + Marks[Index]
D NEXT Index
E OUTPUT Total
Question 2
A Count ← 5
B Total ← 0
C Total = 0
D Total ← Total + Marks[Index]
E NEXT Index
F OUTPUT Total / Count
Question 3
A Total ← Marks[10]
B FOR Index ← 1 TO 10
C Total ← Total + Marks[Index]
D NEXT Index
E OUTPUT Total
Question 4
A Total ← 0
B FOR Index ← 1 TO 8
C Total ← Total + Marks[Index]
D NEXT Total
E OUTPUT Total
Question 5
A DECLARE Total : STRING
B Total ← 0
C FOR Index ← 1 TO 6
D Total ← Total + Marks[Index]
E NEXT Index
F OUTPUT Total

Answer Key

Totalling the elements of an array
Section A: Quick Questions
Q1
B — Total ← 0
Q2
A — Total ← Total + Marks[Index]
Q3
A — True
Q4
B — Total ← 0 FOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index
Q5
A — Total may start with a leftover value, so the final answer is wrong
Section B: Match the Pairs
Q1
Total ← 0 → Sets the running total to zero before the loop starts, so the sum begins from a clean value
Q2
Total ← Total + Marks[Index] → Adds the element at the current index to the running total on each pass of the loop
Q3
Work out the average of the elements after the loop has finished adding them all up → Average ← Total / 10
Q4
Total all 10 elements of an array using a count-controlled loop → FOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index
Q5
FOR Index ← 1 TO 10 Total ← 0 Total ← Total + Marks[Index] NEXT Index → Total is reset to 0 inside the loop, so it is wiped on every pass and only the last mark is kept. Set Total ← 0 once before the loop.
Q6
FOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index OUTPUT Total → Total was never set to 0 before the loop, so it starts with an unknown value and the final answer will be wrong. Add Total ← 0 before the FOR.
Section C: Definitions
Q1
Total ← Total + Marks[Index] — Adds the current element to the running total on each loop pass. Set Total ← 0 before the loop first, or the answer will be wrong. Divide by the number of elements for the average.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Marks[Index]
Q2
Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Marks[Index]
Q3
FOR Index ← 1 TO 5 Total ← Total + Marks[Index] NEXT Index
Q4
OUTPUT Total Average ← Total / 5 OUTPUT Average
Q5
DECLARE Marks : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO 10 Total ← Total + Marks[Index]
Section E: Spot the Error
Q1
B — The running total must start at 0. Starting at 1 makes the final total 1 too big. It should be Total ← 0.
Q2
C — = is a comparison, not an assignment. Pseudocode uses ← to set the total, so the line should be Total ← 0.
Q3
A — The total must be set to 0 before the loop. Starting with Marks[10] would count that element twice. It should be Total ← 0.
Q4
D — NEXT must name the loop counter, which is Index, not the total. It should be NEXT Index.
Q5
A — A total is a number you add to, so it cannot be a STRING. It should be declared as INTEGER (or REAL).