Paper 2: Pseudocode step by step (arrays, functions, procedures)
Arrays 5: parallel arrays, copying and arrays in modules
What you need to be able to do
- 10.2Use parallel arrays: the same index in two arrays describes the same thing.
- 10.2Copy or filter items from one array into another, with a separate index for each array.
- 11.3Use a global array inside a procedure or function, and pass an array as a parameter (BYVAL or BYREF).
You need: Arrays 4 (swap and bubble sort) and Functions and procedures (parameters, BYREF).
Pattern 9: parallel arrays
An array holds one data type. To store a name and a mark for each student, Paper 2 often uses two arrays side by side: Name[i] and Mark[i] belong to the same student. They are called parallel arrays. The index is the link.
| index | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Name | "Ali" | "Bina" | "Cyrus" | "Dua" | "Eman" | "Faris" |
| index | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Mark | 64 | 81 | 47 | 81 | 92 | 55 |
Work out the INDEX using one array, then use that same index in the other. Pattern 3 already gave you the index of the highest value - here that is exactly what is needed.
Use it when the question says the name of the student with …, each … has a corresponding …, the array … and the array … are used together
Best ← 1FOR i ← 2 TO 6 IF Mark[i] > Mark[Best] THEN Best ← i ENDIFNEXT iOUTPUT Name[Best], " scored ", Mark[Best]Trap Keeping only the highest MARK (Max ← Mark[i]) loses the index, so you can't find the name. Keep the index.
Sorting parallel arrays
If you sort Mark and forget Name, every mark ends up next to the wrong student. Whenever two elements of one array swap, the same two elements of the other array must swap too. That needs a Temp of each data type.
FOR i ← 1 TO 5 FOR j ← 1 TO 6 - i IF Mark[j] < Mark[j + 1] THEN TempMark ← Mark[j] Mark[j] ← Mark[j + 1] Mark[j + 1] ← TempMark TempName ← Name[j] Name[j] ← Name[j + 1] Name[j + 1] ← TempName ENDIF NEXT jNEXT i| index | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Name | "Eman" | "Bina" | "Dua" | "Ali" | "Faris" | "Cyrus" |
| index | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Mark | 92 | 81 | 81 | 64 | 55 | 47 |
Result : ARRAY[1:30, 1:2] OF STRING with the name in column 1 and the mark in column 2 (see the 2D arrays page). And an array of RECORDS is a third way. Read which one the question uses.Pattern 10: copy or filter into a second array
To copy only some elements into another array (the passes, the names starting with A, the used elements), you need two indexes: i walks through the source array, and k says where the next item goes in the target. k only moves when something is copied, so the target fills from the front with no gaps.
| i | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Mark | 64 | 81 | 47 | 81 | 92 | 55 |
| k | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Passed | "Ali" | "Bina" | "Dua" | "Eman" | "Faris" | "" |
i goes through every element of the source; k counts what has been copied and is also the position of the last copied item. At the end, k is how many were copied.
Use it when the question says copy … into, store … in a second array, create a new array containing only …, remove the gaps
k ← 0FOR i ← 1 TO 6 IF Mark[i] >= 50 THEN k ← k + 1 Passed[k] ← Name[i] ENDIFNEXT iOUTPUT k, " passed"Trap Using i for the target too (Passed[i] ← Name[i]) copies them into the SAME places - the gaps stay.
See the trace table for pattern 10
| i | Mark[i] | k |
|---|---|---|
| 1 | 64 | 1 |
| 2 | 81 | 2 |
| 3 | 47 | |
| 4 | 81 | 3 |
| 5 | 92 | 4 |
| 6 | 55 | 5 |
k stays at 2 on the row where i = 3: Cyrus is skipped.
Arrays in procedures and functions
Most Paper 2 scenario questions say “a global array … is used”. A global array is declared in the main program, and every module can read and change it directly - it is not passed as a parameter. That is what the earlier pages did.
Sometimes the question says the array is passed as a parameter instead. Then the module gets its own name for it, and the parameter's type is written ARRAY OF INTEGER (no bounds).
FUNCTION CountAbove(Values : ARRAY OF INTEGER, Limit : INTEGER) RETURNS INTEGER DECLARE i, Count : INTEGER Count ← 0 FOR i ← 1 TO 6 IF Values[i] > Limit THEN Count ← Count + 1 ENDIF NEXT i RETURN CountENDFUNCTION PROCEDURE AddBonus(BYREF Values : ARRAY OF INTEGER, BYVAL Bonus : INTEGER) DECLARE i : INTEGER FOR i ← 1 TO 6 Values[i] ← Values[i] + Bonus NEXT iENDPROCEDURE // main programOUTPUT CountAbove(Mark, 55)CALL AddBonus(Mark, 5)OUTPUT CountAbove(Mark, 55)| The array is … | What the module can do | How to write it |
|---|---|---|
| global | read and change it directly | use its name, e.g. Mark[i] - no parameter |
| passed BYVAL (the default) | work on a copy: changes are lost when the module ends | Values : ARRAY OF INTEGER |
| passed BYREF | change the caller's array | BYREF Values : ARRAY OF INTEGER |
Run AddBonus with BYVAL instead of BYREF and the second line outputs 4 again: the bonus was added to a copy and thrown away. So: to change an array that was passed in, pass it BYREF.
AddBonus(BYREF Values : ARRAY OF INTEGER, Bonus : INTEGER) Bonus would be BYREF too, so a call like CALL AddBonus(Mark, 5) is wrong (5 is not a variable). Write BYVAL Bonus, or put the BYREF parameters last.DECLARE Mark : ARRAY[1:6] OF INTEGER inside a procedure makes a new, empty local array that hides the global one).Worked example: a class results system
A typical last question, cut down: parallel global arrays, a filter into a third array, and a module that uses another module.
| Module | Description |
|---|---|
CountMerit() |
|
ShowMerit() |
|
Example 1 · CountMerit() and ShowMerit() (own example) ★★★ Hard
Global parallel arrays Name : ARRAY[1:30] OF STRING and Mark : ARRAY[1:30] OF INTEGER hold the class. An unused element of Name contains an empty string. A third global array Merit : ARRAY[1:30] OF STRING is used for the results. Write pseudocode for the two modules in the table above.
CountMerit is pattern 10 (filter with two indexes) plus pattern 9 (the mark decides, the name is copied). Then it tidies up the rest of Merit, which may still hold old names. ShowMerit just calls it and prints what it made.
- copies the name … whose mark is at leasttest Mark[i], copy Name[i] - the same i
- from Merit[1] with no gapsa second index k for Merit
- sets every remaining element … to an empty stringa second loop from k + 1 to 30
- returns the number of names copiedRETURN k
- uses CountMerit()call it once, keep its answer in a variable
- 1CountMerit → INTEGER
- 2ShowMerit
- 30elements in each array
- ""an unused Name element
- An unused student has Name[i] = "" - skip it, whatever its Mark holds.
- If no names are copied, k = 0 and the tidy-up loop runs from 1 to 30: the whole of Merit is cleared.
- FindCountMerit(Threshold)
- Work out firstthe filter loop: test Name[i] <> "" AND Mark[i] >= Threshold; k ← k + 1; Merit[k] ← Name[i]
- Thenclear Merit[k + 1 … 30]
- Trapcalling CountMerit twice in ShowMerit (once to test, once to loop) - store the result in n
Why clear the rest of Merit?
It may still hold names from an earlier call, and the description says the rest must be empty.
Why does ShowMerit loop to n, not 30?
Only the first n elements were filled this time.
5 Now solve it have a go on paper first, then open the worked solution
- CountMerit: k ← 0; FOR i ← 1 TO 30: if Name[i] <> "" AND Mark[i] >= Threshold then k ← k + 1, Merit[k] ← Name[i].
- FOR i ← k + 1 TO 30: Merit[i] ← "". RETURN k.
- ShowMerit: n ← CountMerit(Threshold); if n = 0 output “No students”, else output Merit[1 … n] then “n students”.
FUNCTION CountMerit(Threshold : INTEGER) RETURNS INTEGER DECLARE i, k : INTEGER k ← 0 FOR i ← 1 TO 30 IF Name[i] <> "" AND Mark[i] >= Threshold THEN k ← k + 1 Merit[k] ← Name[i] ENDIF NEXT i FOR i ← k + 1 TO 30 Merit[i] ← "" NEXT i RETURN kENDFUNCTION PROCEDURE ShowMerit(Threshold : INTEGER) DECLARE i, n : INTEGER n ← CountMerit(Threshold) IF n = 0 THEN OUTPUT "No students" ELSE FOR i ← 1 TO n OUTPUT Merit[i] NEXT i OUTPUT n, " students" ENDIFENDPROCEDURETested with the six students above: threshold 80 gives Bina, Dua, Eman and “3 students”, and clears the old names; 95 gives “No students”.
Practice: three levels
L1 Basic
Look up, filter to output, plain copy.
L1.1 Look up a mark
Using the parallel arrays Name and Mark (6 students, all used), input a name and output that student's mark, or Not found.
Show the model answer
DECLARE Who : STRINGDECLARE Pos : INTEGERINPUT WhoPos ← 0FOR i ← 1 TO 6 IF Name[i] = Who THEN Pos ← i ENDIFNEXT iIF Pos = 0 THEN OUTPUT "Not found"ELSE OUTPUT Mark[Pos]ENDIFSearch Name for the index, then read Mark at that index. (Names are unique here, so carrying on after finding it is harmless.)
L1.2 Everyone above 60
Output the name of every student whose mark is above 60.
Show the model answer
FOR i ← 1 TO 6 IF Mark[i] > 60 THEN OUTPUT Name[i] ENDIFNEXT iTest one array, output the other, same i.
L1.3 Copy everything
Copy all 6 elements of Mark into a second array Backup : ARRAY[1:6] OF INTEGER.
Show the model answer
FOR i ← 1 TO 6 Backup[i] ← Mark[i]NEXT iA plain copy keeps the positions, so one index is enough. You need two indexes only when you leave items out.
L2 Medium
Index of the lowest, filter into a new array, an array parameter.
L2.1 Lowest scorer
Output the name of the student with the lowest mark. If two share it, output the first.
Show the model answer
DECLARE Low : INTEGERLow ← 1FOR i ← 2 TO 6 IF Mark[i] < Mark[Low] THEN Low ← i ENDIFNEXT iOUTPUT Name[Low]Keep the index of the lowest. Using < (not <=) keeps the FIRST of equal marks.
L2.2 Names starting with a letter
Input a letter. Copy every name that starts with it into Found : ARRAY[1:6] OF STRING with no gaps, then output how many were copied.
Show the model answer
DECLARE Letter : CHARINPUT Letterk ← 0FOR i ← 1 TO 6 IF LEFT(Name[i], 1) = Letter THEN k ← k + 1 Found[k] ← Name[i] ENDIFNEXT iOUTPUT kPattern 10 with a string test (LEFT from the strings page).
L2.3 Average of a parameter
Write a function Average() that takes an array of 6 integers as a parameter and returns their average.
Show the model answer
FUNCTION Average(Values : ARRAY OF INTEGER) RETURNS REAL DECLARE i, Total : INTEGER Total ← 0 FOR i ← 1 TO 6 Total ← Total + Values[i] NEXT i RETURN Total / 6ENDFUNCTIONThe array arrives as a parameter, so the function uses the parameter's name (Values), not Mark. It only reads it: BYVAL is fine.
L3 Hard
Filter in place, sort parallel arrays, two targets.
L3.1 Remove the gaps
Item : ARRAY[1:8] OF STRING has unused elements ("") in random places. Write a procedure Compact() that moves the used items to the front in the same order, with all the "" after them - using only Item itself.
Hint
Could the target of pattern 10 be the same array as the source?
Show the model answer
PROCEDURE Compact() DECLARE i, k : INTEGER k ← 0 FOR i ← 1 TO 8 IF Item[i] <> "" THEN k ← k + 1 Item[k] ← Item[i] ENDIF NEXT i FOR i ← k + 1 TO 8 Item[i] ← "" NEXT iENDPROCEDUREPattern 10 into the SAME array: k is never bigger than i, so an item is only ever copied backwards onto a place already read. Then clear what is left.
L3.2 Alphabetical register
Sort the parallel arrays Name and Mark (6 students) so the names are in alphabetical order, each mark staying with its student.
Show the model answer
DECLARE j, TempMark : INTEGERDECLARE TempName : STRINGFOR i ← 1 TO 5 FOR j ← 1 TO 6 - i IF Name[j] > Name[j + 1] THEN TempName ← Name[j] Name[j] ← Name[j + 1] Name[j + 1] ← TempName TempMark ← Mark[j] Mark[j] ← Mark[j + 1] Mark[j + 1] ← TempMark ENDIF NEXT jNEXT iThe comparison is on Name now, but both arrays still swap together.
L3.3 Split into two
Write a procedure Split() that takes an array of 10 integers BYVAL and two arrays BYREF, Even and Odd (each 10 elements). It copies the even numbers into Even and the odd ones into Odd, with no gaps, and fills the unused elements of both with -1.
Show the model answer
PROCEDURE Split(Values : ARRAY OF INTEGER, BYREF Even : ARRAY OF INTEGER, BYREF Odd : ARRAY OF INTEGER) DECLARE i, e, o : INTEGER FOR i ← 1 TO 10 Even[i] ← -1 Odd[i] ← -1 NEXT i e ← 0 o ← 0 FOR i ← 1 TO 10 IF Values[i] MOD 2 = 0 THEN e ← e + 1 Even[e] ← Values[i] ELSE o ← o + 1 Odd[o] ← Values[i] ENDIF NEXT iENDPROCEDURETwo targets, so two target indexes (e and o) - plus i for the source. Filling with -1 first is simpler than tidying up afterwards.
Mistakes that lose marks
| Mistake | Write this instead |
|---|---|
| Keeping the highest value, then searching again for the name | Keep the index of the highest; read Name[Best] |
| Sorting one parallel array only | Swap the same two elements in every parallel array |
Copying with one index: Target[i] ← Source[i] inside an IF | A second index k for the target |
| Not clearing the rest of the target array | Fill from k + 1 to the end with the unused value |
| Changing an array passed BYVAL and expecting the caller to see it | BYREF, or work on the global array |
| Re-declaring a global array inside a module | Just use it - it is already declared |
Can I do this?
- I can use the same index in parallel arrays, and sort them without breaking the pairs.
- I can filter one array into another with two indexes, and say how many were copied.
- I can use a global array in a module, and pass an array BYVAL or BYREF correctly.
- I can write modules that filter, tidy up and call each other.
Anything unticked: revise that section first. All ticked: take the quiz.
The multiple-choice and guided questions need JavaScript. The concept checks and exam practice below work without it.
Concept checks
Answer each one in your head first, then open it.
1Why are parallel arrays needed at all?Show answer
One array holds one data type, so a name (STRING) and a mark (INTEGER) need an array each; the shared index joins them.
2Why can pattern 10 copy within the same array safely?Show answer
k never runs ahead of i, so each item is written to a place that has already been read.
3When is a global array better than a parameter, and the other way round?Show answer
Global: the question says so, and many modules share one data set. Parameter: the module must work on different arrays (it is reusable and easier to test).
Your progress on this note is saved when JavaScript is on.
⬇️ Download this chapter as a PDF: Full notes · Worksheets · Answers