Skip to content

Course home

Representing algorithms

Representing algorithms

EasyMediumHard
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
Question 57

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:

  • The sum of the heights at any corresponding coordinate (i,j)(i, j)(i,j) in both grids is exactly 5.

For example, Grid B is a perfect complement of Grid A, but Grid C is not:

Grid A

Col 0Col 1Col 2
Row 0142
Row 1305
Row 2213

Grid B (Complement of Grid A)

Col 0Col 1Col 2
Row 0413
Row 1250
Row 2342

Grid C (NOT a Complement of Grid A)

Col 0Col 1Col 2
Row 0413
Row 1220
Row 2342

(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
[6]
Markscheme

Representing algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Representing algorithms

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.

Question bank