Pro Content

Upgrade to access all Cambridge 9618 lessons including arrays, data structures, and algorithm implementation.

Upgrade to Pro →
← Back to Dashboard
🗂️ Paper 2 · 2.2 Data Types & Structures
2.2.2 Arrays (1D and 2D)
Cambridge 9618 · International A Level Computer Science · ~14 min read
Notes
Video
Slides
Quiz
Worksheet

What is an Array?

An array is a data structure that stores a fixed number of elements of the same data type in a contiguous block of memory. Arrays allow multiple values to be stored under a single variable name, accessed using an index.

Key properties of arrays in Cambridge 9618:

  • Fixed size — the size must be declared at creation and cannot change
  • Same type — all elements must be of the same data type
  • Indexed — elements are accessed by index (Cambridge uses 1-based indexing by default in pseudocode)
  • Random access — any element can be accessed directly in O(1) time

Declaring a 1D Array

Cambridge 9618 syntax:

DECLARE scores : ARRAY[1:5] OF INTEGER
DECLARE names : ARRAY[1:10] OF STRING
DECLARE temps : ARRAY[0:6] OF REAL // 7 elements, indices 0-6

The format is: DECLARE name : ARRAY[lower:upper] OF type

The array has upper - lower + 1 elements. So ARRAY[1:5] has 5 elements; ARRAY[0:6] has 7 elements.

Visualising a 1D Array — scores[1:5]

scores : ARRAY[1:5] OF INTEGER
42
78
91
55
83
[1]
[2]
[3]
[4]
[5]

Accessing and modifying array elements

// Assign values
scores[1] ← 42
scores[3] ← 91

// Read a value
OUTPUT scores[2] // outputs 78

// Use in calculation
total ← total + scores[i]

Iterating Through a 1D Array

The standard pattern for processing all elements of a 1D array uses a FOR loop from lower bound to upper bound:

DECLARE scores : ARRAY[1:5] OF INTEGER
DECLARE total : INTEGER
DECLARE i : INTEGER
total ← 0

FOR i ← 1 TO 5
  INPUT scores[i]
NEXT i

FOR i ← 1 TO 5
  total ← total + scores[i]
NEXT i
OUTPUT "Total: ", total

2D Arrays

A 2D array is essentially a table (matrix) — an array of arrays. Elements are accessed using two indices: [row, column].

// Declare a 3x4 grid (3 rows, 4 columns)
DECLARE grid : ARRAY[1:3, 1:4] OF INTEGER

// Access element at row 2, column 3
grid[2, 3] ← 99
OUTPUT grid[1, 4]

Visualising a 2D Array — grid[1:3, 1:4]

[,1]
[,2]
[,3]
[,4]
[1,]
10
20
30
40
[2,]
50
60
99
80
[3,]
90
70
55
35

Highlighted cell: grid[2,3] = 99

Iterating Through a 2D Array

Nested FOR loops are needed — outer loop for rows, inner loop for columns:

FOR row ← 1 TO 3
  FOR col ← 1 TO 4
    OUTPUT grid[row, col]
  NEXT col
NEXT row

Common Array Uses

Use caseExample
Storing a list of valuesStudent marks, temperatures across a week
Implementing searchingLinear search or binary search over a sorted array
Implementing sortingBubble sort or merge sort in-place on an array
Representing a gridChess board, seating plan, pixel image
Lookup tablesASCII table, conversion table
Frequency countingCount occurrences of each score
Exam tip: In Cambridge 9618, the exact array declaration syntax is: DECLARE name : ARRAY[lower:upper] OF type. Two common marks lost: (1) writing ARRAY[1..5] instead of ARRAY[1:5]; (2) missing the OF keyword. For 2D arrays, a comma separates dimensions: ARRAY[1:3, 1:4]. The number of elements is always (upper − lower + 1) per dimension.
⚠️ Common Mistakes
  • Using .. instead of : in array bounds — Cambridge uses ARRAY[1:10] not ARRAY[1..10]
  • Forgetting OF type in the declaration
  • Off-by-one errors — for ARRAY[1:10], valid indices are 1 to 10 (not 0 to 9)
  • Assuming arrays grow dynamically — Cambridge arrays are fixed-size at declaration
  • In 2D arrays, confusing row and column — Cambridge uses [row, col] order
  • Not using separate variables for row and column indices in nested loops
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.2.2 Arrays

