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 lists as required by OCR J277 Section 2.3. Arrays are data structures that store multiple values under a single name, accessed by an index. Understanding arrays is essential for working with collections of data and is heavily tested in the OCR GCSE Computer Science exam.
An array is an ordered collection of elements, all of the same data type, stored under a single variable name. Each element is accessed by its index (position number). In most programming contexts (and in OCR pseudocode), arrays use zero-based indexing — the first element is at index 0.
OCR Pseudocode:
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
print(names[0]) // Output: Alice
print(names[2]) // Output: Charlie
print(names[4]) // Output: Eve
Python:
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
print(names[0]) # Output: Alice
print(names[2]) # Output: Charlie
print(names[4]) # Output: Eve
OCR Exam Tip: Arrays in OCR use zero-based indexing. The first element is at index 0, and the last element of an array with n elements is at index n-1. A very common exam mistake is being "off by one" — always check your indices carefully.
The relationship between an array, its zero-based indices, and the values stored in each slot can be drawn like this:
flowchart LR
subgraph names["names array"]
direction LR
I0["[0]<br/>Alice"]
I1["[1]<br/>Bob"]
I2["[2]<br/>Charlie"]
I3["[3]<br/>Diana"]
I4["[4]<br/>Eve"]
end
A["names[2]"] --> I2
I2 --> B["returns ’Charlie’"]
A one-dimensional (1D) array is a simple list of values arranged in a single row.
OCR Pseudocode:
// Pre-populated array
scores = [85, 92, 78, 90, 88]
// Empty array with 5 elements
array results[5]
Python:
# Pre-populated list
scores = [85, 92, 78, 90, 88]
# Empty list
results = []
# List with 5 zeros
results = [0] * 5
scores = [85, 92, 78, 90, 88]
print(scores[1]) // Output: 92
scores[2] = 95 // Change element at index 2 from 78 to 95
print(scores[2]) // Output: 95
OCR Pseudocode:
scores = [85, 92, 78, 90, 88]
total = 0
for i = 0 to scores.length - 1
total = total + scores[i]
next i
average = total / scores.length
print("Average: " + str(average))
Python:
scores = [85, 92, 78, 90, 88]
total = 0
for i in range(len(scores)):
total += scores[i]
average = total / len(scores)
print("Average:", average)
A two-dimensional (2D) array is an array of arrays — like a table with rows and columns. Each element is accessed using two indices: one for the row and one for the column.
OCR Pseudocode:
// A 3x3 grid (3 rows, 3 columns)
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(grid[0][0]) // Output: 1 (row 0, column 0)
print(grid[1][2]) // Output: 6 (row 1, column 2)
print(grid[2][1]) // Output: 8 (row 2, column 1)
Python:
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(grid[0][0]) # Output: 1
print(grid[1][2]) # Output: 6
print(grid[2][1]) # Output: 8
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
grid[1][2] = Row 1, Column 2 = 6
OCR Pseudocode:
for row = 0 to 2
for col = 0 to 2
print(grid[row][col])
next col
next row
Python:
for row in range(3):
for col in range(3):
print(grid[row][col], end=" ")
print() # New line after each row
OCR Exam Tip: 2D array questions often appear on Paper 2. The key is knowing the syntax:
array[row][column]. Always think "row first, then column". Drawing the 2D array as a table can help you trace through the code correctly.
OCR Pseudocode:
// 2D array: 5 students, each with [name, mark]
students = [["Alice", 85],
["Bob", 72],
["Charlie", 91],
["Diana", 68],
["Eve", 95]]
// Find the student with the highest mark
highestMark = 0
topStudent = ""
for i = 0 to 4
if students[i][1] > highestMark then
highestMark = students[i][1]
topStudent = students[i][0]
endif
next i
print("Top student: " + topStudent + " with " + str(highestMark))
Python:
students = [["Alice", 85], ["Bob", 72], ["Charlie", 91],
["Diana", 68], ["Eve", 95]]
highest_mark = 0
top_student = ""
for i in range(len(students)):
if students[i][1] > highest_mark:
highest_mark = students[i][1]
top_student = students[i][0]
print(f"Top student: {top_student} with {highest_mark}")
[row][column]..length gives the number of elements in an array.OCR Exam Tip: When working with arrays in the exam, always write out the index numbers next to the values to avoid off-by-one errors. For the array
[10, 20, 30, 40], write: index 0=10, index 1=20, index 2=30, index 3=40.
A class of 5 students has taken a test. Their marks are stored in a 1D array. Calculate the average mark and identify the highest mark.
OCR reference language pseudocode:
marks = [72, 85, 63, 91, 58]
total = 0
highest = marks[0]
for i = 0 to marks.length - 1
total = total + marks[i]
if marks[i] > highest then
highest = marks[i]
endif
next i
average = total / marks.length
print("Average: " + str(average))
print("Highest: " + str(highest))
Step-by-step trace:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.