← IGCSE Computer Science Paper 2

Totalling rows, columns and a whole 2D array

Adds each element to a running total inside nested loops. Set Total to 0 first: before the loops for a grand total, or inside the outer loop for each row or column total.

Understanding Totalling rows, columns and a whole 2D array in IGCSE Computer Science Paper 2

What it does: With Column in the outer loop, the inner loop moves down one column. Total is reset for each column and then adds every Row in that column. Total is never reset between rows, so it keeps growing. This gives a running total, not a row total.

Common mistakes: The indexes are the wrong way round. Mark[C, R] puts the column loop in the row position, and C goes up to 4 but there are only 3 rows. It should be Total ← Total + Mark[R, C]. The array has 4 columns, but this inner loop stops at 3. Each row total misses the last column. It should be FOR C ← 1 TO 4.

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 469–478) 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
You want ONE grand total of every mark in Marks[Row, Column]. Where should Total ← 0 go?
Inside the inner loopBefore both loops
Matches
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← 0 Total ← Total + Marks[Row, Column]
↓
Total ← 0 is inside the inner loop, so it is reset for every element and only the last element is kept. Move it to just after the outer FOR line, before the inner loop.
Applying
Meanings
Term
Total ← Total + Marks[Row, Column]
Meaning
Adds each element to a running total inside nested loops. Set Total to 0 first: before the loops for a grand total, or inside the outer loop for each row or column total.
Practice
FOR Row ← 1 TO 3
Testing
Gaps
Total ←
Check Answer
Spots
3 FOR C ← 1 TO 4
4 Total ← Total + Mark[C, R]

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

The 'Desert Tortoise' badge — Desert Master, 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. You want ONE grand total of every mark in Marks[Row, Column]. Where should Total ← 0 go?
A. Before both loops ✓
B. Inside the inner loop
Total must start at 0 once. If it is inside the inner loop, it is reset on every pass and the earlier marks are lost.
2. You want a separate total for EACH ROW. Where should Total ← 0 go?
A. Before the outer loop
B. Inside the outer loop, before the inner loop ✓
Each row needs a fresh start at 0. If Total is only reset before the outer loop, each row total also includes the rows before it.
3. Which line adds each element to the running total?
A. Total ← Marks[Row, Column]
B. Total ← Total + Marks[Row, Column] ✓
Total ← Marks[Row, Column] replaces the total with one element. You must add the element to the old Total.
4. You want a total for EACH COLUMN. Which loop order is correct?
A. Outer loop for Column, inner loop for Row ✓
B. Outer loop for Row, inner loop for Column
The inner loop must run down one column while the outer loop is fixed on that column. Swapping the loop order gives column totals.
5. Total ← 0 is placed before the outer loop, but Total is output after each row. What does the output show?
A. Each row total also includes all earlier rows ✓
B. Each row total is correct
Total is never reset between rows, so it keeps growing. This gives a running total, not a row total.

Match the Pairs

Total ← Total + Marks[Row, Column] → Adds the current element of the 2D array to the running total inside the nested loops
FOR Row ← 1 TO 3 Total ← 0 FOR Column ← 1 TO 4 → Resets the total to 0 for each new row, so every row gets its own total
For a column total, swap the loop order so the outer loop is Column and the inner loop is Row → FOR Column ← 1 TO 4 Total ← 0 FOR Row ← 1 TO 3 Total ← Total + Marks[Row, Column]
For a grand total of the whole array, set Total to 0 once, before both loops → Total ← 0 FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← 0 Total ← Total + Marks[Row, Column] → Total ← 0 is inside the inner loop, so it is reset for every element and only the last element is kept. Move it to just after the outer FOR line, before the inner loop.
Total ← 0 FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column] OUTPUT Total → Total ← 0 is placed before the loops, so the row totals keep growing. Each row total includes all the earlier rows. Move Total ← 0 inside the outer loop.

Key Term

Total ← Total + Marks[Row, Column]
Adds each element to a running total inside nested loops. Set Total to 0 first: before the loops for a grand total, or inside the outer loop for each row or column total.

Fill in the Blanks

1. Total ← 0 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
Hint: Type the value the running total must be reset to before adding up one row.
2. FOR Column ← 1 TO 4 Total ← 0 FOR Row ← 1 TO 3
Hint: Type the loop variable that must be the outer loop when totalling each column.
3. FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
Hint: Type the name of the 2D array whose elements are being added to the grand total.
4. NEXT Column OUTPUT Total NEXT Row
Hint: Type the keyword that closes the outer loop after each row total is displayed.
5. Total ← 0 FOR Row ← 1 TO 3 Total ← Total + Marks[Row, Column]
Hint: Type the index variable that changes in the inner loop when totalling one column.

Spot the Error

Question 1
A Total ← 0
B FOR R ← 1 TO 3
C FOR C ← 1 TO 4
D Total ← Total + Mark[C, R]
E NEXT C
F NEXT R
Line D — The indexes are the wrong way round. Mark[C, R] puts the column loop in the row position, and C goes up to 4 but there are only 3 rows. It should be Total ← Total + Mark[R, C].
Question 2
A FOR R ← 1 TO 3
B Total ← 0
C FOR C ← 1 TO 3
D Total ← Total + Mark[R, C]
E NEXT C
F NEXT R
Line C — The array has 4 columns, but this inner loop stops at 3. Each row total misses the last column. It should be FOR C ← 1 TO 4.
Question 3
A Total ← 1
B FOR R ← 1 TO 2
C FOR C ← 1 TO 3
D Total ← Total + Mark[R, C]
E NEXT C
F NEXT R
Line A — A grand total must start at 0 before the loops. Starting at 1 makes the final answer 1 too big. It should be Total ← 0.
Question 4
A FOR C ← 0 TO 4
B Total ← 0
C FOR R ← 1 TO 3
D Total ← Total + Mark[R, C]
E NEXT R
F NEXT C
Line A — The column indexes start at 1 (Mark[1:3,1:4]), so column 0 does not exist and causes an index out of range error. It should be FOR C ← 1 TO 4.
Question 5
A FOR C ← 1 TO 3
B Total = 0
C FOR R ← 1 TO 2
D Total ← Total + Mark[R, C]
E NEXT R
F NEXT C
Line B — Pseudocode assignment uses the arrow ←, not =. The reset for each column total should be Total ← 0.

Student with a class code? Join your class →