Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Paper 2/Additional Programming Techniques

two-dimensional arrays(2D)

The use of arrays (or equivalent) when solving problems: two-dimensional arrays(2D)

2.2.3 Additional Programming Techniques

Practise exam questions

Additional Programming Techniques — Note 7 of 12

Use of 2D arrays to emulate database tables of a collection of fields, and records

What you need to know

A two-dimensional (2D) array is an array of arrays, like a table or grid with rows and columns. You access elements using two indices: the first selects the row and the second selects the column. 2D arrays can be used to emulate database tables where each row is a record and each column is a field.

What to know

  • A 2D array is an array of arrays, like a grid or table.
  • Access elements using two indices: array[row, column] (OCR ERL) or array[row][column] (Python).
  • The first index selects the row (sub-array); the second selects the item within that row.
  • In OCR pseudocode: journeys[3,1] = "3.5" (row 3, column 1).
  • 2D arrays can emulate database tables: rows = records, columns = fields.
  • An array can only store data of ONE data type. Non-string data must be cast (e.g. int() or float()) when stored as strings.
  • Nested FOR loops are used to iterate through all elements of a 2D array.
  • scores[row][column]: the first index picks the row (the week), the second picks the column (the day). scores[1][2] is week 2, Wednesday.
  • ![1D array indexes from 0, and a 2D array where scores[1][2] is week 2, Wednesday](/knowledge/unit-2-knowledge-images/arrays-1d-2d.svg)
  • WHOLE ARRAY: set the total BEFORE both loops, then add every element inside the inner loop.
  • PER ROW: outer loop over the rows; reset the total INSIDE the outer loop (before the inner loop); inner loop over the columns; output the result after the inner loop.
  • PER COLUMN: swap the loops. The outer loop goes over the columns and the inner loop goes over the rows.

Exam technique

  • Tip:When working with a 2D string array, remember to CAST values to numbers before calculations: total = total + float(events[i,3]).
  • Mistake:For a total per row, setting the total before BOTH loops means it never resets, so each row's total includes all the rows before it.
  • Example:To total values in a 2D array matching a condition: loop through rows, check if events[i,0] == date, then add int(events[i,3]) or float(events[i,3]) to total.

Code examples

2D array operations
python
# 2D array (list of lists in Python)
seating = [
    ["Alice", "Bob"],
    ["Cara", "Dan"]
]

print(seating[0][1])  # "Bob"  (row 0, col 1)
print(seating[1][0])  # "Cara" (row 1, col 0)

# Loop through all elements
for row in range(len(seating)):
    for col in range(len(seating[row])):
        print(seating[row][col], end=" ")
    print()  # New line after each row
2D array: 4 weeks × 5 weekdays
python
# Each row is a week; each column is a day (0 = Monday ... 4 = Friday)
scores = [
    [4, 7, 8, 6, 5],    # Week 1
    [6, 5, 9, 4, 3],    # Week 2
    [10, 3, 6, 7, 2],   # Week 3
    [7, 8, 5, 6, 9]     # Week 4
]

# Week 2 (row 1), Wednesday (column 2)
print(scores[1][2])     # 9

# Every score: outer loop = weeks, inner loop = days
for i in range(4):
    for j in range(5):
        print("Week", i + 1, "Day", j, "Score:", scores[i][j])
Total and average for the whole array
python
# uses the scores array defined above
total = 0
count = 0

for i in range(4):          # each week
    for j in range(5):      # each weekday
        total += scores[i][j]
        count += 1

average = total / count
print("Monthly total:", total)
print("Monthly average:", average)
Total and average per row (each week)
python
# uses the scores array defined above
for i in range(4):              # for each week (row)
    week_total = 0              # reset for EVERY week
    for j in range(5):          # for each day in that week
        week_total += scores[i][j]

    week_average = week_total / 5
    print("Week", i + 1, "Total:", week_total, "Average:", week_average)
Total and average per column (each day)
python
# uses the scores array defined above
for j in range(5):              # for each day (column)
    day_total = 0               # reset for EVERY day
    for i in range(4):          # for each week
        day_total += scores[i][j]

    day_average = day_total / 4
    print("Day", j, "(0 = Mon) Total:", day_total, "Average:", day_average)

Ready to test yourself?

Practise 2.2.3 questions
← Previous noteone-dimensional (1D)2.2.3 Additional Programming TechniquesNext note →Sub programs (functions and procedures) to produce structured code2.2.3 Additional Programming Techniques

Back to Additional Programming Techniques

Revise another OCR GCSE topic: Programming Fundamentals

Additional Programming Techniques

Note 7 of 12

  1. 1Concatenation
  2. 2slicing
  3. 3Open, Read, Write, Close
  4. 4Records to store data
  5. 5SQL to search for data
  6. 6one-dimensional (1D)
  7. 7two-dimensional arrays(2D)
  8. 8Sub programs (functions and procedures) to produce structured code
  9. 9local variables
  10. 10global variables
  11. 11arrays
  12. 12Random number generation
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).