SLIDE 1
CSZone.co.uk
Click to reveal · Arrow keys also work
OCR J277 · Component 2 · Topic 2.2.1c

Programming Techniques
Arrays — 1D and 2D

Store and process multiple values efficiently — in OCR ERL and Python

CSZone OCR GCSE Computer Science J277
Learning Objectives

By the end of this video you will be able to...

Explain what an array is and why it is more efficient than using separate variables for multiple values
Declare, initialise, access and update a 1D array in OCR ERL pseudocode and Python
Use a FOR loop to iterate through all elements of a 1D array using the index
Declare, access and update a 2D array using row and column indices, and iterate through it with nested FOR loops
Explain how 2D arrays emulate database tables — rows as records, columns as fields
⚡ Arrays are one of the most common data structures in programming — and one of the most common topics in OCR exams.
Arrays — Introduction

What is an array — and why use one?

DEFINITION
An array is a fixed-length data structure that stores multiple values of the same data type under a single variable name. Elements are accessed by their index, starting at 0.
WITHOUT AN ARRAY — messy
score185 score272 score391 score465 score578 ← 5 variables, can't loop
KEY PROPERTIES
Fixed-length — size set at declaration, cannot grow
Same type — all elements must be the same data type
0-indexed — first element is always index 0
Named — all elements share one variable name
WITH AN ARRAY — clean
scores ← [85, 72, 91, 65, 78] ← 1 name, can loop through all FOR i = 0 TO 4 print(scores[i]) NEXT i
INDEX DIAGRAM
Index[0][1][2][3][4]
scores8572916578
⚡ Arrays let you process 100 values with the same FOR loop you'd use for 5. Without arrays, you would need to write out every variable by hand — and you can't loop through them.
1D Arrays

Declaring, accessing and updating 1D arrays

OCR ERL — DECLARE AND INITIALISE
// Initialise with values: scores ← [85, 72, 91, 65, 78] // Initialise empty (all zeros): temps ← [0, 0, 0, 0, 0] // Declare with array keyword: array names[3]
All three forms are valid in OCR ERL. The most common in exam questions is the bracket initialisation ← [...]
ACCESSING ELEMENTS
scores[0] ← first element → 85 scores[2] ← third element → 91 scores[4] ← fifth element → 78 print(scores[1]) ← outputs 72
UPDATING ELEMENTS
scores[0] ← 90 ← change first to 90 scores[3] ← 70 ← change fourth to 70 // Read input directly into array: scores[2] ← int(input("Score: "))
PYTHON EQUIVALENT
scores = [85, 72, 91, 65, 78] print(scores[0]) # → 85 scores[0] = 90 # update first
⚡ Index starts at 0. A 5-element array has valid indices 0, 1, 2, 3, 4. Accessing index 5 causes an error — it does not exist. This is the most common array mistake in exams.
1D Arrays

Iterating through a 1D array with a FOR loop

WHY USE A LOOP
A FOR loop uses the index variable to step through each element. For an array of length n, loop from 0 to n−1. Each iteration, use the index to read or write one element.
// OCR ERL — print all 5 scores scores ← [85, 72, 91, 65, 78] FOR i = 0 TO 4 print(scores[i]) NEXT i
OCR ERL — INPUT VALUES INTO ARRAY
scores ← [0, 0, 0, 0, 0] FOR i = 0 TO 4 scores[i] ← int(input("Score: ")) NEXT i ← fills all 5 slots from user input
PYTHON — ITERATE WITH range()
scores = [85, 72, 91, 65, 78] # Using index (like ERL): for i in range(5): # 0,1,2,3,4 print(scores[i]) # Using len() — better practice: for i in range(len(scores)): print(scores[i])
CALCULATE TOTAL AND AVERAGE
total0 FOR i = 0 TO 4 totaltotal + scores[i] NEXT i averagetotal / 5 print(average)
⚡ In OCR ERL, loop 0 TO 4 for a 5-element array. In Python, range(5) gives 0,1,2,3,4 — or use range(len(array)) to work with any size.
2D Arrays

2D arrays — rows and columns

WHAT IS A 2D ARRAY
A 2D array is an array of arrays — a grid with rows and columns. Access any element with two indices: array[row][col]. Both row and column indices start at 0.
OCR ERL — DECLARE 2D ARRAY
// 3 rows, 3 columns — a 3×3 grid: grid ← [[0,0,0],[0,0,0],[0,0,0]] // Or with actual values: board ← [[1,2,3],[4,5,6],[7,8,9]]
PYTHON — DECLARE 2D ARRAY
# 3×3 grid, all zeros: grid = [[0,0,0], [0,0,0], [0,0,0]] # Or with values: board = [[1,2,3],[4,5,6],[7,8,9]]
VISUAL GRID — board[row][col]
col [0]col [1]col [2]
row [0]123
row [1]456
row [2]789
board[1][1] → the highlighted 5
(row 1, column 1 — second row, second column)
⚡ Always think: [row][col] — row first, column second. A 3×3 grid has 3 rows and 3 columns. Valid row indices: 0, 1, 2. Valid column indices: 0, 1, 2.
2D Arrays

