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.
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:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.