Arrays
Revise Arrays for Computer Science 0478 (O Level) — revision notes and instant AI marking. Free to start.
Arrays: The Complete Revision Guide
The big idea: An array is a single named box divided into fixed, ordered compartments that all hold the same type of thing — so instead of naming every value separately, you find any value by its position.
Summary — What This Chapter Covers
- What an array actually is, and why it exists (fixed-size, single data type, ordered).
- The index war: Python starts counting at 0, CIE pseudocode starts counting at 1.
- How to declare, access, and modify elements in a 1D array — in both pseudocode and Python.
- Using
len()/ a length variable to find how many elements are in an array. - Iterating through a 1D array with a
FOR...NEXTloop. - 2D arrays — thinking of them as a grid with rows and columns, and how to navigate row-first, then column.
- Declaring, accessing, and modifying elements in a 2D array — pseudocode uses
[row, col], Python uses[row][col]. - Nested
FOR...NEXTloops to iterate through every cell of a 2D array.
1. What Is an Array?
Imagine you need to store the test scores of 5 students. You create five separate variables — score1, score2, score3, score4, score5 — but that's clunky, and it falls apart completely if you have 500 students instead of 5. You'd need 500 variable names, and looping through them would be impossible because you can't easily say "give me variable number 347" in a loop.
An array solves this. Think of it like a strip of numbered pigeonholes bolted to a wall. The whole strip has one name (say, scores), and each pigeonhole has a position number. To get a value out, you don't need a new variable name — you just say "give me the value in pigeonhole 3."
Formally, an array is defined by three properties, and all three matter for exam answers:
- Ordered — every element has a fixed position (an index), so the array always keeps the same sequence unless you explicitly change it.
- Static / fixed-size — once you declare an array to hold, say, 5 elements, it always has exactly 5 slots. You can't stretch it to hold a 6th value without creating a new array. (This is different from a Python , which can grow — but for IGCSE purposes, arrays are treated as fixed-size.)
- Single data type — an array of integers can only hold integers. You can't mix a string into an array of numbers.
Visualising a 1D array
Here's the array ["B","E","A","D","S"] drawn out — notice this uses zero-indexing (the Python/most-languages convention):
Array length is 5 · First element is at index 0 · The 4th element ("D") sits at index 3
In CIE pseudocode, indexes start at 1. In Python, indexes start at 0. This is the #1 thing that trips students up in exams — because you'll be shown pseudocode and Python for the exact same array, and the same real-world position (e.g. "the 4th element") will be written as [4] in pseudocode but [3] in Python.
An array called fruits stores 6 values. In CIE pseudocode, what index would you use to access the very last element? What about in Python?
Explain why you could not use an array to store a mixed list containing a student's name (a string) and their age (an integer) in the same array.
2. Creating, Accessing & Modifying a 1D Array
There are three things you'll always be asked to do with an array: create it, read (access) a value from it, and write (modify) a value in it. Let's go through the exact syntax for each, side by side in both languages.
Creating an array
| Pseudocode | Python |
|---|---|
DECLARE MyArray : ARRAY[1:5] OF INTEGER | array = [1, 2, 3, 4, 5] |
DECLARE MyArray : ARRAY[1:5] OF INTEGER means: "Create an array called MyArray, with indexes running from 1 to 5 (so 5 slots total), and every value stored must be an INTEGER."
You can also declare a blank array and fill it slot by slot with assignment statements — this is exactly what happens in the worked example below.
Accessing a single element
| Pseudocode | Python |
|---|---|
MyArray[1] ← gets 1st element | array[0] ← gets 1st element |
Modifying a single element
| Pseudocode | Python |
|---|---|
MyArray[index] ← newValue | array[index] = newValue |
Notice pseudocode uses a left-pointing arrow ← for assignment, while Python uses a single equals sign =. This is consistent across all of CIE pseudocode, not just arrays — always keep it straight in your head.
Finding the length
Python has a handy built-in function, len(array), that instantly tells you how many elements are in an array. CIE pseudocode has no built-in length function — you either already know the size from the DECLARE statement, or you store it yourself in a variable.
Full worked example — building and using MyArray
// Creating a one-dimensional array DECLARE MyArray : ARRAY[1:5] OF INTEGER MyArray[1] ← 1 MyArray[2] ← 2 MyArray[3] ← 3 MyArray[4] ← 4 MyArray[5] ← 5 // Accessing elements OUTPUT MyArray[1] // Output: 1 OUTPUT MyArray[3] // Output: 3 // Modifying an element MyArray[2] ← 10 // MyArray is now: [1, 10, 3, 4, 5] // Iterating over the array DECLARE Index : INTEGER FOR Index ← 1 TO 5 OUTPUT MyArray[Index] NEXT Index // Output: 1, 10, 3, 4, 5 // Length of the array DECLARE Length : INTEGER Length ← 5 OUTPUT Length // Output: 5
# Creating a one-dimensional array array = [1, 2, 3, 4, 5] # Accessing elements print(array[0]) # Output: 1 print(array[2]) # Output: 3 # Modifying an element array[1] = 10 print(array) # [1, 10, 3, 4, 5] # Iterating over the array for element in array: print(element) # Output: 1, 10, 3, 4, 5 # Length of the array length = len(array) print(length) # Output: 5
In the pseudocode above, MyArray[1] and MyArray[3] print 1 and 3 — the value happens to equal the index because we filled the array in order. But look at the Python version: array[0] and array[2] print exactly the values, 1 and 3. That's because index 0 in Python and index 1 in pseudocode both point at the — they're just counted differently. Always translate by position ("the 3rd element"), not by the raw number.
Given the pseudocode array declared as DECLARE Colours : ARRAY[1:4] OF STRING with values ["Red","Green","Blue","Yellow"] assigned in order, write the line of pseudocode that changes "Blue" to "Purple".
3. 2-Dimensional Arrays
A 1D array is a single strip of pigeonholes. A 2D array is what you get when you stack several of those strips on top of each other — now you have rows columns, exactly like a spreadsheet or a table.
The most natural way to think about a 2D array is as a grid: the row index tells you which strip (which row) to look in, and the column index tells you which pigeonhole along that strip to open.
[row, column] in pseudocode and [row][column] in Python — but always check the example given in the exam question, because occasionally a question will define it as [column, row] instead!
Visualising a 2D array
Here's a 3×5 grid of letters spelling a hidden word, read row by row:
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | B | E | A | D | S |
| 1 | S | E | V | E | N |
| 2 | W | H | I | T | E |
The left index picks the row. The right index picks the column. So position [1,2] (row 1, column 2) gives you the letter V.
Declaring a 2D array
DECLARE NamesAndNumbers : ARRAY[1:3, 1:2] OF STRING1:3 = 3 rows (one row per person) · 1:2 = 2 columns (one for name, one for number) · OF STRING = both names and phone numbers are stored as strings, because arrays must hold a single data type — so even a phone number gets stored as text.
Worked example — building the "players" 2D array
Four players, each with a name and a score, stored as a 4-row, 2-column array:
// 4 rows, 2 columns (name and score) DECLARE players : ARRAY[1:4, 1:2] OF STRING // Assign values to each player players[1,1] ← "Alice" players[1,2] ← "25" players[2,1] ← "Bob" players[2,2] ← "30" players[3,1] ← "Charlie" players[3,2] ← "22" players[4,1] ← "Daisy" players[4,2] ← "28"
# Each player: [name, score] players = [ ["Alice", 25], ["Bob", 30], ["Charlie", 22], ["Daisy", 28] ]
Notice Python is happy to mix a string and an integer inside ["Alice", 25] because that's just a small nested inside a bigger list — Python lists are more flexible than the strict "array" definition CIE pseudocode uses. But in strict pseudocode terms, everything has to be declared as one type (STRING here), which is why the score is stored as the text "25" rather than the number 25.
Modifying an element in a 2D array
| Task | Pseudocode | Python |
|---|---|---|
| Change "Charlie" to "Holly" | players[3,1] ← "Holly" | players[2][0] = "Holly" |
Charlie is the 3rd person, so pseudocode row index is 3 (1-indexed); Python's row index is 2 (0-indexed). The name is the 1st item in each row, so pseudocode column is 1; Python column is 0.
Can you see why players[3,1] and players[2][0] are the exact same box in memory? Row: 3rd person either way. Column: 1st value (the name) either way. Only the counting system changes — the real-world position doesn't.
Full worked example — a 3×3 numeric grid
DECLARE Array2D : ARRAY[1:3, 1:3] OF INTEGER Array2D[1,1] ← 1 Array2D[1,2] ← 2 Array2D[1,3] ← 3 Array2D[2,1] ← 4 Array2D[2,2] ← 5 Array2D[2,3] ← 6 Array2D[3,1] ← 7 Array2D[3,2] ← 8 Array2D[3,3] ← 9 // Accessing elements OUTPUT Array2D[1,1] // Output: 1 OUTPUT Array2D[2,3] // Output: 6
array_2d = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# Accessing elements
print(array_2d[0][0]) # Output: 1
print(array_2d[1][2]) # Output: 6
Using the Array2D grid above, what pseudocode statement would you write to output the value 8, and what is its Python equivalent?
4. Iterating Through a 2D Array
To visit every single element of a 2D array, one loop isn't enough — you need a loop a loop, called a nested loop. Think of it like reading a book: the outer loop moves you down through the pages (rows), and for every page, the inner loop reads every word on that page (columns) before moving to the next page.
DECLARE Row : INTEGER DECLARE Col : INTEGER FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Array2D[Row, Col] NEXT Col NEXT Row
for row in array_2d:
for item in row:
print(item, end=" ")
print() # newline after each row
Row) only advances to the next value the entire inner loop (Col) has finished running from start to end. So for a 3×3 grid: Row=1 runs Col 1,2,3 (3 outputs) → Row=2 runs Col 1,2,3 (3 more outputs) → Row=3 runs Col 1,2,3 (3 more outputs) = 9 outputs total, visiting every single cell exactly once.
In the exam, the question will always give you an example showing which order the array is being read from. Some questions use [row, column], others flip it to [column, row]. Always check the example given before answering — don't assume it's the same convention used here.
Worked example — the TV-watching table
A parent records minutes of TV watched by 4 children (Quinn, Lyla, Harry, Elias) across 5 days, stored in a 2D array called minsWatched using Python-style zero-indexing, rows = days, columns = children:
| Quinn (0) | Lyla (1) | Harry (2) | Elias (3) | |
|---|---|---|---|---|
| Monday (0) | 34 | 67 | 89 | 78 |
| Tuesday (1) | 56 | 43 | 45 | 56 |
| Wednesday (2) | 122 | 23 | 34 | 45 |
| Thursday (3) | 13 | 109 | 23 | 90 |
| Friday (4) | 47 | 100 | 167 | 23 |
Worked step-by-step: "Elias watched 78 minutes of TV on Monday" — how do we know?
- Identify the row for Monday → Row 0
- Identify the column for Elias → Column 3
- Look up
minsWatched[0][3]→ value is 78 ✓
Using the table above, write a line of Python code to output the number of minutes Harry watched TV on Friday.
Using the table above, write a line of code (pseudocode Python) to output the number of minutes Quinn watched TV on Wednesday.
What to Memorise
array[3].
[row, column] in pseudocode, [row][column] in Python.
DECLARE name : ARRAY[lower:upper] OF type for 1D, add a second range for 2D.
←. Python uses =. Same meaning: store the right-hand value at the position on the left.
len()
Python's built-in function to find array length. No equivalent exists in CIE pseudocode — you must know or store the size yourself.
Concepts Checklist
Exam Tips
The single most common error: using 0-indexing in pseudocode, or 1-indexing in Python. If the question is written in pseudocode, count from 1. If it's written in Python, count from 0. Double-check which language the question is actually asking for before you write your index.
Don't assume [row, column] is always correct — some exam questions define their 2D array as [column, row] instead. Always find the example given in the question (like "Elias watched 78 minutes on Monday, found at [0][3]") and work out the convention from that before answering later parts.
If asked to explain something can't be stored in an array, or why an array is different from other structures, always mention BOTH properties: fixed size AND single data type. Markschemes usually award a point for each one separately.
When writing a FOR...NEXT loop to iterate a pseudocode array of size 5, the loop must run FOR Index ← 1 TO 5 — not 1 TO 4 (misses the last element) and not 0 TO 5 (goes out of bounds and reads an extra, non-existent element). In Python, range(len(array)) correctly gives you 0 to size-1.
- Correct index numbers matching the language used in the question (pseudocode vs Python).
- Correct syntax — square brackets, comma for 2D pseudocode vs double brackets for 2D Python.
- Using the exact variable/array name given in the question — not inventing your own.
- For "write a line of code" questions worth 1 mark, a single correct statement is usually enough — don't overcomplicate with unnecessary loops.
- For iteration questions, correctly nesting the loops (row loop OUTSIDE, column loop INSIDE) and correctly closing them (
NEXT ColbeforeNEXT Row).
- 2. Creating, Accessing & Modifying a 1D Array
Read the full Arrays notes free
That's the preview — create a free account to read the rest, plus flashcards and practice questions with instant AI marking. No credit card.
Unlock the full notes free →