8.9 - Arrays
Introduction to arrays
Arrays provide an efficient way to handle collections of related data in programming. Instead of creating separate variables for each item, you can group them together, which simplifies code and improves organisation.
A data structure is a way of organising and storing data so that it can be accessed and modified efficiently. An array is a specific type of data structure that holds multiple values of the same type under a single name.
Key features of arrays
- Elements - Individual pieces of data stored in the array. Each element has a unique position.
- Index - A number that identifies an element's position in the array. Indices usually start at 0.
- Declaration - In many programming languages, you must declare an array before using it, specifying its size (the maximum number of elements it can hold).
Arrays are particularly useful for storing related items, such as a list of student names or test scores, where using individual variables would be impractical.
One-dimensional arrays
A one-dimensional array, often called a 1D array, is like a simple list of items. It stores elements in a single row, and you access them using one index.
Creating a one-dimensional array
To create an array, you specify its name and size. For example, in pseudocode:
This creates an array called names with 3 elements, assigning string values to positions 0 through 2.
Retrieving elements from a one-dimensional array
To access an element, use the array name followed by its index in square brackets. For example:
output names[1]
This would display "Bob", the value at index 1.
Changing elements in a one-dimensional array
You can update an element by assigning a new value to its index. For example:
This replaces "Alice" with "Eve", resulting in: ["Eve", "Bob", "Chloe"].
Using loops with one-dimensional arrays
Loops are powerful tools for processing arrays systematically. A FOR loop can iterate over each element, allowing you to perform operations like searching, updating, or calculating totals.
Example algorithm: Adding a value to each element
Consider an array called marks containing these values: [10, 20, 30, 40]. An algorithm to add 5 to each mark uses a FOR loop to access and modify every position.
After running this, the array becomes: [15, 25, 35, 45].
How this works:
- The loop variable
istarts at 0 and goes up to 3 (the last index). - For each
i, it adds 5 to the element at that position.
This approach ensures all elements are updated efficiently without repeating code.
Two-dimensional arrays
A two-dimensional array, or 2D array, extends the idea of a 1D array by organising data in rows and columns, like a grid or table. It is essentially a list of lists, where each element is accessed using two indices: one for the row and one for the column.
Creating a two-dimensional array
In pseudocode, you might create a 2D array like this:
This creates a 2x3 array (2 rows, 3 columns). You can visualise it as a table:
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | Red | Green | Blue |
| Row 1 | Yellow | Orange | Purple |
Retrieving and changing elements in a two-dimensional array
Access elements using two indices, such as [row, column]. For example:
output grid[0, 1]
This displays "Green".
To change an element:
grid[1, 1] <- "Magenta"
Now the array at [1, 1] holds "Magenta".
Algorithms and examples with arrays
Arrays are often used in algorithms to process data, such as calculating totals or counting specific values. Nested loops are common for 2D arrays to iterate through rows and columns.
Example: Calculating a pupil's total score
Suppose a 2D array results stores test scores for 4 pupils across 3 tests:
| Pupil 0 | Pupil 1 | Pupil 2 | Pupil 3 | |
|---|---|---|---|---|
| Test 1 | 10 | 15 | 8 | 12 |
| Test 2 | 13 | 9 | 11 | 14 |
| Test 3 | 7 | 18 | 13 | 10 |
To calculate the total for a given pupil:
How this algorithm works:
- The loop adds scores from each test (row) for the selected pupil (column).
Example: Counting passes in a 2D array
Using the same results array, count how many scores are 12 or above (pass mark). This uses nested FOR loops:
How this algorithm works:
- The outer loop handles rows, the inner loop handles columns.
- The IF statement checks each score and increments the counter if it meets the condition.