Write values into, and read values from, an array using iteration (count‑controlled FOR loops and pre‑condition WHILE loops). Include both one‑dimensional and two‑dimensional arrays, demonstrate correct handling of array bounds and show how to validate user‑supplied indices.
| Syllabus Requirement | How the note satisfies it |
|---|---|
| Topic 8.2 – Arrays (declare, write, read, 1‑D & 2‑D, lower‑bound convention) | Declarations for 1‑D and 2‑D arrays, explicit lower‑bound tables, write/read examples with FOR and WHILE loops, and index‑validation. |
| Topic 8.1 – Programming concepts (variables, constants, data types, I/O, sequence, selection, iteration, operators) | Quick‑reference box, keyword‑to‑syllabus mapping, and a simple sequence example. |
| Iteration structures (count‑controlled, pre‑condition, post‑condition) | Side‑by‑side comparison table, examples of FOR, WHILE and REPEAT … UNTIL. |
| Array bounds & validation (AO2) | Two validation snippets – one simple check, one repeat‑until‑valid – each labelled “AO2”. |
| Algorithm design & trace tables (AO2 & AO3) | Step‑by‑step procedure, full trace table for a student‑marks program, and guidance on dry‑run. |
DECLARE … AS INTEGER | REAL | STRINGCONSTANT … ← …INPUT … OUTPUT …FOR i ← lower TO upper DO … END FORWHILE condition DO … END WHILEREPEAT … UNTIL conditionIF … THEN … ELSE … END IF← Arithmetic: +, –, *, /, MOD Relational: =, ≠, <, ≤, >, ≥ Logical: AND, OR, NOT
DECLARE – variables & data typesCONSTANT – constantsINPUT / OUTPUT – I/OIF … THEN … ELSE – selectionFOR, WHILE, REPEAT … UNTIL – iteration (count‑controlled, pre‑condition, post‑condition)DECLARE a ← 5
DECLARE b ← 12
DECLARE sum ← a + b
OUTPUT "The sum is ", sum
This demonstrates the “sequence” part of Topic 8.1 – code executed from top to bottom without any control structures.
| Convention | First valid index | Loop limits for an array of size SIZE |
|---|---|---|
| 0‑based (default in many languages) | 0 | FOR i ← 0 TO SIZE‑1 |
| 1‑based (allowed by Cambridge) | 1 | FOR i ← 1 TO SIZE |
FOR i ← 1 TO SIZE) leads to off‑by‑one errors and runtime “index out of bounds” messages.
| Structure | Syntax (pseudocode) | When to use | Assessment Objective |
|---|---|---|---|
| Count‑controlled | FOR i ← lower TO upper DO … END FOR |
Number of repetitions known before the loop starts (e.g. processing every element of a fixed‑size array). | AO1 – write/read arrays; AO2 – recognise correct bounds. |
| Pre‑condition | WHILE condition DO … END WHILE |
Loop should continue only while a condition remains true; condition tested before each iteration (e.g. sentinel‑controlled input). | AO1 – use of WHILE; AO2 – correct loop termination. |
| Post‑condition | REPEAT … UNTIL condition |
At least one execution is required; condition tested after the body (e.g. menu that must be shown at least once). | AO1 – use of REPEAT … UNTIL; AO2 – proper termination condition. |
CONSTANT SIZE ← 10
DECLARE numbers[SIZE] AS INTEGER
FOR loopFOR i ← 0 TO SIZE‑1 DO
numbers[i] ← (i + 1) * 3 // store the first SIZE multiples of 3
END FOR
FOR loopFOR i ← 0 TO SIZE‑1 DO
OUTPUT numbers[i]
END FOR
CONSTANT CLASS_SIZE ← 5
DECLARE marks[CLASS_SIZE] AS INTEGER
DECLARE i ← 0
WHILE i < CLASS_SIZE DO
OUTPUT "Enter mark for student ", i+1, ": "
INPUT marks[i]
i ← i + 1
END WHILE
DECLARE total ← 0
DECLARE count ← 0
DECLARE n ← -1
REPEAT
OUTPUT "Enter a positive number (0 to stop): "
INPUT n
total ← total + n
count ← count + 1
UNTIL n = 0
DECLARE index ← 7
IF index ≥ 0 AND index < SIZE THEN
OUTPUT "Element = ", numbers[index]
ELSE
OUTPUT "Error: index out of bounds"
END IF
DECLARE idx ← -1
REPEAT
OUTPUT "Enter an index between 0 and ", SIZE-1, ": "
INPUT idx
IF idx < 0 OR idx ≥ SIZE THEN
OUTPUT "Invalid index – try again."
END IF
UNTIL idx ≥ 0 AND idx < SIZE
OUTPUT "Element at index ", idx, " = ", numbers[idx]
CONSTANT ROWS ← 3
CONSTANT COLS ← 4
DECLARE matrix[ROWS][COLS] AS INTEGER
FOR loopsFOR r ← 0 TO ROWS‑1 DO
FOR c ← 0 TO COLS‑1 DO
matrix[r][c] ← (r+1) * (c+1) // simple multiplication table
END FOR
END FOR
FOR loopsFOR r ← 0 TO ROWS‑1 DO
FOR c ← 0 TO COLS‑1 DO
OUTPUT "matrix[", r, "][", c, "] = ", matrix[r][c]
END FOR
END FOR
| Step | Action |
|---|---|
| 1 | Declare the array (or matrix) with a constant size. |
| 2 | Choose a lower‑bound convention (0‑based or 1‑based) and set the loop counter to that lower bound. |
| 3 | Write values: inside the loop assign array[i] ← value (or matrix[r][c] ← value). |
| 4 | If a second loop is needed, reset the counter (or use a new one) before the read phase. |
| 5 | Read values: inside the loop output array[i] (or matrix[r][c]). |
| 6 | When an index may come from the user, include an AO2 validation step before any access. |
array[SIZE] when the highest valid index is SIZE‑1).Class size = 5. The program asks for each mark, stores it, then prints the marks.
CONSTANT CLASS_SIZE ← 5
DECLARE marks[CLASS_SIZE] AS INTEGER
DECLARE i ← 0
// ---- Write phase (FOR loop) ----
FOR i ← 0 TO CLASS_SIZE‑1 DO
OUTPUT "Enter mark for student ", i+1, ": "
INPUT marks[i]
END FOR
// ---- Read phase (FOR loop) ----
OUTPUT "Marks entered:"
FOR i ← 0 TO CLASS_SIZE‑1 DO
OUTPUT "Student ", i+1, ": ", marks[i]
END FOR
| Iteration (i) | Input value | marks[i] after assignment |
|---|---|---|
| 0 | 78 | 78 |
| 1 | 85 | 85 |
| 2 | 92 | 92 |
| 3 | 67 | 67 |
| 4 | 73 | 73 |
Students should produce a similar table for the read phase, showing the values displayed on each iteration.
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.