Intermediate

Arrays and Abstract Data Types

AicademyAicademy
·A-Level Computer Science·AQA 7517·5 min
4.2.1 Data structures and abstract data types: arrays, files, ADTs

Arrays

An array is a data structure that stores a fixed number of elements of the same data type in contiguous memory locations. Each element is accessed by its index — an integer that specifies its position.

AQA pseudocode uses 1-based indexing by convention (first element is at index 1). Check whether your language uses 0-based (Python, Java, C++) or 1-based indexing.

1D array declaration and access:

DECLARE scores[5] : INTEGER      // Array of 5 integers

scores[1] ← 85
scores[2] ← 72
scores[3] ← 91
scores[4] ← 67
scores[5] ← 88

OUTPUT scores[3]    // 91

Array traversal — process every element using a loop:

total ← 0
FOR i ← 1 TO 5
    total ← total + scores[i]
NEXT i
average ← total / 5

Two-Dimensional Arrays

A 2D array stores data in rows and columns — like a table or grid. It is accessed with two indices: array[row][column].

DECLARE grid[3][3] : INTEGER    // 3×3 grid

// Populate with zeroes
FOR r ← 1 TO 3
    FOR c ← 1 TO 3
        grid[r][c] ← 0
    NEXT c
NEXT r

grid[2][3] ← 7    // Row 2, column 3 = 7
OUTPUT grid[2][3] // 7

Example use: a chessboard is an 8×8 2D array; a multiplication table is a 12×12 2D array; an image is a 2D array of pixel values.

Multi-dimensional arrays extend to 3 or more dimensions — for example, a 3D array could represent a data cube (rows × columns × depth).

File Operations

Programs often need to persist data beyond a single run by reading from and writing to files. AQA requires knowledge of:

OperationAQA pseudocodeEffect
Open for readingOPENFILE "data.txt" FOR READOpens file; positions at start
Open for writingOPENFILE "data.txt" FOR WRITEOpens file; overwrites content
Open for appendingOPENFILE "data.txt" FOR APPENDOpens file; positions at end
Read one lineREADFILE "data.txt", lineReads next line into line
Write one lineWRITEFILE "data.txt", valueWrites value then newline
Check end of fileEOF("data.txt")Returns True if at end
CloseCLOSEFILE "data.txt"Closes file; writes buffered data

Worked example — read all lines from a file and output each:

OPENFILE "students.txt" FOR READ
WHILE NOT EOF("students.txt") DO
    READFILE "students.txt", name
    OUTPUT name
ENDWHILE
CLOSEFILE "students.txt"

Files must be closed after use. Unclosed files can cause data loss (unflushed buffer) or prevent other programs from accessing the file.

Sequential and Random Access

Sequential access — data must be read from the beginning in order. Reading record 50 requires reading records 1 through 49 first. Used for log files, streaming data.

Random access — any record can be accessed directly by its position (address). Used for databases and binary files where records have a fixed size.

Sequential access:
[Rec1][Rec2][Rec3]...[Rec50]  ← must read all previous records

Random access:
[Rec1][Rec2][Rec3]...[Rec50]  ← jump directly to RecN using address
                                  address = start + (N-1) × record_size

Magnetic tape only supports sequential access. Hard disks and SSDs support random access.

How much of this have you taken in?

Quiz yourself on this section — free, no card needed.

Test myself

Abstract Data Types

An abstract data type (ADT) defines a data structure in terms of its behaviour — the operations it supports — without specifying how it is implemented internally.

This separation is important:

  • Code that uses an ADT depends only on its defined interface
  • The implementation can change (e.g. from array-based to linked-list-based) without affecting any calling code
  • Different implementations can be substituted without rewriting user code
ADTCore operationsDefining property
Stackpush, pop, peek, isEmptyLIFO
Queueenqueue, dequeue, isEmptyFIFO
Listappend, insert, delete, search, getOrdered, indexed
Dictionaryset, get, delete, containsKey-value lookup
GraphaddVertex, addEdge, getNeighboursVertex-edge structure

Interface vs implementation:

Interface (what):       Stack.push(item), Stack.pop(), Stack.isEmpty()
Implementation (how):   Could use an array with a top pointer,
                        or a linked list with a head pointer —
                        the calling code cannot tell the difference.

Common Exam Mistakes

1. Using the wrong index base

AQA pseudocode is 1-indexed; Python is 0-indexed. Accessing scores[0] in AQA pseudocode is typically out of bounds. Be explicit about which convention you are using in exam answers.

2. Declaring an array too small by one

If you need to store 10 items, declare DECLARE arr[10] — not DECLARE arr[9]. Off-by-one errors here produce index-out-of-bounds errors at runtime.

3. Not closing files after use

Forgetting CLOSEFILE may leave data unwritten (buffered but not flushed to disk) or prevent other processes from accessing the file.

4. Conflating an ADT with its implementation

A stack is an ADT — it could be implemented using an array or a linked list. Describing a stack as "an array" in an exam answer conflates the abstraction with one particular implementation.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Object-Oriented Programming

Next

Stacks and Queues

Related lessons

7 Slides

Lesson

Stacks and Queues

A-Level Computer Science · AQA 7517

8 days ago

8 Slides

Lesson

Programming Fundamentals

A-Level Computer Science · AQA 7517

10 hours ago

6 Slides

Lesson

Graphs

A-Level Computer Science · AQA 7517

10 hours ago

7 Slides

Lesson

Hash Tables and Dictionaries

A-Level Computer Science · AQA 7517

10 hours ago

7 Slides

Lesson

Binary Search Trees

A-Level Computer Science · AQA 7517

7 days ago