10.2 Arrays

Bulk view disabled for Guests. View lessons individually.

Arrays: 1D, 2D and 3D

An array is a composite data type holding a fixed number of elements of the same data type, each identified by an index. At A Level you must handle one, two and three dimensions, and write algorithms that traverse them.

1. Declaring Arrays

// 1D - a list of 30 marks DECLARE Marks : ARRAY[1:30] OF INTEGER // 2D - a table: 30 students x 4 tests DECLARE Results : ARRAY[1:30, 1:4] OF INTEGER // 3D - 5 classes x 30 students x 4 tests DECLARE School : ARRAY[1:5, 1:30, 1:4] OF INTEGER
DimensionsMental modelIndexes neededElements
1DA list130
2DA table of rows and columns2 — [row, column]30 × 4 = 120
3DA stack of tables3 — [table, row, column]5 × 30 × 4 = 600
Arrays are fixed in size. The bounds are set at declaration and cannot change while the program runs. Where the number of items is unknown in advance, a dynamic structure built with pointers — a linked list — is the appropriate choice instead.

2. Traversing Arrays with Iteration

1D: one loop

Total ← 0 FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index OUTPUT "Average: ", Total / 30

2D: nested loops

FOR Student ← 1 TO 30 Total ← 0 FOR Test ← 1 TO 4 Total ← Total + Results[Student, Test] NEXT Test OUTPUT "Student ", Student, " total: ", Total NEXT Student

Note that Total is reset inside the outer loop but outside the inner one, because a separate total is wanted per student. Where the total belongs determines what is calculated — this is the crux of most 2D array questions.

3D: three nested loops

FOR Class ← 1 TO 5 FOR Student ← 1 TO 30 FOR Test ← 1 TO 4 OUTPUT School[Class, Student, Test] NEXT Test NEXT Student NEXT Class

The innermost statement executes 5 × 30 × 4 = 600 times — once per element.

3. Row and Column Operations

Fixing one index and looping the other processes a single row or column.

// Total for ONE student (row 7) - fix the row, loop the columns Total ← 0 FOR Test ← 1 TO 4 Total ← Total + Results[7, Test] NEXT Test // Average for ONE test (column 2) - fix the column, loop the rows Total ← 0 FOR Student ← 1 TO 30 Total ← Total + Results[Student, 2] NEXT Student OUTPUT Total / 30
Which index is fixed decides what you calculate. Fixing the row totals one student across all tests; fixing the column averages one test across all students. Swapping them answers a different question entirely, and is the most frequent error in 2D array questions.

4. Finding a Maximum and Its Position

DECLARE Highest, BestStudent, BestTest : INTEGER Highest ← Results[1, 1] // start from the FIRST element BestStudent ← 1 BestTest ← 1 FOR Student ← 1 TO 30 FOR Test ← 1 TO 4 IF Results[Student, Test] > Highest THEN Highest ← Results[Student, Test] BestStudent ← Student // record WHERE it was found BestTest ← Test ENDIF NEXT Test NEXT Student
Initialise Highest from the first element, never from 0. With negative data an initial 0 would never be beaten and the answer would be wrong — and it would also fail a boundary test.

5. Arrays Compared with Other Structures

ArrayLinked list
SizeFixed at declarationGrows and shrinks at run time
AccessDirect by index — O(1)Must traverse from the start — O(n)
Insert in the middleSlow — elements must shiftFast — adjust two pointers
MemoryContiguous blockScattered, plus space for pointers
Binary searchPossible, if sortedNot possible — no direct access

6. Exam Focus

Give complete declarations. Identifier, bounds for every dimension, and the data type: DECLARE Results : ARRAY[1:30, 1:4] OF INTEGER. Omitting any part loses the mark.
Place the accumulator deliberately. Resetting a total inside or outside the outer loop produces completely different results. Decide which is wanted before writing the loops, and state your intention with a comment.
Match loop bounds to the declared bounds. An array declared [1:30] needs 1 TO 30. Starting at 0 accesses an element outside the array.

Quick self-check

  • Declare a 2D array for 8 teams and 12 matches holding whole-number scores.
  • How many elements does ARRAY[1:4, 1:6, 1:2] contain?
  • Write pseudocode to total column 3 of a [1:20, 1:5] array.
  • Explain why a maximum should be initialised from the first element.
  • Give two advantages of an array over a linked list, and two of a linked list over an array.