Using arrays

Using Arrays

An array is a data structure that stores a fixed number of values of the same data type under a single name. Each value sits at a numbered position called an index, which starts at 0 in Python, C#, and VB.NET. Arrays are ideal when you know in advance how many values you need and all values share the same type.

AQA requires you to use both one-dimensional (1D) arrays and two-dimensional (2D) arrays. Python does not have a built-in array type at GCSE level - a list is used as the equivalent and behaves identically for indexing and iteration.

1D and 2D Arrays in Practice

1D Arrays: index and element

A 1D array is a single row of values. Each value is called an element; its position is its index. The table below shows an array of five scores - notice that index 0 holds the first value and index 4 holds the last.

Index 01234
Element (value) 7285916877

AQA pseudocode accesses elements as scores[0], scores[1] ... scores[4]. To process all five you loop from index 0 to 4 (i.e. 0 to length - 1).

The array is declared and initialised with five values. A FOR loop walks from index 0 to 4, accumulating a running total.

AQA pseudocode:

scores ← [72, 85, 91, 68, 77]
total ← 0
FOR i ← 0 TO 4
    total ← total + scores[i]
ENDFOR
OUTPUT "Total: " + total
# Python -- list used as 1D array equivalent
scores = [72, 85, 91, 68, 77]   # 5 elements, indices 0..4

total = 0
for i in range(len(scores)):    # i: 0, 1, 2, 3, 4
    total += scores[i]

print("Total:", total)           # 393
print("Element at index 0:", scores[0])  # 72
print("Element at index 4:", scores[4])  # 77
' VB.NET -- 1D array of scores
' Dim scores(4) would declare 5 elements (indices 0..4)
' Here we initialise directly with values
Dim scores() As Integer = {72, 85, 91, 68, 77}

Dim total As Integer = 0
For i As Integer = 0 To scores.Length - 1  ' 0 to 4
    total += scores(i)
Next

Console.WriteLine("Total: " & total)                     ' 393
Console.WriteLine("Element at index 0: " & scores(0))   ' 72
Console.WriteLine("Element at index 4: " & scores(4))   ' 77
// C# -- 1D array of scores
int[] scores = { 72, 85, 91, 68, 77 };  // 5 elements, indices 0..4

int total = 0;
for (int i = 0; i < scores.Length; i++) // i: 0, 1, 2, 3, 4
    total += scores[i];

Console.WriteLine("Total: "            + total);     // 393
Console.WriteLine("Element at index 0: " + scores[0]); // 72
Console.WriteLine("Element at index 4: " + scores[4]); // 77

Here the array is filled by reading values from the user rather than being hard-coded. A second loop then finds the minimum value. This pattern - fill first, then process - is common in exam questions.

AQA pseudocode:

FOR i ← 0 TO 2
    OUTPUT "Enter temperature " + i
    INPUT temps[i]
ENDFOR

lowest ← temps[0]
FOR i ← 1 TO 2
    IF temps[i] < lowest THEN
        lowest ← temps[i]
    ENDIF
ENDFOR
OUTPUT "Lowest: " + lowest
# Python -- fill array from user input, then find minimum
SIZE = 3
temps = [0] * SIZE          # create list of 3 zeros first

for i in range(SIZE):       # fill: indices 0, 1, 2
    temps[i] = float(input("Enter temperature " + str(i) + ": "))

lowest = temps[0]           # assume first is lowest
for i in range(1, SIZE):    # check remaining elements
    if temps[i] < lowest:
        lowest = temps[i]

print("Lowest temperature:", lowest)
' VB.NET -- fill array from user input, then find minimum
Const SIZE As Integer = 3
Dim temps(SIZE - 1) As Double   ' indices 0..2

For i As Integer = 0 To SIZE - 1   ' fill
    Console.Write("Enter temperature " & i & ": ")
    temps(i) = CDbl(Console.ReadLine())
Next

Dim lowest As Double = temps(0)     ' assume first is lowest
For i As Integer = 1 To SIZE - 1   ' check remaining
    If temps(i) < lowest Then lowest = temps(i)
Next

Console.WriteLine("Lowest temperature: " & lowest)
// C# -- fill array from user input, then find minimum
const int SIZE = 3;
double[] temps = new double[SIZE];  // indices 0..2

for (int i = 0; i < SIZE; i++)      // fill
{
    Console.Write("Enter temperature " + i + ": ");
    temps[i] = double.Parse(Console.ReadLine());
}

double lowest = temps[0];           // assume first is lowest
for (int i = 1; i < SIZE; i++)      // check remaining
    if (temps[i] < lowest) lowest = temps[i];

Console.WriteLine("Lowest temperature: " + lowest);

2D Arrays: rows and columns

A 2D array is a grid of values with two indices: row first, then column. You access an element with grid[row][col] in Python and C#, or grid(row, col) in VB.NET. Nested loops - one for rows, one for columns - are used to visit every cell.

