Analyse a problem, select the most suitable array structure (1‑D or 2‑D) and justify the choice.
DECLARE identifier[lower‑bound … upper‑bound] : TYPEsize‑1.
| Term | Definition (Cambridge) | Typical Use in Pseudocode |
|---|---|---|
| Lower‑bound | Smallest valid index (always 0 in Cambridge exams). | FOR i ← 0 TO n‑1 |
| Upper‑bound | Largest valid index; for an array of size n it is n‑1. | FOR i ← 0 TO n‑1 |
| Index | Integer used to locate an element; written as i (1‑D) or i, j (2‑D). | value ← array[i] or value ← matrix[i][j] |
| Size | Number of elements an array can hold. For a 2‑D array the size is rows × columns. | DECLARE scores[0…9] : INTEGER // size = 10 |
DECLARE temps[0…29] : REAL // 1‑D array, 30 elements
DECLARE marks[0…29][0…5] : INTEGER // 2‑D array, 30×6 elements
double[] temps = new double[30]; // 1‑D
int[][] marks = new int[30][6]; // 2‑D
# 1‑D
temps = [0.0] * 30
# 2‑D (list of lists)
marks = [[0] * 6 for _ in range(30)]
Each declaration mirrors the pseudocode, helping you translate between the exam language and a real programming language.
FOR i ← 1 TO n) – Cambridge always expects 0‑based.size‑1) when writing loop limits.DECLARE a[0…9] instead of DECLARE a[0…9] : INTEGER).| Checklist Item (What you must analyse) | AO2 Requirement (Syllabus wording) | Guiding Question |
|---|---|---|
| Identify the logical dimensions of the data. | “Analyse the problem and decide the most suitable array structure.” | Is the data a simple list or a table? |
| Determine the number of indices needed to locate an element. | “Select the most suitable array type and justify the choice.” | Can every item be described with one number (day, position) or do you need a pair (row & column)? |
| Consider future extensions or alternative processing. | “Justify the choice of array structure.” | Will you ever need to treat the data as a linear list (flatten) or as a grid? |
| Check memory and performance implications. | “Analyse the problem and select the most suitable array structure.” | Does the extra dimension add unnecessary mental overhead? |
| Criterion | 1‑D Array | 2‑D Array |
|---|---|---|
| Indexing | Single index i |
Two indices i, j |
| Typical Use‑case | Lists, queues, simple vectors | Grids, matrices, tables |
| Memory layout | Linear block of n elements | Linear block of rows × columns (row‑major or column‑major) |
| Complexity of access | O(1) using array[i] |
O(1) using matrix[i][j] (after offset calculation) |
| Declaration (Java) | int[] scores = new int[10]; |
int[][] board = new int[8][8]; |
// Safe access pattern (pseudocode)
IF i < 0 OR i ≥ size THEN
OUTPUT "Error: index out of bounds"
ELSE
// read or write here
END IF
// Pseudocode – increase the 5th temperature by 2.5°C
IF 4 >= 0 AND 4 < 30 THEN
temps[4] ← temps[4] + 2.5
END IF
// Java
if (4 >= 0 && 4 < temps.length) {
temps[4] = temps[4] + 2.5;
}
// 1‑D – print all temperatures
FOR i ← 0 TO 29 DO
OUTPUT temps[i]
END FOR
// 2‑D – row‑wise traversal of a 5×4 scores table
FOR i ← 0 TO 4 DO
FOR j ← 0 TO 3 DO
OUTPUT scores[i][j] + " "
END FOR
OUTPUT NEWLINE
END FOR
// Find the first occurrence of 85 in a marks array
DECLARE position ← -1
FOR i ← 0 TO n‑1 DO
IF marks[i] = 85 THEN
position ← i
EXIT FOR
END IF
END FOR
// position = -1 means “not found”
FOR pass ← 0 TO n‑2 DO
FOR i ← 0 TO n‑2‑pass DO
IF array[i] > array[i+1] THEN
// swap
temp ← array[i]
array[i] ← array[i+1]
array[i+1] ← temp
END IF
END FOR
END FOR
// Add 5 bonus points to every player’s score in game 2 (column index 1)
FOR i ← 0 TO rows‑1 DO
scores[i][1] ← scores[i][1] + 5
END FOR
A 2‑D array can be simulated with a 1‑D array using a conversion formula. This is useful when a language only supports 1‑D structures or when an API expects a flat list.
k = i × n + ji = k ÷ n, j = k mod n// Pseudocode – accessing element (i, j) in a flattened 1‑D array
k ← i * n + j
value ← flatArray[k]
Task: Store the temperature for each of the 30 days in a month.
// Pseudocode
DECLARE temps[0…29] : REAL
FOR d ← 0 TO 29 DO
INPUT temps[d]
END FOR
Justification: Each temperature is identified by a single day number → 1‑D array.
// Java declaration
int[][] marks = new int[30][6]; // marks[student][subject]
// Row‑wise input
FOR s ← 0 TO 29 DO
FOR sub ← 0 TO 5 DO
INPUT marks[s][sub]
END FOR
END FOR
Justification: Data forms a table (students × subjects) → 2‑D array.
DECLARE flat[0…11] : INTEGER // 3×4 = 12 elements
DECLARE rows ← 3, cols ← 4
// Set element (2,1) to 99 (row‑major)
k ← 2 * cols + 1 // k = 2*4 + 1 = 9
flat[k] ← 99
75 in a 1‑D array called scores of size n. Include bounds checking.10. Use pseudocode.DECLARE marks[0…29][0…5] : INTEGERint[][] marks = new int[30][6];k = i * cols + j.
// Access (i, j) in a flat array
k ← i * cols + j
value ← flatArray[k]
DECLARE position ← -1
FOR i ← 0 TO n‑1 DO
IF i < 0 OR i ≥ n THEN
OUTPUT "Index error"
EXIT FOR
END IF
IF scores[i] = 75 THEN
position ← i
EXIT FOR
END IF
END FOR
FOR i ← 0 TO 2 DO // rows
FOR j ← 0 TO 3 DO // columns
matrix[i][j] ← matrix[i][j] + 10
END FOR
END FOR
size‑1 in Cambridge exams.Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.