# 2D Arrays & Matrix Grid Iteration

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 8: Programming Concepts & File Handling
- **Estimated Duration:** 20 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

A Two-Dimensional (2D) Array represents data arranged in a grid of rows and columns (a matrix). Like 1D arrays, all elements in a 2D array must be of the same data type.

1. Declaration in Cambridge Pseudocode
DECLARE <Identifier> : ARRAY[<RowStart>:<RowEnd>, <ColStart>:<ColEnd>] OF <DataType>
Example:
DECLARE ChessBoard : ARRAY[1:8, 1:8] OF CHAR
DECLARE StudentMarks : ARRAY[1:30, 1:4] OF INTEGER  // 30 students, 4 subjects
Row-Major Indexing:
Cambridge pseudocode follows the standard mathematical convention where the first index represents the row and the second index represents the column:
$$\text{Array}[\text{Row}, \text{Column}]$$

2. Iterating Across 2D Grids
Traversing every cell in a 2D array requires nested FOR loops:
The outer loop iterates through the rows.The inner loop iterates through the columns for each row:
FOR r <- 1 TO 30
    TotalScore <- 0
    FOR c <- 1 TO 4
        TotalScore <- TotalScore + StudentMarks[r, c]
    NEXT c
    OUTPUT "Student ", r, " Total: ", TotalScore
NEXT r
Worked non-square traversal
For two rows [2,4,6] and [10,20,30], row totals are 12 and 60. Set Total to zero at the start of EACH row, then add its three columns. If Total is reset only once, the second result becomes 72. In ARRAY[1:2,1:3], swapping the indices can also attempt a nonexistent third row.
Before solving, draw the two-by-three table, label both index ranges and trace the reset. Then use data different from this worked example.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Cambridge 2D Array Declaration and Index Convention (Multiple choice, 25 XP)

**Instructions:**
An array is declared in Cambridge pseudocode as: `DECLARE Board : ARRAY[1:4, 1:6] OF CHAR`

How many total elements does this 2D array contain, and what do the indices represent?

**Options:**
- **A**: 10 elements arranged in a 4 by 6 grid
- **B**: 24 elements arranged in 4 rows and 6 columns
- **C**: 24 elements arranged in 6 rows and 4 columns
- **D**: Dynamic unbounded storage


**Autotest Expectations:**
- **Correct dimensions and element count identified**: `selection_match` -> Expected: `opt_24`

### Exercise 2: Exam-Style Question: Access Specific 2D Matrix Cells (Fill in blank, 35 XP)

**Instructions:**
Given the 2D array `SeatingPlan : ARRAY[1:5, 1:8] OF STRING`:

1. To reference the student sitting in Row 2, Column 5, write `SeatingPlan[blank_1]`.
2. The total number of seats in the room is [blank_2].


**Starter Code:**
```text
{"prompt":"Given the 2D array `SeatingPlan : ARRAY[1:5, 1:8] OF STRING`:","template":"1. To reference Row 2, Column 5: SeatingPlan[blank_1]\n2. Total seats in the room: [blank_2]","word_bank":["2, 5","5, 2","40","13","20"]}
```

**Autotest Expectations:**
- **Coordinates 2, 5**: `text_exact` -> Expected: `2\s*,\s*5`
- **Total seats 40**: `text_exact` -> Expected: `40`

### Exercise 3: Independent Practice: Row totals (Pseudocode, 30 XP)

**Instructions:**
Read six integers into a 2-row, 3-column array in row order. Output the total of row 1, then the total of row 2. Reset the total at the correct point.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.


**Starter Code:**
```text
// Plan your solution, then write your program.

```

**Autotest Expectations:**
- **Different row totals**: `test_case` -> Expected: `{&quot;inputs&quot;:[1,2,3,10,20,30],&quot;output&quot;:&quot;6\n60&quot;}`
- **Mixed signs**: `test_case` -> Expected: `{&quot;inputs&quot;:[5,-5,0,-2,-3,1],&quot;output&quot;:&quot;0\n-4&quot;}`
- **Reset at each row**: `test_case` -> Expected: `{&quot;inputs&quot;:[1,1,1,1,1,1],&quot;output&quot;:&quot;3\n3&quot;}`