8 questions · Cambridge 9618 standard

Q1Write Cambridge 9618 pseudocode to declare a 1D array called temps with 7 elements (indexed 1 to 7) of type REAL.[2]
✅ Mark scheme
DECLARE temps : ARRAY[1:7] OF REAL [2]. (Lose 1 mark if OF REAL missing or bounds use .. instead of :.)
Q2How many elements does an array declared as ARRAY[3:8] OF INTEGER contain?[1]
✅ Mark scheme
6 elements [1]. Formula: upper − lower + 1 = 8 − 3 + 1 = 6.
Q3Write Cambridge 9618 pseudocode to find the maximum value in a 1D integer array A with indices 1 to n. The maximum should be stored in a variable called maxVal.[5]
✅ Mark scheme
DECLARE maxVal : INTEGER [1]; maxVal ← A[1] [1]; FOR i ← 2 TO n [1]; IF A[i] > maxVal THEN maxVal ← A[i] ENDIF [1]; NEXT i [1].
Q4Write Cambridge 9618 pseudocode to declare a 2D array called seats representing a cinema hall with 10 rows and 15 columns of BOOLEAN values (TRUE = occupied).[2]
✅ Mark scheme
DECLARE seats : ARRAY[1:10, 1:15] OF BOOLEAN [2]. (Lose 1 if OF BOOLEAN missing, bounds wrong, or syntax error.)
Q5Write the pseudocode to calculate the sum of all elements in a 2D INTEGER array M with dimensions ARRAY[1:3, 1:4].[5]
✅ Mark scheme
DECLARE total : INTEGER [1]; total ← 0 [1]; FOR row ← 1 TO 3 [1]; FOR col ← 1 TO 4 [1]; total ← total + M[row, col]; NEXT col; NEXT row; OUTPUT total [1].
Q6State two advantages of using an array over separate individual variables to store 20 student marks.[2]
✅ Mark scheme
Any two: all marks can be processed with a loop (FOR i ← 1 TO 20) instead of repeating the same code 20 times [1]; a single array name is easier to manage than 20 separate variable names [1]; searching, sorting and statistical operations (sum, average, max) are straightforward with a loop [1].
Q7Trace the binary search algorithm on the sorted array [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] (1-indexed) searching for the value 23. Show the values of Low, High, and Mid at each step, and state the number of comparisons made before finding the value.[5]
✅ Mark scheme
Step 1: Low=1, High=10, Mid=(1+10) DIV 2=5 → List[5]=16 < 23, so Low=6 [1]; Step 2: Low=6, High=10, Mid=(6+10) DIV 2=8 → List[8]=56 > 23, so High=7 [1]; Step 3: Low=6, High=7, Mid=(6+7) DIV 2=6 → List[6]=23 = Target → found at index 6 [1]; 3 comparisons made [1]; Award 1 mark for correct identification that Mid is calculated as (Low+High) DIV 2 [1].
Q8A binary search is performed on a sorted array of 256 elements. State the maximum number of comparisons needed to find any element, showing your working. If the array grows to 1,024 elements, state the new maximum. Explain why doubling the array size does not double the maximum comparisons needed.[4]
✅ Mark scheme
Maximum comparisons = ceiling(log₂(n)) + 1 or equivalent; log₂(256) = 8, so maximum = 8 comparisons [1]; log₂(1024) = 10, so maximum = 10 comparisons [1]; Doubling from 256 to 512 adds only 1 comparison; doubling from 512 to 1024 adds only 1 more — the maximum grows logarithmically, not linearly [1]; this is the nature of O(log n) complexity — each comparison halves the search space, so the number of steps needed increases very slowly as n grows [1].
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 6
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 2.2.2 Arrays

10 questions · 10 marks · 10 minutes

← 2.2.1 Data Types
38 of 82 · Cambridge 9618
2.2.3 Records & Files →