🔒
Unlock Everything
£7.99/month
or £59/year
Subscribe now →
💻 Component 2 · 2.2 Programming
2.2.1d Arrays
OCR J277 · GCSE Computer Science · ~10 min read
Notes
Video
Slides
Worksheet
Quiz

What is an Array?

An array is a data structure that stores multiple values of the same data type under a single variable name. Each value is accessed using an index (position number).

In OCR J277, arrays are zero-indexed — the first element is at index 0, not 1.

Declaring and Initialising Arrays

// 1D array of integers — 5 elements
scores = [85, 72, 91, 64, 78]

// 1D array of strings
names = ["Alice", "Bob", "Charlie"]

// Accessing elements by index
OUTPUT scores[0]  // outputs 85 (first element)
OUTPUT scores[4]  // outputs 78 (last element)
OUTPUT names[1]   // outputs "Bob"

Array Structure — Visual

Index01234
scores8572916478

Key fact: A 5-element array has indices 0, 1, 2, 3, 4. The last valid index is always length - 1.

Modifying Array Elements

// Assigning a new value to an element
scores[2] = 95  // changes 91 to 95 at index 2
names[0] = "Anna"  // changes "Alice" to "Anna"

Traversing an Array with a FOR Loop

The most common way to process every element in an array is a FOR loop using the index.

// Print all elements
FOR i = 0 TO 4
    OUTPUT scores[i]
NEXT i

// Find the sum of all elements
total = 0
FOR i = 0 TO 4
    total = total + scores[i]
NEXT i
OUTPUT total

// Find the maximum value
maxVal = scores[0]
FOR i = 1 TO 4
    IF scores[i] > maxVal THEN
        maxVal = scores[i]
    END IF
NEXT i
OUTPUT maxVal

2D Arrays

A 2D array is a table (grid) of values, accessed using two indices: array[row][column]. Think of a spreadsheet or seating plan.

// 2D array — 3 rows, 3 columns
grid = [[1,2,3],[4,5,6],[7,8,9]]
OUTPUT grid[0][0]  // 1 (row 0, col 0)
OUTPUT grid[1][2]  // 6 (row 1, col 2)
OUTPUT grid[2][1]  // 8 (row 2, col 1)

// Traverse all elements with nested FOR loops
FOR row = 0 TO 2
    FOR col = 0 TO 2
        OUTPUT grid[row][col]
    NEXT col
NEXT row

2D Array — Visual (grid above)

Col 0Col 1Col 2
Row 0123
Row 1456
Row 2789
Exam tip: Arrays are almost always tested with FOR loops — know how to traverse, find the sum, find the max/min, and search an array. For a 2D array, always say array[row][column]. OCR J277 arrays are zero-indexed — the first element is [0], not [1]. A common question: "Given array scores = [40,75,90,55,85], what is scores[2]?" — answer is 90 (index 2 = third element).
⚠️ Common Mistakes
  • Starting the index at 1 instead of 0 — arrays in OCR J277 are zero-indexed
  • Accessing scores[5] in a 5-element array — valid indices are 0–4; index 5 is out of bounds
  • Storing different data types in one array — arrays hold values of the same type
  • In 2D arrays, confusing row and column: grid[row][column] not grid[column][row]
  • Forgetting to iterate from 0, not 1: FOR i = 0 TO length-1
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.2.1d Arrays

8 questions · 20 marks

Q1Define what an array is and state two properties of an array.[3]
✅ Mark scheme
An array is a data structure that stores multiple values under a single variable name [1]. Properties (any 2): all elements are the same data type [1]; elements are accessed using an index [1]; arrays are zero-indexed in OCR J277 (first element at index 0) [1]; arrays have a fixed size [1].
Q2Given: grades = ["A","B","C","D","E"]. What is the value of grades[2]? What is the index of "E"?[2]
✅ Mark scheme
grades[2] = "C" [1] (zero-indexed: A=0, B=1, C=2). The index of "E" is 4 [1].
Q3Write pseudocode to declare an array called temps with values 12, 15, 9, 18, 21, then output all values using a FOR loop.[3]
✅ Mark scheme
temps = [12,15,9,18,21] [1]; FOR i = 0 TO 4 [1]; OUTPUT temps[i]; NEXT i [1]. Award marks for correct declaration, correct loop bounds (0 to 4), and access with index.
Q4Write pseudocode to calculate and output the total of all values in the array scores = [40, 75, 90, 55, 85].[3]
✅ Mark scheme
scores = [40,75,90,55,85]; total = 0 [1]; FOR i = 0 TO 4; total = total + scores[i] [1]; NEXT i; OUTPUT total [1]. Answer: total = 345.
Q5Explain what a 2D array is and give an example of when you might use one.[2]
✅ Mark scheme
A 2D array is a grid/table of values accessed using two indices [row][column] [1]. Example: storing a seating plan, a grid for a game (e.g. noughts and crosses), a timetable, or marks for multiple students across multiple tests [1].
Q6Given grid = [[5,10,15],[20,25,30]], what is the value of grid[1][2]?[1]
✅ Mark scheme
30 [1]. Row 1 = [20,25,30]. Column 2 = 30.
Q7Write pseudocode to find and output the largest value in nums = [34, 17, 58, 42, 9].[4]
✅ Mark scheme
nums = [34,17,58,42,9] [½]; maxVal = nums[0] [1]; FOR i = 1 TO 4 [1]; IF nums[i] > maxVal THEN maxVal = nums[i]; END IF [1]; NEXT i; OUTPUT maxVal [½]. Answer: 58.
Q8State one advantage of using an array over separate variables to store 100 student scores.[2]
✅ Mark scheme
Any one with explanation: you only need one variable name instead of 100 (score1, score2... score100) [1]; you can process all values with a single FOR loop rather than 100 separate lines [1]; code is shorter, easier to read and maintain [1].
?
out of 20 — self-mark above
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
Complete!
TermDefinition
🎯

Mini Test — 2.2.1d Arrays

10 questions · 10 marks · 10 minutes

← 2.2.1c Sequence, Selection & Iteration 2.2 Programming 2.2.1e Subroutines →