Ch 3 · 8.2 Arrays · IGCSE Computer Science 0478
Topic 3 of 4 in Programming — IGCSE Computer Science 0478 — 49 lessons.
Introduction
Storing 30 marks in 30 separate variables would be painful. An array stores them all under one name, and a loop can process every one. (Syllabus section 8.2.)
Key ideas — What an array is
An array is a fixed-length data structure that stores elements of the same data type under one identifier; each element is accessed by its index (position).
🧑🏫 Teacher's explanation
Imagine you are a teacher with a stack of 30 test papers. Instead of having 30 separate folders named 'Paper1', 'Paper2', and so on, you have one single box labeled 'TestMarks'. To find a specific student's score, you don't look through the whole room; you simply look at the 5th paper in that box. That box is your array, and the position '5' is your index.
📖 Definitions
🔍 Worked example — Easy
A 1D array StudentMarks is declared with 5 elements. The values are: [85, 72, 90, 65, 88]. Calculate the sum of all elements using iteration.
🔍 Worked example — Standard
An array Grid is a 2D array of size 3 × 3. The values are: \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}. Find the value at row 2, column 3.
🔍 Worked example — Exam-style
Write pseudocode to find the highest value in a 1D array Prices of size n.
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
Key ideas — One-dimensional (1D) arrays
The first index can be 0 or 1 (in Cambridge pseudocode the lower bound is given in the declaration).
🧑🏫 Teacher's explanation
Imagine you are a teacher with a stack of 30 exam papers. Instead of having 30 different folders named 'Mark1', 'Mark2', etc., you have one single drawer labeled 'Marks' with 30 numbered slots. Each slot holds one specific score. This is exactly what an array is: a single variable name that holds a collection of items of the same data type.
📖 Definitions
🔍 Worked example — Easy
A 1D array Scores is declared as ARRAY[1:5] OF INTEGER. Assign the values 10, 20, 30, 40, and 50 to the array and output the value at index 3.
🔍 Worked example — Standard
An array Temperatures is declared as ARRAY[1:4] OF REAL. Use a loop to find the sum of the elements if the values are 15.5, 16.0, 15.8, and 16.2.
🔍 Worked example — Exam-style
A program uses an array Data : ARRAY[1:10] OF INTEGER. A loop runs from i arrow 1 to 10. Inside the loop, the instruction is Data[i] arrow Data[i] * 2. If the initial values are all 5, what is the value of Data[7] after the loop finishes?
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
(a) Declare the array. (b) Use a loop to input the marks from the user. (c) Calculate and output the sum of all marks. [5 marks]
Key ideas — Two-dimensional (2D) arrays
A 2D array is like a table with rows and columns. Each element needs two indexes: [row, column].
🧑🏫 Teacher's explanation
Imagine a standard spreadsheet or a classroom seating plan. A 1D array is just a single row of lockers, where you only need one number to find a specific locker. A 2D array, however, is a grid—like a chessboard or a spreadsheet—where you need two pieces of information to locate a single cell: the row number and the column number.
📖 Definitions
🔍 Worked example — Easy
A 2D array Grid[1:3, 1:2] is declared. Assign the value 10 to the element in the second row and first column, and 20 to the element in the third row and second column.
🔍 Worked example — Standard
Given a 2D array Matrix[1:2, 1:2] containing: \\ Matrix[1, 1] = 5, Matrix[1, 2] = 10, Matrix[2, 1] = 3, Matrix[2, 2] = 4. Calculate the sum of all elements using nested iteration.
🔍 Worked example — Exam-style
A 2D array Data[1:4, 1:2] stores temperatures. The following pseudocode is used: \\ FOR r arrow 1 TO 4 \\ FOR c arrow 1 TO 2 \\ IF Data[r, c] > 30 THEN OUTPUT "Hot" \\ ENDIF \\ NEXT c \\ NEXT r. If the array contains: \\ Data[1,1]=25, Data[1,2]=35, Data[2,1]=32, Data[2,2]=28, Data[3,1]=40, Data[3,2]=15, Data[4,1]=10, Data[4,2]=31. How many times is "Hot" output?
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
Key ideas — Parallel arrays
Two or more 1D arrays with the same index can hold related data, e.g. Names[Index] and Marks[Index] for the same student.
🧑🏫 Teacher's explanation
Imagine you are organizing a school sports day. You have a list of student names on one piece of paper and a list of their race times on a separate piece of paper. To make sense of this, you need both lists to be in the exact same order. If 'Alice' is the 3rd name on the first list, her time must be the 3rd entry on the second list. This way, the index (the position) acts as the link between the two pieces of information.
📖 Definitions
🔍 Worked example — Easy
Given two arrays: Names = ["Ali", "Bo", "Cy"] and Scores = [85, 92, 78]. Find the score for "Bo".
🔍 Worked example — Standard
Write pseudocode to find the highest score in a parallel array setup where Names[1..5] contains names and Scores[1..5] contains marks. Output the name of the student with the highest score.
🔍 Worked example — Exam-style
A system uses three parallel arrays: ID[1..100], Age[1..100], and Grade[1..100]. If we want to update the Grade of the student whose ID is 55, what is the index used for the Grade array?
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
Write pseudocode to find the name of the student who achieved the highest mark and output that name. [4] [4 marks]
Worked example
An array Rainfall holds the rainfall for each of 12 months. Write pseudocode to output the month number with the highest rainfall.
🔍 Worked example — Easy
An array Scores contains [15, 42, 7, 88, 32]. Write pseudocode to find the maximum value in the array.
🔍 Worked example — Standard
An array Matrix is a 2D array: [[1, 2], [3, 4]]. Write pseudocode to output the sum of all elements.
🔍 Worked example — Exam-style
An array Temperatures stores values for 5 days: [22, 25, 19, 28, 24]. Write pseudocode to output the index (day number) of the coldest day, assuming the array is indexed 1 to 5.
⚠️ Common errors
Common mistakes
Practice
Answers: (1) DECLARE Names : ARRAY[1:25] OF STRING. (2) FOR Index ← 1 TO 10 / INPUT Numbers[Index] / NEXT Index. (3) Count ← 0 / FOR Row ← 1 TO 10 / FOR Seat ← 1 TO 20 / IF Seats[Row, Seat] = TRUE THEN Count ← Count + 1 ENDIF / NEXT Seat / NEXT Row / OUTPUT Count. (4) One identifier for many items; can be processed with a loop (less code); easier to search, sort and total.