Accessing and updating 2D array elements

ACCESS SYNTAX
arrayName[row][col]

First index = row (which list to pick). Second index = column (which element within that list).
board ← [[1,2,3],[4,5,6],[7,8,9]] print(board[0][0]) ← 1 (row 0, col 0) print(board[0][2]) ← 3 (row 0, col 2) print(board[2][0]) ← 7 (row 2, col 0) print(board[2][2]) ← 9 (row 2, col 2)
UPDATING ELEMENTS — OCR ERL
board[0][1] ← 99 ← row 0, col 1 = 99 board[1][2] ← 55 ← row 1, col 2 = 55 // Input directly: board[0][0] ← int(input("Value: "))
PYTHON EQUIVALENT — ACCESS AND UPDATE
board = [[1,2,3],[4,5,6],[7,8,9]] # Access: print(board[1][1]) # → 5 print(board[2][2]) # → 9 # Update: board[0][0] = 100 # row 0, col 0 = 100 print(board[0][0]) # → 100
QUICK REFERENCE — WHAT INDEX MEANS
board[r][c] Position Value
[0][0]Top-left1
[0][2]Top-right3
[2][0]Bottom-left7
[2][2]Bottom-right9
⚡ Identical syntax in OCR ERL and Python: board[row][col]. The only difference is vs = for assignment.
2D Arrays

Iterating through a 2D array — nested FOR loops

WHY NESTED LOOPS
A 2D array needs two loops — an outer loop for rows and an inner loop for columns. For each row, the inner loop steps through every column in that row.
OCR ERL — PRINT ALL ELEMENTS
grid ← [[1,2,3],[4,5,6],[7,8,9]] FOR row = 0 TO 2 FOR col = 0 TO 2 print(grid[row][col]) NEXT col NEXT row ← outputs: 1 2 3 4 5 6 7 8 9
TRACE — OUTER LOOP row=0
rowcolgrid[row][col]output
00grid[0][0]1
01grid[0][1]2
02grid[0][2]3
PYTHON EQUIVALENT
grid = [[1,2,3],[4,5,6],[7,8,9]] for row in range(3): for col in range(3): print(grid[row][col]) # Using len() for flexibility: for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
INNER/OUTER LOOP RULE
Outer loop → controls rows — runs once per row (0, 1, 2)
Inner loop → controls columns — runs once per column for EACH row

Total iterations = rows × columns = 3 × 3 = 9
2D Arrays

2D arrays as database tables

SPEC POINT
The OCR spec states: "2D arrays [can be used] to emulate database tables." Each row is one record. Each column is a field. This lets you store structured, multi-field data without a real database.
EXAMPLE TABLE — 3 students, 3 fields
col[0] Namecol[1] Yearcol[2] Score
row[0]Ali1085
row[1]Beth1192
row[2]Carl1078
OCR ERL DECLARATION
students ← [["Ali", 10, 85], ["Beth", 11, 92], ["Carl", 10, 78]]
ACCESSING FIELDS BY NAME
// Access student 2's name (row 1, col 0): print(students[1][0]) ← "Beth" // Access student 2's score (row 1, col 2): print(students[1][2]) ← 92 // Print all names (column 0): FOR i = 0 TO 2 print(students[i][0]) NEXT i ← Ali Beth Carl
ROWS vs COLUMNS
Row = one complete record (all info about one student)
Column = one field across all records (all names, or all scores)

To loop all records: loop over rows
To find a specific field: fix the col index
Worked Example

Arrays in action — exam-style problem

PROBLEM
Write a program that stores 5 test scores in a 1D array. The user enters each score. Then output the highest score and whether each score is above or below the average.
OCR ERL SOLUTION
scores ← [0,0,0,0,0] FOR i = 0 TO 4 scores[i] ← int(input("Score: ")) NEXT i total0 highestscores[0] FOR i = 0 TO 4 totaltotal + scores[i] IF scores[i] > highest THEN highestscores[i] ENDIF NEXT i avgtotal / 5 print("Highest: " + str(highest))
PYTHON SOLUTION
scores = [0] * 5 for i in range(5): scores[i] = int(input("Score: ")) total = 0 highest = scores[0] for i in range(5): total += scores[i] if scores[i] > highest: highest = scores[i] avg = total / 5 print("Highest:", highest)
KEY TECHNIQUES USED
Initialise array before use, then fill with a FOR loop
Accumulator — total starts at 0, adds each element
Tracker — highest starts at first element, updates if bigger found
Exam Practice

