Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  Two-dimensional arrays and nested loops
Lesson pack · IGCSE Computer Science 0478 · 8.2 · IGCSE

Two-dimensional arrays and nested loops

Rows and columns. Some data comes as a table: sales for each shop in each week, a noughts and crosses board, marks for each student in each test.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.2
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 10 of 11 in Programming; follows One-dimensional arrays; leads into Reading from and writing to files
Central model
Fig. 1: a 2D array of sales for 3 shops (rows) over 4 weeks (columns)
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Declaring a 2D array: The two bounds in the declaration give the number of rows and the number of columns.
  • Nested loops: In the lab, choose 2D array with nested loops.
  • Column totals and grand totals: Swap the loops to work down each column instead of across each row.
Key points to leave on the board
  • Declare: ARRAY[1:rows, 1:columns] OF type.
  • Access: Name[Row, Column]; elements = rows × columns.
  • Nested loops: the inner loop completes for each pass of the outer loop.
  • Totals: reset a row total inside the outer loop, before the inner loop.
The core visual model

One picture every idea returns to

A two-dimensional (2D) array is a table of elements of the same data type, with two indexes: one for the row and one for the column. Declare it with two sets of bounds: DECLARE Sales : ARRAY[1:3, 1:4] OF INTEGER (3 rows, 4 columns, 12 elements). Access an element with Sales[Row, Column]. To process every element, use nested loops: the inner loop runs completely for every pass of the outer loop.

Sales : ARRAY[1:3, 1:4] OF INTEGER column (week) 1234 row 1row 2row 3 5826 3947 6158 Sales[2, 3] = 4 first index = row, second index = column
Fig. 1: a 2D array of sales for 3 shops (rows) over 4 weeks (columns)
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Rows and columns.. Some data comes as a table: sales for each shop in each week, a noughts and crosses board, marks for each student in each test. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Declaring a 2D arrayThe two bounds in the declaration give the number of rows and the number of columns. Your turn: Using the table, what are Sales[3, 1] and Sales[1, 4], and how many elements does Sales have?Whole class, then pairs
13 to 21 minTeach and check: Nested loopsIn the lab, choose 2D array with nested loops. Project tab 6 of the lab (2D arrays with nested loops) and let a student drive it while the class predicts. Model the worked example Row totals on the board, one line at a time.Pairs
21 to 30 minTeach and check: Column totals and grand totalsSwap the loops to work down each column instead of across each row. Model the worked example Column totals on the board, one line at a time. Your turn: A 2D array is declared ARRAY[1:5, 1:6]. How many times does the statement inside a pair of nested loops over every element run?Mix-Pair-Share
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Column totals and grand totals, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Mix-Pair-Share

Students mix around the room until you call pair; they pair with the nearest classmate, discuss the prompt, then mix again. Three prompts of rising difficulty, three different partners.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Do not swap the indexes.Sales[3, 2] is row 3, column 2 (the value 1), but Sales[2, 3] is row 2, column 3 (the value 4).
RowTotal ← 0 must be inside the outer loop but before the inner loop.Outside both loops, each row's total would include the rows before it.
Worksheet

Two-dimensional arrays and nested loops: worksheet

6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[2 marks]

Describe a two-dimensional array.

Q2 · write[2 marks]

Write a declaration for a 2D array Seats that stores a CHAR for each seat in a cinema with 12 rows of 20 seats.

Q3 · trace[3 marks]

State the values stored and the output.

FOR R ← 1 TO 2
    FOR C ← 1 TO 3
        Grid[R, C] ← R * C
    NEXT C
NEXT R
OUTPUT Grid[2, 3], " ", Grid[1, 2]
Q4 · write[5 marks]

Marks[1:30, 1:4] stores 4 test marks for each of 30 students (one row per student). Write pseudocode that outputs each student's total mark.

Q5 · analysis[2 marks]

A student puts RowTotal ← 0 before both loops in a row-total program. Explain the effect on the output.

Q6 · apply[3 marks]

Using the Sales table in this lesson (3 shops by 4 weeks), state (a) Sales[1, 2] (b) the total for week 4 (c) the total for shop 2.

Answer key and marking notes
Q1 · 2 marks
  • A table-like data structure with rows and columns, under one identifier, all elements the same data type.
  • Each element is accessed with two indexes: row and column.
Q2 · 2 marks
  • DECLARE Seats : ARRAY[1:12, 1:20]
  • OF CHAR
