Totalling rows, columns and a whole 2D array

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

Totalling rows, columns and a whole 2D array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
3. Which line adds each element to the running total?
A Total ← Marks[Row, Column]
B Total ← Total + Marks[Row, Column]
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
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

Match the Pairs

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

Left columnRight column
1Total ← Total + Marks[Row, Column] AAdds the current element of the 2D array to the running total inside the nested loops
2FOR Row ← 1 TO 3 Total ← 0 FOR Column ← 1 TO 4 BTotal ← 0 FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
3For a column total, swap the loop order so the outer loop is Column and the inner loop is Row CTotal ← 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.
4For a grand total of the whole array, set Total to 0 once, before both loops DResets the total to 0 for each new row, so every row gets its own total
5FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← 0 Total ← Total + Marks[Row, Column] EFOR Column ← 1 TO 4 Total ← 0 FOR Row ← 1 TO 3 Total ← Total + Marks[Row, Column]
6Total ← 0 FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column] OUTPUT Total FTotal ← 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.

Definitions

Total ← Total + Marks[Row, Column]

Fill in the Blanks

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

1.
Total ← _ FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
2.
FOR ______ ← 1 TO 4 Total ← 0 FOR Row ← 1 TO 3
3.
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + _____[Row, Column]
4.
NEXT Column OUTPUT Total ____ Row
5.
Total ← 0 FOR Row ← 1 TO 3 Total ← Total + Marks[___, Column]

Spot the Error

Shade in the letter of the line that contains an 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
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
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
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
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

Answer Key

Totalling rows, columns and a whole 2D array
Section A: Quick Questions
Q1
A — Before both loops
Q2
B — Inside the outer loop, before the inner loop
Q3
B — Total ← Total + Marks[Row, Column]
Q4
A — Outer loop for Column, inner loop for Row
Q5
A — Each row total also includes all earlier rows
Section B: Match the Pairs
Q1
Total ← Total + Marks[Row, Column] → Adds the current element of the 2D array to the running total inside the nested loops
Q2
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
Q3
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]
Q4
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]
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
Q2
FOR Column ← 1 TO 4 Total ← 0 FOR Row ← 1 TO 3
Q3
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 Total ← Total + Marks[Row, Column]
Q4
NEXT Column OUTPUT Total NEXT Row
Q5
Total ← 0 FOR Row ← 1 TO 3 Total ← Total + Marks[Row, Column]
Section E: Spot the Error
Q1
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].
Q2
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.
Q3
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.
Q4
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.
Q5
B — Pseudocode assignment uses the arrow ←, not =. The reset for each column total should be Total ← 0.