Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Additional Programming Techniques

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

All topicsPractise exam questions
Knowledge organiser

Additional Programming Techniques

2.2.3g

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.

Key points

  • 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.
  • Exam Tip:When working with a 2D string array, remember to CAST values to numbers before calculations: total = total + float(events[i,3]).
  • Exam 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