arrays
arrays
Computer Science
Arrays
Contents
Arrays
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 1
Arrays
Your notes
1-Dimensional Arrays
What is an array?
An array is an ordered, static set of elements in a fixed-size memory location
An array can only store 1 data type
A 1D array is a linear array
Indexes start at generally start at 0, known as zero-indexed
Example in Python
Creating a one-dimensional array called ‘array’ which contains 5 integers.
Create the array with the following syntax:
array = [1, 2, 3, 4, 5]
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 2
Access the individual elements of the array by using the following syntax:
array[index]
Your notes
Modify the individual elements by assigning new values to specific indexes using the
following syntax:
array[index] = newValue
Use the len function to determine the length of the array by using the following syntax:
len(array)
In the example the array has been iterated through to output each element within the
array. A for loop has been used for this
Python
# Output:
#1
# 10
#3
#4
#5
2-Dimensional Arrays
What is a 2-dimensional array?
A 2D array extends the concept on a 1D array by adding another dimension
A 2D array can be visualised as a table with rows and columns
When navigating through a 2D array you first have to go down the rows and then across
the columns to find a position within the array
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 3
Your notes
Create Declare a 2D array with name and Creates a 3x2 blank array (3 people,
number for 3 people each with name and number)
Declare a 2D array called players with name and score assigned for 4 people
(Alice, Bob, Charlie & Daisy)
// Declare a 2D array with 4 rows and 2 # Each player has a name and a score
columns (name and score)
players = [
DECLARE players : ARRAY[1:4, 1:2] OF
["Alice", 25],
STRING
["Bob", 30],
// Assign values to each player
["Charlie", 22],
players[1,1] ← "Alice"
["Daisy", 28]
players[1,2] ← "25"
]
players[2,1] ← "Bob"
players[2,2] ← "30"
players[3,1] ← "Charlie"
players[3,2] ← "22"
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 4
players[4,1] ← "Daisy"
Example in Python
Initialising a 2D array with 3 rows and 3 columns, with the specified values
Worked Example
A parent records the length of time being spent watching TV by 4 children
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 5
Data for one week (Monday to Friday) is stored in a 2D array with the identifier
minsWatched.
Your notes
The following table shows the array
0 1 2 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
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 6