Data Structures

AS Level — Unit 1: Fundamentals of Computer Science

Arrays (One-Dimensional and Two-Dimensional)

An array is a static data structure that stores a fixed number of elements of the same data type under a single identifier. Elements are stored in contiguous memory locations, meaning they sit next to each other in memory. Each element is accessed using an index (position number).

An array is an ordered, fixed-size collection of elements of the same data type, stored in contiguous memory locations. Each element is accessed by its index.

One-Dimensional Arrays

A 1D array is a linear sequence of elements — essentially a single list. Most languages use zero-based indexing, where the first element has index 0.

# Python — declaring and using a 1D array (list)
scores = [95, 82, 89, 74, 91]

# Accessing elements
print(scores[0])    # Output: 95 (first element)
print(scores[4])    # Output: 91 (last element)

# Modifying an element
scores[2] = 100
print(scores)       # Output: [95, 82, 100, 74, 91]

# Iterating through the array
for i in range(len(scores)):
    print(f"Index {i}: {scores[i]}")
' VB.NET — declaring and using a 1D array
Dim scores(4) As Integer  ' Declares an array with 5 elements (index 0 to 4)
scores(0) = 95
scores(1) = 82
scores(2) = 89
scores(3) = 74
scores(4) = 91

' Accessing elements
Console.WriteLine(scores(0))    ' Output: 95
Console.WriteLine(scores(4))    ' Output: 91

' Modifying an element
scores(2) = 100

' Iterating through the array
For i As Integer = 0 To scores.Length - 1
    Console.WriteLine("Index " & i & ": " & scores(i))
Next

Two-Dimensional Arrays

A 2D array is a “table” or “grid” of elements organised into rows and columns. It can be thought of as an array of arrays. To access an element you need two indices: the row index and the column index.

# Python — 2D array (list of lists)
# A 3x4 grid representing student marks for 3 students across 4 tests
marks = [
    [72, 85, 90, 68],   # Student 0
    [55, 60, 75, 80],   # Student 1
    [91, 88, 95, 79]    # Student 2
]

# Access element at row 1, column 2
print(marks[1][2])   # Output: 75

# Iterate through all elements using nested loops
for row in range(len(marks)):
    for col in range(len(marks[row])):
        print(f"marks[{row}][{col}] = {marks[row][col]}", end="  ")
    print()  # New line after each row
' VB.NET — 2D array
Dim marks(2, 3) As Integer  ' 3 rows (0-2), 4 columns (0-3)
marks(0, 0) = 72 : marks(0, 1) = 85 : marks(0, 2) = 90 : marks(0, 3) = 68
marks(1, 0) = 55 : marks(1, 1) = 60 : marks(1, 2) = 75 : marks(1, 3) = 80
marks(2, 0) = 91 : marks(2, 1) = 88 : marks(2, 2) = 95 : marks(2, 3) = 79

' Access element at row 1, column 2
Console.WriteLine(marks(1, 2))   ' Output: 75

' Iterate through all elements using nested loops
For row As Integer = 0 To 2
    For col As Integer = 0 To 3
        Console.Write("marks(" & row & "," & col & ") = " & marks(row, col) & "  ")
    Next
    Console.WriteLine()
Next

Key Properties of Arrays

Property Detail
Size Fixed at declaration (static)
Data type All elements must be the same type
Access Direct access via index — O(1) time
Memory Contiguous allocation
Searching Linear search O(n); binary search O(log n) if sorted
Insertion/Deletion Expensive — elements must be shifted

When working with 2D arrays, always remember the convention: first index = row, second index = column. Nested loops are required to traverse all elements — the outer loop iterates over rows and the inner loop iterates over columns.


Records (Structs)

A record (sometimes called a struct) is a data structure that groups together related items of different data types under a single identifier. Unlike arrays, where all elements share the same type, a record can hold a mix of strings, integers, booleans, and other types.

A record is a data structure that stores a collection of related fields, each of which can be a different data type. Each field is accessed by its field name rather than an index.

Defining and Using Records

# Python — using a class to represent a record
class Student:
    def __init__(self, student_id, name, date_of_birth, mark):
        self.student_id = student_id
        self.name = name
        self.date_of_birth = date_of_birth
        self.mark = mark

# Creating record instances
student1 = Student("S101", "Alice Smith", "2007-03-15", 85)
student2 = Student("S102", "Bob Jones", "2006-11-22", 72)

# Accessing fields
print(student1.name)          # Output: Alice Smith
print(student2.mark)          # Output: 72

# Modifying a field
student1.mark = 90

# An array of records
students = [student1, student2]
for s in students:
    print(f"{s.student_id}: {s.name} scored {s.mark}")
' VB.NET — using a Structure to represent a record
Structure Student
    Dim StudentID As String
    Dim Name As String
    Dim DateOfBirth As Date
    Dim Mark As Integer
End Structure

' Creating and using records
Dim student1 As Student
student1.StudentID = "S101"
student1.Name = "Alice Smith"
student1.DateOfBirth = #03/15/2007#
student1.Mark = 85

Dim student2 As Student
student2.StudentID = "S102"
student2.Name = "Bob Jones"
student2.DateOfBirth = #11/22/2006#
student2.Mark = 72

' Accessing fields
Console.WriteLine(student1.Name)     ' Output: Alice Smith
Console.WriteLine(student2.Mark)     ' Output: 72

' An array of records
Dim students(1) As Student
students(0) = student1
students(1) = student2
For Each s As Student In students
    Console.WriteLine(s.StudentID & ": " & s.Name & " scored " & s.Mark)
Next

Arrays vs Records

Feature Array Record
Data types All elements same type Fields can be different types
Access method By index (e.g., arr[0]) By field name (e.g., student.name)
Purpose Store list of similar items Group related but different attributes
Size Fixed number of elements Fixed number of fields

Manipulating Arrays and Records

The specification asks you to describe the manipulation of records and arrays, so you need the standard operations, not just the definitions.

Operation Array Record
Read a value By index: scores[2] By field name: student.name
Update a value scores[2] = 75 student.name = "Ali"
Traverse Loop the index from 0 to length-1 (nested loops for 2D) Access each field in turn
Search Linear search, or binary search if sorted Search an array of records on a chosen field
Insert / delete Costly – later elements must be shifted along Fields are fixed at design time

Most real data is an array of records – a list of things, each with several attributes:

# Array of records: each element is one student
students[0].name = "Ali"      students[0].mark = 72
students[1].name = "Bethan"   students[1].mark = 65
students[2].name = "Cai"      students[2].mark = 81

# Traverse the array and search on a field
best = 0
FOR i = 1 TO 2
    IF students[i].mark > students[best].mark THEN
        best = i
    END IF
NEXT i
OUTPUT students[best].name, "scored the highest mark"

Exam tip – when asked to justify a data structure, say what the manipulation costs. An array gives direct access by index in one step, which is why searching and traversing are cheap, but inserting in the middle means shifting everything after it. A record is the right choice when the attributes have different data types, which an array cannot hold.

Choosing Between an Array and a Record

Data Structure Type Access Insertion Deletion Key Feature
Array Static, linear Direct, by index Costly – shift elements Costly – shift elements Fixed size, all elements the same type
Array of records Static By index, then field name Costly – shift elements Costly – shift elements A list of things, each with several attributes
Record Static By field name Fields fixed at design time Fields fixed at design time Groups fields of different data types