Q3 · 3 marks
  • Row 1: 1, 2, 3.
  • Row 2: 2, 4, 6.
  • Output: 6 2
Q4 · 5 marks
  • FOR Student ← 1 TO 30
  • Total ← 0 inside the outer loop, before the inner loop
  • FOR Test ← 1 TO 4 ... NEXT Test
  • Total ← Total + Marks[Student, Test] inside the inner loop
  • OUTPUT Student, Total after the inner loop, then NEXT Student
Q5 · 2 marks
  • RowTotal is never reset between rows.
  • So each row's output is a running total that includes all previous rows; only the first row's total is correct.
Q6 · 3 marks
  • (a) 8
  • (b) 6 + 7 + 8 = 21
  • (c) 3 + 9 + 4 + 7 = 23
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

How many elements are in the array declared by DECLARE Seats : ARRAY[1:4, 1:6] OF CHAR?

  • A24
  • B10
  • C6
  • D35
Q21 mark

The 2D array Sales holds these values.

SalesColumn 1Column 2Column 3Column 4
Row 1149127
Row 21116813
Row 3510156
What is output by OUTPUT Sales[2, 3]?

  • A8
  • B10
  • C16
  • D12
Q31 mark

How many times is OUTPUT "*" run?

FOR Row ← 1 TO 3
    FOR Column ← 1 TO 4
        OUTPUT "*"
    NEXT Column
NEXT Row

  • A12
  • B7
  • C4
  • D3
Q41 mark

Sales holds the values in the table.

SalesColumn 1Column 2Column 3Column 4
Row 1149127
Row 21116813
Row 3510156
What is the second line output by this program?
FOR Row ← 1 TO 3
    RowTotal ← 0
    FOR Column ← 1 TO 4
        RowTotal ← RowTotal + Sales[Row, Column]
    NEXT Column
    OUTPUT "Shop ", Row, " total: ", RowTotal
NEXT Row

  • AShop 2 total: 48
  • BShop 2 total: 35
  • CShop 2 total: 90
  • DShop 2 total: 42
Q51 mark

Sales holds the values in the table.

SalesColumn 1Column 2Column 3Column 4
Row 1149127
Row 21116813
Row 3510156
A student moves RowTotal ← 0 to before the outer loop.
RowTotal ← 0
FOR Row ← 1 TO 3
    FOR Column ← 1 TO 4
        RowTotal ← RowTotal + Sales[Row, Column]
    NEXT Column
    OUTPUT "Shop ", Row, " total: ", RowTotal
NEXT Row
What is the third line output?

  • AShop 3 total: 126
  • BShop 3 total: 36
  • CShop 3 total: 90
  • DShop 3 total: 48
Q61 mark

Grid holds the values in the table.

GridColumn 1Column 2Column 3
Row 1412
Row 2958
Row 3637
What is output?
Total ← 0
FOR Index ← 1 TO 3
    Total ← Total + Grid[Index, 4 - Index]
NEXT Index
OUTPUT Total

  • A13
  • B16
  • C19
  • D7
Quiz key
Q1: A

There are 4 rows and 6 columns, so 4 × 6 = 24 elements. 10 adds the bounds instead of multiplying. 6 is only the number of columns. 35 treats the bounds as running from 0 (5 × 7).

Q2: A

Sales[2, 3] is row 2, column 3, which holds 8. 10 is Sales[3, 2], with the indexes swapped. 16 is row 2, column 2. 12 is row 1, column 3.

Q3: A

The outer loop makes 3 passes and the inner loop runs 4 times in each, so the OUTPUT runs 3 × 4 = 12 times. 7 adds the loop counts. 4 counts only one pass of the outer loop. 3 counts only the outer loop.

Q4: A

On the second pass Row = 2 and the inner loop adds 11 + 16 + 8 + 13 = 48. 35 is the total of column 2, with rows and columns confused. 90 would be the running total of rows 1 and 2, which happens only if RowTotal is not reset. 42 is the total of row 1.

Q5: A

Because RowTotal is only set to 0 once, each output includes all rows so far: 42, then 42 + 48 = 90, then 90 + 36 = 126. 36 is the correct row 3 total from the original program. 90 is the second line. 48 is the row 2 total.

Q6: A

The elements used are Grid[1, 3] = 2, Grid[2, 2] = 5 and Grid[3, 1] = 6: the diagonal from top right to bottom left, total 13. 16 is the other diagonal, Grid[Index, Index] (4 + 5 + 7). 19 is the total of column 1. 7 is the total of row 1.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonOne-dimensional arrays