You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers arrays and records — two fundamental static data structures used extensively in A-Level Computer Science. Arrays store collections of elements of the same type, while records group related data of different types.
An array is a static data structure that stores a fixed number of elements of the same data type in contiguous memory locations. Each element is accessed using an index (a numerical position).
| Property | Description |
|---|---|
| Fixed size | The size is set when the array is created and cannot change. |
| Homogeneous | All elements must be of the same data type. |
| Contiguous memory | Elements are stored next to each other in memory. |
| Indexed access | Any element can be accessed directly using its index in O(1) time. |
| Zero-based indexing | In most languages, the first element is at index 0. |
DECLARE names: ARRAY[0..4] OF STRING
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Charlie"
// Traverse the array
FOR i = 0 TO 4
OUTPUT names[i]
NEXT i
# Python lists are dynamic, but are often used to demonstrate array concepts
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
# Access by index
print(names[0]) # Alice
print(names[2]) # Charlie
# Traverse the array
for name in names:
print(name)
# Access with index
for i in range(len(names)):
print(f"Index {i}: {names[i]}")
A one-dimensional (1D) array is a linear list of elements, like a single row.
Index: [0] [1] [2] [3] [4]
Values: 10 20 30 40 50
| Operation | Description | Time Complexity |
|---|---|---|
| Access | Read/write element at index i | O(1) |
| Search (linear) | Find an element by value | O(n) |
| Search (binary) | Find in sorted array | O(log n) |
| Insert | Add at a specific position (shifting required) | O(n) |
| Delete | Remove from a specific position (shifting required) | O(n) |
A two-dimensional (2D) array is an array of arrays — a grid or table with rows and columns.
Col 0 Col 1 Col 2
Row 0 [ 10 , 20 , 30 ]
Row 1 [ 40 , 50 , 60 ]
Row 2 [ 70 , 80 , 90 ]
DECLARE grid: ARRAY[0..2, 0..2] OF INTEGER
grid[0][0] = 10
grid[1][2] = 60
// Traverse a 2D array
FOR row = 0 TO 2
FOR col = 0 TO 2
OUTPUT grid[row][col]
NEXT col
NEXT row
grid = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
# Access element at row 1, column 2
print(grid[1][2]) # 60
# Traverse the 2D array
for row in range(len(grid)):
for col in range(len(grid[row])):
print(f"grid[{row}][{col}] = {grid[row][col]}")
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.