Arrays, Records and String Handling
Storing Collections of Data
A simple variable holds one value. But real programs need to store collections — a class of thirty student names, a grid of pixel colours, the items in a shopping basket. Data structures solve this problem. The two you need for this course are arrays and records.
One-Dimensional Arrays
An array stores multiple values of the same data type in a single named structure. Each value sits at a numbered position called an index. In most languages (and in the standard pseudo-code), indexing starts at 0, so the first element is at index 0, the second at index 1, and so on.
Declaring and using a 1D array in the standard pseudo-code:
scores ← [72, 85, 61, 90, 78] OUTPUT scores[0] // outputs 72 (first element) OUTPUT scores[4] // outputs 78 (fifth element) scores[2] ← 65 // changes the third element to 65
An array stores multiple values of the same data type. Access individual elements using the array name followed by the index in square brackets — for example, names[0] gives the first element. Only one-dimensional and two-dimensional arrays are required.
Two-Dimensional Arrays
A 2D array adds a second dimension — think of it as a grid or table. You need two indices to access an element: the row first, then the column. A 2D array is ideal for storing a seating plan, a game board, or a small spreadsheet of results.
A classroom has 3 rows and 4 seats per row. We store the seat numbers in a 2D array:
seats ← [[1,2,3,4],[5,6,7,8],[9,10,11,12]] OUTPUT seats[0][0] // row 0, seat 0 → outputs 1 OUTPUT seats[1][2] // row 1, seat 2 → outputs 7 OUTPUT seats[2][3] // row 2, seat 3 → outputs 12
The first index selects the row; the second selects the column within that row. Changing seats[0][3] ← 99 would update the fourth seat in the first row to 99.
Records
An array stores values of the same type. A record stores related values of different data types together. Each piece of data inside a record is called a field. You access a field by its name, not a number.
The specification uses this exact example for defining a record:
RECORD Car make : String model : String reg : String price : Real noOfDoors : Integer ENDRECORD
To use the record, create a variable of that record type and then access fields by name:
myCar.make ← "Toyota" myCar.model ← "Corolla" myCar.price ← 18500.00 myCar.noOfDoors ← 5 OUTPUT myCar.make // outputs Toyota
An array stores many values of the same type, accessed by index. A record stores related values of different types, accessed by field name. Use arrays for lists; use records for objects with multiple properties.
String Handling Operations
String operations let you inspect and manipulate text. It is easy to lose marks by applying string functions incorrectly — especially length and substring. Work through each one carefully with the examples below.
Let name ← "London" for all examples below.
Length — returns the number of characters in a string.
LEN(name) returns 6. (L, o, n, d, o, n — count all six.)
Position — returns the index of the first place a character appears in a string.
POSITION(name, “n”) returns 2. Counting starts at 0, so “L” is index 0 and the first “n” is index 2.
Substring — extracts part of a string. The call is SUBSTRING(startIndex, endIndex, string) and both index positions are included.
SUBSTRING(1, 3, name) returns “ond”. (The characters at index 1, 2 and 3.)
Concatenation — joins two or more strings together using +.
“Big ” + name returns “Big London”.
Character to character code — returns the ASCII/Unicode code for a character.
CHAR_TO_CODE(“A”) returns 65.
Character code to character — returns the character for a given code.
CODE_TO_CHAR(65) returns “A”.
String conversion — converts between strings and numeric types.
STRING_TO_INT(“42”) returns the integer 42. INT_TO_STRING(42) returns the string “42”. This is essential because user input arrives as a string by default — you must convert before doing maths.
Input, Output and Random Numbers
Programs interact with users through USERINPUT and OUTPUT. The value from USERINPUT is always a string — cast it to the correct type before using it in calculations. Random number generation lets programs produce unpredictable values within a range. A dice simulator, a quiz question shuffler, and a password generator all rely on this.
// Input and cast OUTPUT "Enter your age: " age ← STRING_TO_INT(USERINPUT) // Random number between 1 and 6 (inclusive) die ← RANDOM_INT(1, 6)