← IGCSE Computer Science Paper 2

Totalling the elements of an array

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.

Understanding Totalling the elements of an array in IGCSE Computer Science Paper 2

What it does: The average is the total divided by the number of elements, here 10 elements, and it is calculated after the loop. The loop visits every index from 1 to 10 and adds each element Marks[Index] to Total once. An unset or leftover value in Total gets included in the sum, so the answer is wrong. The loop itself still runs. Total ← 0 initialises the running total before any elements are added to it. The total must be reset before the loop, not inside it. Otherwise earlier additions are lost. Without initialising Total to 0, the running total begins from an undefined value, so the sum is incorrect.

Common mistakes: The running total must start at 0. Starting at 1 makes the final total 1 too big. It should be Total ← 0. = is a comparison, not an assignment. Pseudocode uses ← to set the total, so the line should be Total ← 0.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 459–468) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line should come BEFORE the loop when totalling the elements of an array?
Total ← 0Total ← Marks[1]
Matches
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.
Applying
Meanings
Term
Total ← Total + Marks[Index]
Meaning
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.
Practice
Total ← 0
Testing
Gaps
Total ←
Check Answer
Spots
1Total ← 0
2Total ← 1

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Narwhal' badge — Unicorn Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line should come BEFORE the loop when totalling the elements of an array?
A. Total ← Marks[1]
B. Total ← 0 ✓
The running total must start at 0 so that nothing extra is added to the answer. Setting it to Marks[1] would count the first element twice.
2. Which line inside the loop adds each element to the running total?
A. Total ← Total + Marks[Index] ✓
B. Total ← Marks[Index]
The correct line adds the current element to what is already in Total. The wrong line overwrites Total on every pass, so only the last element is kept.
3. True or false: Total ← 0 must be written before the FOR loop starts.
A. True ✓
B. False
Total must be set to 0 before the loop. If it is set inside the loop, it is wiped on every pass.
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 ✓
Resetting Total to 0 inside the loop throws away the running total each time, so the result is just the last mark.
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
An unset or leftover value in Total gets included in the sum, so the answer is wrong. The loop itself still runs.

Match the Pairs

Total ← 0 → Sets the running total to zero before the loop starts, so the sum begins from a clean value
Total ← Total + Marks[Index] → Adds the element at the current index to the running total on each pass of the loop
Work out the average of the elements after the loop has finished adding them all up → Average ← Total / 10
Total all 10 elements of an array using a count-controlled loop → FOR Index ← 1 TO 10 Total ← Total + Marks[Index] NEXT Index
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.
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.

Key Term

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.

Fill in the Blanks

1. Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Marks[Index]
Hint: Type the starting value the total must be set to before the loop begins.
2. Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Marks[Index]
Hint: Type the loop variable that picks the current element each pass.
3. FOR Index ← 1 TO 5 Total ← Total + Marks[Index] NEXT Index
Hint: Type the keyword that closes the count-controlled loop.
4. OUTPUT Total Average ← Total / 5 OUTPUT Average
Hint: Type the operator that divides the total by the number of elements.
5. DECLARE Marks : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO 10 Total ← Total + Marks[Index]
Hint: Type the upper bound so the loop visits every element of the array.

Spot the Error

Question 1
A Total ← 0
B Total ← 1
C Total ← Total + Marks[Index]
D NEXT Index
E OUTPUT Total
Line B — The running total must start at 0. Starting at 1 makes the final total 1 too big. It should be Total ← 0.
Question 2
A Count ← 5
B Total ← 0
C Total = 0
D Total ← Total + Marks[Index]
E NEXT Index
F OUTPUT Total / Count
Line C — = is a comparison, not an assignment. Pseudocode uses ← to set the total, so the line should be Total ← 0.
Question 3
A Total ← Marks[10]
B FOR Index ← 1 TO 10
C Total ← Total + Marks[Index]
D NEXT Index
E OUTPUT Total
Line 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.
Question 4
A Total ← 0
B FOR Index ← 1 TO 8
C Total ← Total + Marks[Index]
D NEXT Total
E OUTPUT Total
Line D — NEXT must name the loop counter, which is Index, not the total. It should be NEXT Index.
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
Line A — A total is a number you add to, so it cannot be a STRING. It should be declared as INTEGER (or REAL).

Student with a class code? Join your class →