A 3x3 board is a natural fit for a 2D array. Each cell stores a string. Nested loops print the full grid row by row.

AQA pseudocode:

board[0][0] ← "X"  board[0][1] ← "O"  board[0][2] ← "X"
board[1][0] ← "O"  board[1][1] ← "X"  board[1][2] ← "O"
board[2][0] ← "X"  board[2][1] ← " "  board[2][2] ← "O"

FOR row ← 0 TO 2
    FOR col ← 0 TO 2
        OUTPUT board[row][col]
    ENDFOR
ENDFOR
# Python -- 2D list (list of lists) as a 3x3 board
board = [
    ["X", "O", "X"],   # row 0
    ["O", "X", "O"],   # row 1
    ["X", " ", "O"]    # row 2
]

for row in range(3):
    for col in range(3):
        print(board[row][col], end=" ")
    print()                          # newline after each row

print("Centre cell:", board[1][1])   # X
' VB.NET -- 2D array as a 3x3 board
' board(2,2) means rows 0..2, cols 0..2 (3x3 grid)
Dim board(,) As String = {
    {"X", "O", "X"},
    {"O", "X", "O"},
    {"X", " ", "O"}
}

For row As Integer = 0 To 2
    For col As Integer = 0 To 2
        Console.Write(board(row, col) & " ")
    Next
    Console.WriteLine()              ' newline after each row
Next

Console.WriteLine("Centre cell: " & board(1, 1))  ' X
// C# -- 2D array as a 3x3 board
string[,] board = {
    {"X", "O", "X"},   // row 0
    {"O", "X", "O"},   // row 1
    {"X", " ", "O"}    // row 2
};

for (int row = 0; row < 3; row++)
{
    for (int col = 0; col < 3; col++)
        Console.Write(board[row, col] + " ");
    Console.WriteLine();             // newline after each row
}

Console.WriteLine("Centre cell: " + board[1, 1]); // X

A 2D array can represent a weekly timetable: rows are days (Mon-Fri), columns are periods (1-3). Each cell holds the subject name, or "Free" if the slot is empty. The program counts how many free periods there are in the week.

AQA pseudocode:

freeCount ← 0
FOR day ← 0 TO 4
    FOR period ← 0 TO 2
        IF timetable[day][period] = "Free" THEN
            freeCount ← freeCount + 1
        ENDIF
    ENDFOR
ENDFOR
OUTPUT "Free periods: " + freeCount
# Python -- 5 days x 3 periods timetable
# Rows = days (0=Mon .. 4=Fri), Cols = periods (0, 1, 2)
timetable = [
    ["Maths",    "English", "Free"   ],  # Monday
    ["Science",  "Free",    "History"],  # Tuesday
    ["English",  "Maths",   "PE"     ],  # Wednesday
    ["Free",     "Science", "Free"   ],  # Thursday
    ["History",  "PE",      "Maths"  ]   # Friday
]

free_count = 0
for day in range(5):
    for period in range(3):
        if timetable[day][period] == "Free":
            free_count += 1

print("Free periods this week:", free_count)  # 4
' VB.NET -- 5 days x 3 periods timetable
' Rows = days (0=Mon..4=Fri), Cols = periods (0,1,2)
Dim timetable(,) As String = {
    {"Maths",   "English", "Free"   },
    {"Science", "Free",    "History"},
    {"English", "Maths",   "PE"     },
    {"Free",    "Science", "Free"   },
    {"History", "PE",      "Maths"  }
}

Dim freeCount As Integer = 0
For day As Integer = 0 To 4
    For period As Integer = 0 To 2
        If timetable(day, period) = "Free" Then freeCount += 1
    Next
Next

Console.WriteLine("Free periods this week: " & freeCount)  ' 4
// C# -- 5 days x 3 periods timetable
// Rows = days (0=Mon..4=Fri), Cols = periods (0, 1, 2)
string[,] timetable = {
    {"Maths",    "English", "Free"   },
    {"Science",  "Free",    "History"},
    {"English",  "Maths",   "PE"     },
    {"Free",     "Science", "Free"   },
    {"History",  "PE",      "Maths"  }
};

int freeCount = 0;
for (int day = 0; day < 5; day++)
    for (int period = 0; period < 3; period++)
        if (timetable[day, period] == "Free") freeCount++;

Console.WriteLine("Free periods this week: " + freeCount); // 4

 Key Takeaways

  • An array stores a fixed number of same-type values under one name. Each value is an element stored at a numbered index, starting at 0.
  • A 1D array is a single row. Loop from index 0 to length - 1 to process every element.
  • A 2D array is a grid accessed by two indices - row first, then column. Nested loops visit every cell.
  • In VB.NET, Dim scores(4) creates 5 elements (indices 0 to 4) - the number is the highest index, not the count.
  • Python uses a list as its array equivalent. VB.NET uses round brackets for indices, e.g. scores(2); Python and C# use square brackets, e.g. scores[2].