Data Structures
Storing 100 student names in 100 separate variables is horribly inefficient. We use Data Structures like Arrays and Records to organise data logically.
Organising Information
1D Arrays
A single list of items. In OCR J277 Core Algorithms, arrays are considered static (fixed-length), meaning they cannot grow in size once created. They can only hold one data type at a time.
names = ["Alan", "Ada", "Tim"]
# Accessing the second element (Index 1)
print(names[1]) -> "Ada"
Records
A record is used to store data about a single entity, but importantly, it can hold multiple different data types.
Name: "Alan" (String)
Age: 16 (Integer)
Passed: True (Boolean)
Examiner's Eye - Array vs Record Trap
If an exam question asks "State the most appropriate data structure to hold a student's name, their age, and whether they have paid for lunch", you must answer Record. An array is incorrect because an array can strictly only hold one data type at a time (e.g. ALL Strings or ALL Integers). Since the data types differ (String, Int, Boolean), only a Record will work.
2D Array / Database Emulation Explorer
A 2D Array is an "Array of Arrays", forming a grid. OCR loves to ask how 2D Arrays emulate databases. Click the buttons to visualise how Rows and Columns map to Database terminology.
| Index | Col [0] | Col [1] | Col [2] |
|---|---|---|---|
| Row [0] | "Ada" | "Lovelace" | "London" |
| Row [1] | "Alan" | "Turing" | "Bletchley" |
| Row [2] | "Grace" | "Hopper" | "New York" |
Click the buttons above to see how 2D arrays emulate a database matrix.
To extract "Turing", you address grid[1][1].
Check Your Understanding
1. Why is an Array considered a 'static' data structure?
2. When a 2D Array is used to emulate a database table, what does a Single Column represent?
3. I need to store a User's ID (Integer), Level (Integer), and BannedStatus (Boolean). Which data structure MUST I use?
Written Exam Scenario (AO2/AO3)
Stretch (Grade 9)"A school stores its register in a 1D Array called students which contains exactly 30 names. Write an algorithm that uses Iteration to output every name in the array." (4 marks)
FOR i = 0 to 29:
print( students[i] )
NEXT i
Mark 1 for identifying a FOR loop is needed.
Mark 2 for the correct bounds (0 to 29). Since Arrays start at Index 0, thirty items goes from 0 to 29.
Mark 3 for using the print/output command.
Mark 4 for successfully placing the index variable 'i' inside the array brackets
students[i].