DECLARE arr : ARRAY[lowerBound..upperBound] OF TYPE
IF i < lowerBound OR i > upperBound THEN
OUTPUT "Index out of range!"
STOP
END IF
General declaration syntax:
DECLARE: ARRAY[lowerBound..upperBound] OF
Example – an integer array for 10 test scores:
DECLARE scores : ARRAY[1..10] OF INTEGER
All elements receive a default value (0 for numbers, “” for strings, FALSE for booleans). If a different start value is required, initialise explicitly (see the templates in §5).
value ← array[i]array[i] ← valueFOR i ← lowerBound TO upperBound DO
array[i] ← array[i] + 5
END FOR
| Operation | Pseudocode Template |
|---|---|
Initialise all elements to a specific value v |
FOR i ← lowerBound TO upperBound DO
array[i] ← v
END FOR |
| Sum of all elements | SET sum ← 0
FOR i ← lowerBound TO upperBound DO
sum ← sum + array[i]
END FOR |
| Find maximum value | SET max ← array[lowerBound]
FOR i ← lowerBound+1 TO upperBound DO
IF array[i] > max THEN
SET max ← array[i]
END IF
END FOR |
Linear search for a value x |
SET found ← FALSE
FOR i ← lowerBound TO upperBound DO
IF array[i] = x THEN
SET found ← TRUE
EXIT FOR
END IF
END FOR |
Binary search for a value x (array must be sorted ascending) |
SET low ← lowerBound
SET high ← upperBound
SET found ← FALSE
WHILE low ≤ high AND NOT found DO
SET mid ← (low + high) DIV 2
IF array[mid] = x THEN
SET found ← TRUE
ELSEIF array[mid] < x THEN
SET low ← mid + 1
ELSE
SET high ← mid - 1
END IF
END WHILE |
| Bubble sort (ascending) | FOR pass ← lowerBound TO upperBound-1 DO
FOR i ← lowerBound TO upperBound-pass DO
IF array[i] > array[i+1] THEN
SET temp ← array[i]
SET array[i] ← array[i+1]
SET array[i+1] ← temp
END IF
END FOR
END FOR |
| Selection sort (ascending) | FOR i ← lowerBound TO upperBound-1 DO
SET minIdx ← i
FOR j ← i+1 TO upperBound DO
IF array[j] < array[minIdx] THEN
SET minIdx ← j
END IF
END FOR
IF minIdx ≠ i THEN
SET temp ← array[i]
SET array[i] ← array[minIdx]
SET array[minIdx] ← temp
END IF
END FOR |
| Insertion sort (ascending) | FOR i ← lowerBound+1 TO upperBound DO
SET key ← array[i]
SET j ← i-1
WHILE j ≥ lowerBound AND array[j] > key DO
SET array[j+1] ← array[j]
SET j ← j-1
END WHILE
SET array[j+1] ← key
END FOR |
Insert value v at position p (right‑shift) |
FOR i ← upperBound DOWNTO p+1 DO
array[i] ← array[i-1]
END FOR
SET array[p] ← v |
Delete element at position p (left‑shift) |
FOR i ← p TO upperBound-1 DO
array[i] ← array[i+1]
END FOR
/* Optional: clear the now‑unused last element */
SET array[upperBound] ← 0 |
Given an array marks[1..n], compute the average and display it to two decimal places.
DECLARE n, i : INTEGER
DECLARE marks : ARRAY[1..100] OF INTEGER
DECLARE total : INTEGER
DECLARE average : REAL
READ n
/* 1️⃣ Input the marks */
FOR i ← 1 TO n DO
READ marks[i]
END FOR
/* 2️⃣ Compute the total */
SET total ← 0
FOR i ← 1 TO n DO
total ← total + marks[i]
END FOR
/* 3️⃣ Calculate and output the average */
SET average ← total / n
OUTPUT "Average = ", average TO 2DP
bits[1..8]. Converting a decimal number to binary is a classic array exercise.
DECLARE n : INTEGER
DECLARE bits : ARRAY[1..8] OF INTEGER
SET n ← 173 /* decimal */
FOR i ← 8 DOWNTO 1 DO
SET bits[i] ← n MOD 2
SET n ← n DIV 2
END FOR
image[1..height][1..width]. A simple greyscale inversion:
FOR r ← 1 TO height DO
FOR c ← 1 TO width DO
SET image[r][c] ← 255 - image[r][c]
END FOR
END FOR
FOR i ← lowerBound TO upperBound DO
SEND array[i] TO destination
END FOR
RECEIVE.address = baseAddress + (i‑lowerBound) × elementSize.SET array[i] ← array[i] AND 0x0F /* keep lower 4 bits */
FOR i ← lowerBound TO upperBound DO
array[i] ← 0
END FOR
DECLARE student : RECORD
id : INTEGER
name : STRING
mark : INTEGER
END RECORD
DECLARE students : ARRAY[1..30] OF student
students array.DECLARE weights : ARRAY[1..n] OF REAL
/* initialise */
FOR i ← 1 TO n DO
SET weights[i] ← 0.0
END FOR
n).sum, total) before re‑using them in a new pass.lowerBound, upperBound and data type.n) fits within the declared bounds.a[1..n] in place.temps[1..30] containing daily temperatures, write pseudocode to find the number of days with temperature above the monthly average.scores[1..m] and output the sorted list.p from an array data[1..k] and adjust the logical size accordingly.bits[1..8] (most‑significant bit first).image[1..h][1..w].values[1..n] over a network, including a basic error‑check for missing elements.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.