A terrain-mapping application represents 3x3 grids of elevation data using two-dimensional arrays, where each element is an integer from 0 to 5 denoting height.
Two grids are perfect complements of each other if:
For example, Grid B is a perfect complement of Grid A, but Grid C is not:
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 1 | 4 | 2 |
| Row 1 | 3 | 0 | 5 |
| Row 2 | 2 | 1 | 3 |
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 4 | 1 | 3 |
| Row 1 | 2 | 5 | 0 |
| Row 2 | 3 | 4 | 2 |
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 4 | 1 | 3 |
| Row 1 | 2 | 2 | 0 |
| Row 2 | 3 | 4 | 2 |
(Note: In Grid C, Row 1, Col 1 has a height of 2, and in Grid A, Row 1, Col 1 has a height of 0. Their sum is 2, not 5.)
A developer has started writing a pseudo-code algorithm to check if two 3x3 elevation grids, grid1 and grid2, are perfect complements of each other.
Complete the algorithm in pseudo-code, ensuring that, when the algorithm terminates, the variable isComplement is set to true if they are perfect complements, or false otherwise.
The algorithm must work for any valid 3x3 grids stored in grid1 and grid2. Zero-based indexing is used.
grid1 ← [ [1, 4, 2], [3, 0, 5], [2, 1, 3] ]
grid2 ← [ [4, 1, 3], [2, 5, 0], [3, 4, 2] ]
isComplement ← true
i ← 0
WHILE i ≤ 2
j ← 0
WHILE j ≤ 2
208 exam-style questions on AQA GCSE Computer Science Representing algorithms. Each one has a worked solution and a mark scheme showing where the marks go.