Arrays — exam questions

Question 1 — 1 mark
A programmer declares: temps ← [5, 12, 8, 20, 3]
State the value of temps[3].
Answer — Q1
20 — index 3 is the 4th element (0-indexed: 0=5, 1=12, 2=8, 3=20). (1 mark)
Question 2 — 2 marks
Write OCR ERL pseudocode to declare a 1D array called marks with 4 elements, all initialised to 0. Then write one statement to store 75 in the third element.
Answer — Q2
marks ← [0, 0, 0, 0] ← [1 mark] marks[2] ← 75 ← [1 mark] index 2 = third
Index 2 = third element. A common error is writing marks[3] — that is the fourth element.
Question 3 — 4 marks
A 2D array called data is declared as:
data ← [["Ali",85],["Beth",92],["Carl",78]]

(a) State the value of data[2][0]. (1 mark)
(b) Write OCR ERL pseudocode to output the score of every student using a FOR loop. (3 marks)
Exam Answers

Question 3 — answer and mark scheme

Q3 MARK SCHEME
(a) "Carl" — row 2, column 0 = Carl. (1 mark)

(b)
FOR i = 0 TO 2 ← [1] correct loop range print(data[i][1]) ← [1] correct index [i][1] NEXT i ← [1] NEXT i present
Column index 1 holds the score. Row index uses i because it changes each iteration.
COMMON MARK LOSSES ON THIS Q
• Writing data[i][0] instead of data[i][1] — that prints the name, not the score
• Looping 1 TO 3 instead of 0 TO 2 — off by one, misses row 0
• Forgetting NEXT i — always required in OCR ERL
INDEX REFERENCE — data array
col [0] Namecol [1] Score
row [0]Ali85
row [1]Beth92
row [2]Carl78
data[2][0] → "Carl" (row 2, col 0 highlighted above)
PYTHON EQUIVALENT — FOR REFERENCE
data = [["Ali",85],["Beth",92],["Carl",78]] for i in range(3): print(data[i][1]) # 85, 92, 78
⚡ The key rule: when the first index is fixed, you're selecting a specific column (field). When the first index is a variable like i, you're iterating through all rows. Fix second index to access the same field across all records.
Common Mistakes

Common mistakes — avoid these in the exam

MISTAKE 1 — 1-based indexing
Writing scores[1] to access the first element — arrays are 0-indexed. The first element is always index 0
✓ First element = scores[0] · Third element = scores[2] · Always subtract 1 from the position
MISTAKE 2 — Swapping row and column indices
Writing grid[col][row] when the question means row first — the order matters and produces the wrong element
✓ Always [row][col] — row is the outer index, column is the inner index
MISTAKE 3 — Off-by-one in loop range
Looping FOR i = 1 TO 5 for a 5-element array — this accesses indices 1–5, but valid indices are 0–4. Index 5 does not exist
✓ A 5-element array: FOR i = 0 TO 4 — always start at 0, end at length−1
MISTAKE 4 — Trying to resize an array
Assuming arrays can grow dynamically (like Python lists with .append()) — arrays in OCR ERL are fixed-length. Their size is set at declaration and cannot change
✓ Always declare the full size needed upfront. The spec says: "fixed-length/static structures"
Summary

Key points — 2.2.1c

Arrays store multiple values of the same type under one name. They are fixed-length, 0-indexed, and allow loops to process all elements efficiently
1D array — declared as name ← [v0, v1, v2...]; access with name[i]; update with name[i] ← value; iterate with FOR i = 0 TO length−1
2D array — grid of rows and columns; access with name[row][col]; iterate with nested FOR loops — outer for rows, inner for columns
Database tables — 2D arrays emulate tables: each row is a record, each column is a field. Fix the column index to access the same field across all rows
Key rule — always 0-indexed. First element = index 0. A 5-element array uses indices 0–4. Loop from 0 to length−1. Swap row and column and you get the wrong element
⚡ Next topic: 2.2.1d — File Handling. We'll look at how programs open, read, write and close files.
2.2.1c Complete

Arrays
1D and 2D

Get the full resource pack at CSZone.co.uk

📄
Marked Worksheet
CSZone.co.uk
Quiz
CSZone.co.uk
📊
Slides
CSZone.co.uk
Next Up
2.2.1d — File Handling