Write clear, correct pseudocode for declaring, initialising, accessing and processing one‑dimensional (1‑D) and two‑dimensional (2‑D) arrays. Relate arrays to data types, records, abstract data types (ADTs), testing, and to the broader Cambridge syllabus topics on representation, hardware, communication and system software.
INTEGER, REAL, CHAR, BOOLEAN.FOR loops and IF statements.| Syllabus Block | Relevance to Arrays |
|---|---|
| 1 Information representation | Arrays store binary data; character arrays rely on ASCII/Unicode codes; compression can be applied to large arrays. |
| 2 Communication | Arrays are often transmitted over networks (e.g., sending a sensor data buffer). |
| 3 Hardware | Contiguous memory layout, address calculation, registers and bus transfers. |
| 4 Processor fundamentals | Bit‑wise operations for packing/unpacking values in arrays. |
| 5 System software | File I/O for persisting arrays; OS services for memory allocation. |
DEC = 45
BIN = 0010 1101
HEX = 2D
Conversion rule: address = base_address + index × word_size (see “Memory layout” below).
CHAR occupies one byte (ASCII 0‑127) or more (Unicode). When declaring a CHAR array, remember that each element stores the numeric code point.
INPUT : 5 5 5 2 2 9 9 9 9
OUTPUT : (5,3) (2,2) (9,4) // value, count pairs
Use when the question asks to “compress an array before transmission”.Arrays occupy a contiguous block of memory. The address of element A[i] is:
address = base_address + i × word_size
For a 2‑D array M[ROWS][COLS] stored row‑major:
address = base_address + (row × COLS + col) × word_size
| Example | Base address | Word size | Index calculation |
|---|---|---|---|
scores[5] |
0x1000 | 4 bytes (INTEGER) | address(scores[i]) = 0x1000 + i×4 |
matrix[3][4] |
0x2000 | 4 bytes | address(matrix[r][c]) = 0x2000 + (r×4 + c)×4 |
packed ← (highByte << 8) OR lowByte
highByte ← (packed >> 8) AND 0xFF
lowByte ← packed AND 0xFF
When an array is sent over a network the following steps are typical:
PROCEDURE writeArrayToFile(arr, size, filename)
OPEN file FOR WRITE AS f
WRITE f, size // first write the length
FOR i FROM 0 TO size‑1
WRITE f, arr[i]
END FOR
CLOSE f
END PROCEDURE
PROCEDURE readArrayFromFile(filename) RETURNS (array, size)
OPEN file FOR READ AS f
READ f, size
DECLARE array[size] AS INTEGER
FOR i FROM 0 TO size‑1
READ f, array[i]
END FOR
CLOSE f
RETURN (array, size)
END PROCEDURE
These procedures illustrate the OS service (file handling) that underpins persistent storage of arrays.
0 ≤ index < size. Accessing outside this range causes a run‑time error.
CONST N ← 5 // size of a 1‑D array
CONST ROWS← 3 // rows in a 2‑D array
CONST COLS← 4 // columns in a 2‑D array
DECLARE scores[N] AS INTEGER // 1‑D array
FOR i FROM 0 TO N‑1
scores[i] ← 0 // initialise all elements to 0
END FOR
DECLARE matrix[ROWS][COLS] AS INTEGER // 2‑D array
FOR row FROM 0 TO ROWS‑1
FOR col FROM 0 TO COLS‑1
matrix[row][col] ← 0
END FOR
END FOR
scores[0] ← 85 scores[1] ← 92 scores[2] ← 78 scores[3] ← 90 scores[4] ← 88 matrix[0][0] ← 1 matrix[0][1] ← 2 matrix[0][2] ← 3 matrix[0][3] ← 4 matrix[1][0] ← 5 matrix[1][1] ← 6 matrix[1][2] ← 7 matrix[1][3] ← 8 matrix[2][0] ← 9 matrix[2][1] ←10 matrix[2][2] ←11 matrix[2][3] ←12
PRINT "Score at index 2: ", scores[2] PRINT "Element at (row 1, col 2): ", matrix[1][2]
sum ← 0
FOR i FROM 0 TO N‑1
sum ← sum + scores[i]
END FOR
average ← sum / N
PRINT "Total =", sum, "Average =", average
FOR row FROM 0 TO ROWS‑1
sum ← 0
FOR col FROM 0 TO COLS‑1
sum ← sum + matrix[row][col]
END FOR
rowAvg ← sum / COLS
PRINT "Average of row ", row, ": ", rowAvg
END FOR
DECLARE temp AS INTEGER
FOR i FROM 0 TO 2
FOR j FROM i+1 TO 2
temp ← matrix[i][j]
matrix[i][j] ← matrix[j][i]
matrix[j][i] ← temp
END FOR
END FOR
CONST THRESHOLD ← 80
count ← 0
FOR i FROM 0 TO N‑1
IF scores[i] > THRESHOLD THEN
count ← count + 1
END IF
END FOR
PRINT "Number of scores > ", THRESHOLD, ": ", count
0 ≤ index < size before accessing.
DECLARE target AS INTEGER
READ target
found ← FALSE
FOR i FROM 0 TO N‑1
IF scores[i] = target THEN
PRINT "Target found at index ", i
found ← TRUE
EXIT FOR
END IF
END FOR
IF NOT found THEN
PRINT "Target not in array."
END IF
FOR pass FROM 1 TO N‑1
FOR i FROM 0 TO N‑pass‑1
IF scores[i] > scores[i+1] THEN
// swap
temp ← scores[i]
scores[i] ← scores[i+1]
scores[i+1] ← temp
END IF
END FOR
END FOR
PRINT "Array sorted in ascending order."
CONST MAX ← 10
DECLARE stack[MAX] AS INTEGER
top ← -1 // empty stack
PROCEDURE push(value)
IF top = MAX‑1 THEN
PRINT "Stack overflow"
ELSE
top ← top + 1
stack[top] ← value
END IF
END PROCEDURE
PROCEDURE pop() RETURNS INTEGER
IF top = -1 THEN
PRINT "Stack underflow"
RETURN NULL
ELSE
value ← stack[top]
top ← top - 1
RETURN value
END IF
END PROCEDURE
CONST MAX ← 10
DECLARE queue[MAX] AS INTEGER
front ← 0
rear ← -1
size ← 0
PROCEDURE enqueue(value)
IF size = MAX THEN
PRINT "Queue full"
ELSE
rear ← (rear + 1) MOD MAX
queue[rear] ← value
size ← size + 1
END IF
END PROCEDURE
PROCEDURE dequeue() RETURNS INTEGER
IF size = 0 THEN
PRINT "Queue empty"
RETURN NULL
ELSE
value ← queue[front]
front ← (front + 1) MOD MAX
size ← size - 1
RETURN value
END IF
END PROCEDURE
DECLARE TYPE StudentRecord
name AS STRING(20)
age AS INTEGER
mark AS INTEGER
END TYPE
CONST STUDENTS ← 3
DECLARE class[STUDENTS] AS StudentRecord
// initialise
FOR i FROM 0 TO STUDENTS‑1
class[i].name ← ""
class[i].age ← 0
class[i].mark ← 0
END FOR
Always perform a dry‑run with a small data set before coding. The table below shows a step‑by‑step trace of the “Row‑wise average” algorithm for the 3 × 4 matrix defined earlier.
| Step | row | col | sum (so far) | rowAvg (after inner loop) |
|---|---|---|---|---|
| 1 | 0 | 0 | 1 | |
| 2 | 0 | 1 | 3 | |
| 3 | 0 | 2 | 6 | |
| 4 | 0 | 3 | 10 | 10/4 = 2.5 |
| 5 | 1 | 0 | 5 | |
| 6 | 1 | 1 | 11 | |
| 7 | 1 | 2 | 18 | |
| 8 | 1 | 3 | 26 | 26/4 = 6.5 |
| 9 | 2 | 0 | 9 | |
| 10 | 2 | 1 | 19 | |
| 11 | 2 | 2 | 30 | |
| 12 | 2 | 3 | 42 | 42/4 = 10.5 |
| Row \ Col | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 |
| 1 | 5 | 6 | 7 | 8 |
| 2 | 9 | 10 | 11 | 12 |
A[i], where 0 ≤ i < n.M[i][j], where 0 ≤ i < r and 0 ≤ j < c.size‑1. Use constants and loop to size‑1.array[row][col] for 2‑D arrays; never swap unintentionally.IF 0 ≤ i < size THEN … END IF before any access, or comment “// assume i is in range”.CONST N ← 5).M[ROWS][COLS], write pseudocode to calculate the sum of all elements.push and pop procedures with overflow/underflow handling.{85, 92, 78, 90, 88} searching for 90. Show the values of the loop variable and the found flag at each step.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.