Arrays

2026 Syllabus Objectives

By the end of this topic, you should be able to:

  1. Declare and use one-dimensional (1D) and two-dimensional (2D) arrays
  2. Understand the use of arrays, including the use of variables as indexes in arrays; The first index can be zero or one
  3. Write values into, and read values from, an array using iteration, including nested iteration

What is an Array?

An array is a special type of variable that can store multiple values of the same type in one place. Think of it like a row of boxes, where each box can hold one piece of data.

Key features of arrays:

  • Ordered — The items are stored in a specific sequence (first, second, third, etc.)
  • Fixed size — Once you create an array, its size cannot change
  • Same data type — All items in the array must be the same type (e.g., all numbers, or all text, but not mixed)

Why use arrays?

Instead of creating separate variables like:

score1 = 85
score2 = 90
score3 = 78
score4 = 92
score5 = 88

You can use one array to store all five scores together:

scores = [85, 90, 78, 92, 88]

This makes your code much simpler and easier to manage, especially when you have lots of values to store.


One-Dimensional (1D) Arrays

A one-dimensional array (also called a 1D array) is a simple list of values arranged in a single line, like beads on a string.

Visual Example of a 1D Array

Imagine an array called letters containing five characters:

Index:     0     1     2     3     4
         +---+ +---+ +---+ +---+ +---+
Array:   | B | | E | | A | | D | | S |
         +---+ +---+ +---+ +---+ +---+
         ↑                           ↑
    First element              Fifth element
  • The array length is 5 (it contains 5 elements)
  • Each element has a position number called an index

Array Indexing: Zero-Based vs One-Based

Index means the position number of an element in the array.

There are two ways to count positions in an array:

  1. Zero-indexed (starts at 0) — Most common in programming languages like Python

    • First element is at index 0
    • Second element is at index 1
    • Third element is at index 2, and so on
  2. One-indexed (starts at 1) — Sometimes used in pseudocode

    • First element is at index 1
    • Second element is at index 2
    • Third element is at index 3, and so on

Important: In your exam, the question will tell you whether the array is zero-indexed or one-indexed. Always check this before answering!

Declaring (Creating) a 1D Array

In Pseudocode:

DECLARE scores: ARRAY[0:4] OF INTEGER

This means: "Create an array called scores that can hold 5 integers (from index 0 to index 4)."

You can also create an array and fill it with values at the same time:

scores ← [12, 10, 5, 2, 8]

In Python:

scores = []  # Creates an empty array

Or create it with values:

scores = [12, 10, 5, 2, 8]  # Creates an array with 5 values

Accessing Elements in a 1D Array

To read a value from an array, use the array name followed by the index in square brackets.

In Pseudocode:

OUTPUT scores[0]    // Outputs the first element (12)
OUTPUT scores[3]    // Outputs the fourth element (2)

In Python:

print(scores[0])    # Outputs: 12
print(scores[3])    # Outputs: 2

Modifying Elements in a 1D Array

To change a value in an array, use the assignment operator.

In Pseudocode:

colours[4] ← "Red"    // Assigns "Red" to index 4 (5th element)

In Python:

colours[4] = "Red"    # Assigns "Red" to index 4

Example:

scores = [12, 10, 5, 2, 8]
scores[1] = 15         # Changes the second element from 10 to 15
print(scores)          # Outputs: [12, 15, 5, 2, 8]

Using Variables as Indexes

You can use a variable to specify which element to access. This is very useful when working with loops.

Example:

scores = [12, 10, 5, 2, 8]
position = 2
print(scores[position])    # Outputs: 5 (the element at index 2)

You can also do calculations with the index:

i = 1
print(scores[i + 1])    # Outputs: 5 (element at index 2)

Iterating Through a 1D Array

Iteration means going through each element in the array one by one. We use loops to do this.

Method 1: Using a FOR loop with the array directly

In Python:

scores = [12, 10, 5, 2, 8]

for score in scores:
    print(score)

Output:

12
10
5
2
8

This loop automatically goes through each element in the array.

Method 2: Using a FOR loop with index numbers

In Pseudocode:

FOR i ← 0 TO 4
    OUTPUT scores[i]
NEXT i

In Python:

scores = [12, 10, 5, 2, 8]

for i in range(5):    # range(5) gives us 0, 1, 2, 3, 4
    print(scores[i])

This method is useful when you need to know the position (index) of each element.

Finding the length of an array:

In Python:

length = len(scores)    # Returns 5

You can use this in a loop:

for i in range(len(scores)):
    print(scores[i])

This is better because it automatically adjusts if the array size changes.

Sign in to view full notes