CS10: Data Structures
Understanding 1D arrays, 2D arrays, and records - how to declare, access, iterate, and use them in practical programs.
Understanding 1D arrays, 2D arrays, and records - how to declare, access, iterate, and use them in practical programs.
| Data Structure | Description | When to Use |
|---|---|---|
| 1D Array | A list of items of the same type, accessed by index | Storing a list of scores, names, or temperatures |
| 2D Array | A grid of items in rows and columns, accessed by two indices | Storing a seating plan, grid, or spreadsheet |
| Record | A collection of named fields of different types | Storing related data about one entity (e.g. a student) |
scores ← [85, 92, 78, 90, 67] // Array of 5 integers names ← ["Ali", "Beth", "Carla"] // Array of 3 strings
scores ← [85, 92, 78, 90, 67] OUTPUT scores[0] // 85 (first element) OUTPUT scores[2] // 78 (third element) OUTPUT scores[4] // 67 (fifth/last element)
scores ← [85, 92, 78, 90, 67] scores[2] ← 88 // Change third element from 78 to 88 OUTPUT scores[2] // Now outputs 88
scores ← [85, 92, 78, 90, 67]
FOR i ← 0 TO 4
OUTPUT scores[i]
NEXT i
// Outputs: 85, 92, 78, 90, 67
scores ← [85, 92, 78, 90, 67]
total ← 0
FOR i ← 0 TO 4
total ← total + scores[i]
NEXT i
OUTPUT "Total: ", total // 412
OUTPUT "Average: ", total / 5 // 82.4
scores ← [85, 92, 78, 90, 67]
maxScore ← scores[0]
FOR i ← 1 TO 4
IF scores[i] > maxScore THEN
maxScore ← scores[i]
ENDIF
NEXT i
OUTPUT "Highest: ", maxScore // 92
grid ← [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
This creates a 3x3 grid (3 rows, 3 columns).
grid ← [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
OUTPUT grid[0][0] // 1 (row 0, column 0)
OUTPUT grid[1][2] // 6 (row 1, column 2)
OUTPUT grid[2][1] // 8 (row 2, column 1)
grid ← [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
FOR row ← 0 TO 2
FOR col ← 0 TO 2
OUTPUT grid[row][col]
NEXT col
NEXT row
// Outputs all 9 numbers in order
seats ← [["Empty", "Bob", "Empty"],
["Alice", "Empty", "Carl"],
["Empty", "Dana", "Empty"]]
OUTPUT seats[1][0] // "Alice" (row 1, seat 0)
seats[0][0] ← "Eve" // Assign Eve to row 0, seat 0
TYPE Student
name AS STRING
age AS INTEGER
tutorGroup AS STRING
averageGrade AS REAL
ENDTYPE
student1 ← NEW Student() student1.name ← "Alice" student1.age ← 16 student1.tutorGroup ← "10B" student1.averageGrade ← 7.5 OUTPUT student1.name // "Alice" OUTPUT student1.averageGrade // 7.5
TYPE Student
name AS STRING
score AS INTEGER
ENDTYPE
class ← [NEW Student(), NEW Student(), NEW Student()]
class[0].name ← "Alice"
class[0].score ← 85
class[1].name ← "Bob"
class[1].score ← 72
class[2].name ← "Carla"
class[2].score ← 91
FOR i ← 0 TO 2
OUTPUT class[i].name, ": ", class[i].score
NEXT i
| Feature | Array | Record |
|---|---|---|
| Access method | By index number | By field name |
| Data types | All elements same type | Each field can be different type |
| When to use | List of similar items | Related data about one thing |
| Example | List of scores [85, 92, 78] | Student {name, age, grade} |
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Accessing index 5 in a 5-element array | Indices go from 0 to 4 for length 5 | Use LENGTH(array) - 1 for the last index |
| Confusing row and column in 2D arrays | grid[0][1] and grid[1][0] are different | Remember: grid[row][column] |
| Forgetting to initialise array elements | Uninitialised elements may have unknown values | Set all elements before reading them |
| Using wrong data type in arrays | Arrays hold one type only | Use records if you need different types |
Q1: Write pseudo-code to declare an array of 5 temperatures and output the third temperature.
Q2: Write pseudo-code that finds the sum of all elements in the array [10, 20, 30, 40, 50].
Q3: A 2D array represents a 3x3 tic-tac-toe board. How would you access the centre square?
Q4: Define a record called Car with fields for make (string), model (string), year (integer), and price (real).
Q5: Write pseudo-code that iterates through an array of names and outputs only the names that start with "A".
temps ← [18.5, 20.3, 22.1, 19.8, 21.0] OUTPUT temps[2] // Outputs 22.1 (third element, index 2)
numbers ← [10, 20, 30, 40, 50]
total ← 0
FOR i ← 0 TO 4
total ← total + numbers[i]
NEXT i
OUTPUT total // 150
board[1][1]TYPE Car
make AS STRING
model AS STRING
year AS INTEGER
price AS REAL
ENDTYPE
names ← ["Alice", "Bob", "Amy", "Charlie", "Anna"]
FOR i ← 0 TO 4
IF SUBSTRING(names[i], 0, 1) = "A" THEN
OUTPUT names[i]
ENDIF
NEXT i
✗ Thinking arrays and records are the same thing ✓ An array stores multiple values of the SAME data type accessed by index; a record stores related values of DIFFERENT data types accessed by field name.
✗ Confusing 0-based and 1-based indexing ✓ Most programming languages use 0-based indexing (first element is index 0). Some pseudo-code uses 1-based. Always check the exam board convention.
✗ Forgetting that arrays have a fixed size once declared ✓ In most GCSE-level languages, arrays have a fixed size declared at creation. You cannot add or remove elements beyond the declared size.
✗ Believing a 2D array is a completely different concept from a 1D array ✓ A 2D array is an array of arrays — each row is itself a 1D array. Access uses two indices: array[row][column].
A program needs to store the names and test scores of 5 students. Explain why a record is more suitable than two separate arrays, and write pseudo-code to declare and use this data structure. [5 marks]
Using two separate arrays (one for names, one for scores) keeps related data in separate structures, making it harder to keep data together when sorting or passing to subroutines. A record groups related fields of different types into one unit. TYPE Student name : STRING score : INTEGER ENDTYPE declare students : ARRAY[1:5] OF Student students[1].name ← 'Alice' students[1].score ← 87 students[2].name ← 'Bob' students[2].score ← 72 This keeps each student's name and score together as a single unit, making the data easier to manage, sort, and pass to subroutines.
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including data structures: arrays and records for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including data structures: arrays and records to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including data structures: arrays and records, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.