TopMyGrade

GCSE/Computer Science/AQA

CS2.5Data structures: 1D and 2D arrays (lists), records; choosing a structure to fit a problem

Notes

Arrays and records

A data structure organises related data so a program can store and retrieve it efficiently. AQA GCSE covers two: arrays (one and two dimensional) and records.

Arrays (1D)

A 1-D array is an ordered list of items of the same type, accessed by an integer index. AQA pseudocode is 0-indexed, so the first element is at position 0.

SET names TO ["Aisha", "Ben", "Chloe", "Dan"]
OUTPUT names[0]   -- prints "Aisha"
OUTPUT names[3]   -- prints "Dan"
SET names[1] TO "Ben Senior"
  • The whole array has a size — LEN(names) returns 4.
  • Use a loop to process every element:
FOR i FROM 0 TO LEN(names) - 1
  OUTPUT names[i]
ENDFOR

Why use arrays?

  • Fixed memory — array contents are contiguous in memory; access by index is constant-time.
  • Loops over data — easy to iterate.
  • Algorithms — searches and sorts are written for arrays.

2D arrays

A 2-D array is a grid (rows × columns). It's like an array of arrays. Access uses two indices: grid[row][col].

SET seating TO [
  ["Anna", "Ben", "Carla"],
  ["Dani", "Eli", "Faye"]
]
OUTPUT seating[0][0]  -- "Anna"
OUTPUT seating[1][2]  -- "Faye"

Use a nested loop to process every cell:

FOR row FROM 0 TO LEN(seating) - 1
  FOR col FROM 0 TO LEN(seating[row]) - 1
    OUTPUT seating[row][col]
  ENDFOR
ENDFOR

Common 2D-array uses: chess boards, spreadsheets, image pixel grids.

Records

A record groups together related data of possibly different types under one name. Each piece is a field with its own name. (In some languages this is called a "struct" or "object".)

RECORD Student
  name  : string
  age   : integer
  grade : character
ENDRECORD

SET pupil1 TO Student("Aisha", 15, 'A')
OUTPUT pupil1.name    -- "Aisha"
OUTPUT pupil1.grade   -- 'A'

Records are perfect when each item has many attributes — a Book has title, author, ISBN, year — that don't share a type.

Choosing array vs record

  • All items the same type, ordered by position → array.
  • Several different attributes per item → record.
  • Many items, each with several attributes → array of records.
RECORD Student ... ENDRECORD
SET students TO [Student("Aisha", 15, 'A'), Student("Ben", 14, 'B'), ...]
OUTPUT students[0].name

Common mistakesPitfalls

  1. Off-by-one indexing. First index is 0; last is LEN − 1.
  2. Index out of bounds. names[10] on a 4-element list crashes.
  3. Confusing rows and columns in 2D arrays. grid[row][col] — row first.
  4. Forgetting that arrays are fixed-size in some languages (Python lists grow; static arrays don't).
  5. Comparing two arrays with = rarely works — usually compares references, not contents.

Worked exampleWorked examples

Example 1. Find the largest number in an array.

SET largest TO numbers[0]
FOR i FROM 1 TO LEN(numbers) - 1
  IF numbers[i] > largest THEN
    SET largest TO numbers[i]
  ENDIF
ENDFOR
OUTPUT largest

Example 2. Count how many "A" grades in a 2D array results[year][student]:

SET count TO 0
FOR y FROM 0 TO LEN(results) - 1
  FOR s FROM 0 TO LEN(results[y]) - 1
    IF results[y][s] = 'A' THEN
      SET count TO count + 1
    ENDIF
  ENDFOR
ENDFOR

Example 3. Define a Book record and store an array of them:

RECORD Book
  title  : string
  author : string
  year   : integer
ENDRECORD

SET library TO [
  Book("Dune", "Herbert", 1965),
  Book("1984", "Orwell", 1949)
]

Try thisQuick check

What does grid[2][1] refer to in a 4 × 3 grid? Row 2 (third row, since 0-indexed), column 1 (second column).

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 14 marks

    Array indexing

    Given the array names = ["Anna", "Ben", "Carla", "Dan"], state the values of (a) names[0], (b) names[2], (c) LEN(names), (d) names[LEN(names) - 1].

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 23 marks

    Iterate over array

    Write pseudocode that outputs every element of an integer array marks, one per line.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 34 marks

    2D array element

    A 2D array board represents a 3 × 3 noughts-and-crosses grid. State the index expression for (a) the top-right corner, (b) the centre.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 44 marks

    Record definition

    Define a record type to hold a customer's name, age and account balance.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 54 marks

    Array vs record

    When would you use an array, and when would you use a record? Give one example of each.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    Find largest

    Write pseudocode that finds the largest value in an integer array nums. Assume the array has at least one element.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    Off-by-one

    What is wrong with this code intended to print every element of an array a of length 5?

    FOR i FROM 1 TO 5
      OUTPUT a[i]
    ENDFOR
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.5 — Data structures: arrays (1D and 2D) and records

11-card SR deck for AQA GCSE Computer Science topic CS2.5

11 cards · spaced repetition (SM-2)