Arrays
One name holding many values, so a loop can work through them.
RUN THE PSEUDOCODE
Write the pseudocode your syllabus uses and press Run. Assignment is the arrow — type <- and it works the same as ←. Both syllabuses are accepted, so OUTPUT and PRINT both work; where a word differs from the one the exam prints, the runner says so.
Press Run to see what it doesTotal ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
//Declaring arrays in Cambridge pseudocode
You state the bounds and the type. A 2-D array is indexed [row, column].
DECLARE Scores : ARRAY[1:30] OF INTEGER
DECLARE Grid : ARRAY[1:5, 1:5] OF CHAR
Scores[1] ← 78
Grid[2, 3] ← 'x'
FOR i ← 1 TO 30
OUTPUT Scores[i]
NEXT i//Linear search
Check each element in turn until you find the target or run out. Simple, works on unsorted data, but slow on large arrays.
Found ← FALSE
FOR i ← 1 TO 30
IF Scores[i] = Target THEN
Found ← TRUE
Position ← i
ENDIF
NEXT i//Bubble sort
Repeatedly compare neighbouring pairs and swap them if they are the wrong way round. After each pass the largest remaining value has 'bubbled' to the end.
FOR Pass ← 1 TO 29
FOR i ← 1 TO 29
IF Scores[i] > Scores[i+1] THEN
Temp ← Scores[i]
Scores[i] ← Scores[i+1]
Scores[i+1] ← Temp
ENDIF
NEXT i
NEXT PassCHECK YOURSELF
1.An array is DECLARE A : ARRAY[1:10] OF INTEGER. How many values can it hold?
2.Why does swapping two array elements need a third variable?