8.2 Arrays

Arrays: 1D, 2D and Iteration

1. Why Arrays Exist

Suppose a program must store the marks of 30 students. Without arrays you would need 30 separate variables — and no way to loop through them. An array solves this: it is a single named structure holding many values of the same data type, each reached by its position number.

An array holds multiple values of the same data type under one identifier. Each value is accessed using an index (also called a subscript), which is its position in the array.

2. One-Dimensional (1D) Arrays

A 1D array is a single list of values.

DECLARE Names : ARRAY[1:5] OF STRING DECLARE Marks : ARRAY[1:5] OF INTEGER

The [1:5] gives the lower and upper bounds — this array has five elements, numbered 1 to 5.

[1][2][3][4][5]
PatrickJaneJohnAmiraBen

Writing to and reading from an array

Names[1] ← "Patrick" // write a value Names[2] ← "Jane" OUTPUT Names[1] // read a value - outputs Patrick
Syllabus note: the first index of an array may be zero or one. Both are accepted — but you must be consistent within one answer, and you must say which you are using if it is not obvious from your declaration.

3. Using Iteration with Arrays

The real power of an array is that a loop counter can be used as the index. This is what lets one short loop process any number of elements.

Filling an array

FOR Index ← 1 TO 5 OUTPUT "Enter a name: " INPUT Names[Index] // Index is the loop counter NEXT Index

Reading an array back

FOR Index ← 1 TO 5 OUTPUT Names[Index] NEXT Index

Totalling an array

Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Marks[Index] NEXT Index OUTPUT "Average: ", Total / 5
Do not confuse the index with the value. Marks[3] is the value stored at position 3, not the number 3. Writing Total ← Total + Index adds the position numbers instead of the marks — a very common slip.

4. Two-Dimensional (2D) Arrays

A 2D array is best pictured as a table with rows and columns — useful for a seating plan, a timetable, or several marks per student.

DECLARE ClassData : ARRAY[1:3, 1:2] OF INTEGER // 3 rows, 2 columns
Column 1 (Test 1)Column 2 (Test 2)
Row 19588
Row 27280
Row 36491

Both a row and a column index are needed, given as [row, column]:

ClassData[1, 1] ← 95 ClassData[1, 2] ← 88 OUTPUT ClassData[1, 2] // outputs 88 OUTPUT ClassData[3, 1] // outputs 64
Row comes first, then column. ClassData[3, 1] is row 3 column 1. Reversing them reads a different element — or an element that does not exist, if the array is not square.

5. Nested Iteration with a 2D Array

Processing every element of a 2D array needs one loop inside another: the outer loop steps through the rows, the inner loop through the columns of the current row.

FOR Row ← 1 TO 3 FOR Column ← 1 TO 2 OUTPUT ClassData[Row, Column] NEXT Column NEXT Row
The inner loop completes in full for every single pass of the outer loop. Here the outer loop runs 3 times and the inner loop 2 times per pass, so OUTPUT executes 3 × 2 = 6 times — once per element.

Totalling every element

Total ← 0 FOR Row ← 1 TO 3 FOR Column ← 1 TO 2 Total ← Total + ClassData[Row, Column] NEXT Column NEXT Row OUTPUT "Total of all marks: ", Total

Totalling one row only

// Row 2 only - fix the row, loop the columns Total ← 0 FOR Column ← 1 TO 2 Total ← Total + ClassData[2, Column] NEXT Column

6. Searching an Array

A linear search over an array combines a loop, a flag and the index:

Found ← FALSE Index ← 1 WHILE Index <= 5 AND Found = FALSE IF Names[Index] = SearchName THEN Found ← TRUE ELSE Index ← Index + 1 ENDIF ENDWHILE IF Found = TRUE THEN OUTPUT "Found at position ", Index ELSE OUTPUT "Not in the list" ENDIF

7. Program Code Equivalents

For the extended scenario question you may answer in program code instead of pseudocode. The same 1D and 2D structures in Python look like this:

# 1D array (a Python list) students = ["Patrick", "Jane", "John"] print(students[0]) # Patrick - Python indexes from 0 # 2D array (a list of lists) class_data = [ ["Patrick", 95], ["Jane", 88] ] print(class_data[0][1]) # 95
Python indexes from 0, Cambridge pseudocode examples usually from 1. If you switch between them, adjust your loop bounds. Mixing the two conventions in one answer produces off-by-one errors that lose marks even when the logic is sound.

8. Exam Focus

Declare the array properly. A declaration needs the identifier, the bounds and the data type: DECLARE Marks : ARRAY[1:30] OF INTEGER. Omitting the bounds or the type loses the declaration mark.
Use the loop counter as the index. Answers that write out Names[1], Names[2], Names[3]… one line at a time do not demonstrate iteration and are not given full credit.
Match the loop bounds to the array bounds. For ARRAY[1:30] the loop is 1 TO 30. Looping 0 TO 30 or 1 TO 29 reads outside the array or misses an element.

Quick self-check

  • Declare a 1D array to hold 20 real numbers.
  • Write a loop that outputs every element of that array.
  • For ARRAY[1:4, 1:3], how many elements are there, and how many times does a full nested loop iterate?
  • Explain the difference between Marks[3] and the value 3.
  • Write pseudocode to find the largest value in Marks : ARRAY[1:10] OF INTEGER.