[8.2.1-3] Arrays

[8.2.1–3] Arrays: 1D and 2D, Indexes, and Iteration

What is an array and why use one?

An array is a collection of items of the same data type stored under one name and accessed by a numeric index. Arrays help you organise related data so you can process it efficiently, for example storing the daily temperatures for a month, or a mark for each student in a class. In IGCSE programming you will use one-dimensional (1D) arrays for lists and two-dimensional (2D) arrays for tables or grids. You must be confident to declare arrays, read and write elements using variables as indexes, and to use iteration (loops) to fill, search, or print arrays.

The syllabus allows the first index to be either zero or one. You should recognise and work with both. Many real languages (e.g. Python) start at 0; CAIE pseudocode may show either 0-based or 1-based indexing. Be clear which convention you are using, and test boundary values carefully to avoid off-by-one mistakes.

Key terminology

Term Meaning (IGCSE context) Example
1D array A list of values of the same type addressed by one index. marks[3] refers to the 4th or 3rd item depending on start index.
2D array A table addressed by row and column indexes. grid[row][col] or grid[row, col] depending on notation.
Index Position number used to access elements. May start at 0 or 1. For 10 items: valid indexes may be 0..9 or 1..10.
Element One value stored within the array. names[i] is one element from names.
Traverse Visit elements in order using a loop. FOR each index, read/write or process.

Declaring and initialising arrays

To declare an array, state its name, length, and the data type of its elements. To initialise, you either assign values when declaring or fill it later with a loop. In CAIE pseudocode you may see array literals such as myList ← [10, 20, 30] or you may be told the size and asked to fill values using INPUT or computed results.

Indexing conventions: compare and contrast

1-based arrays number elements starting from 1. With N elements you loop from 1 to N inclusive. This often matches human counting and is common in exam-style tables.

Loop sketch: FOR i ← 1 TO N … NEXT i

0-based arrays number elements from 0. With N elements you loop 0 to N−1. This is the default in many languages like Python.

Loop sketch: FOR i ← 0 TO N−1 … NEXT i

Off-by-one mistakes happen when the loop runs one time too many or too few. Always confirm both the first and last index, and whether the loop upper bound is inclusive.

Reading and writing values with iteration (1D)

You can use a loop to write values into an array (e.g. from user input) or to read values out for processing (e.g. to find a maximum). Using a variable as an index is essential: the loop counter selects the current element.

Goal: Read N marks into marks. Use the loop counter as the index and store each value.

Goal: Traverse the array to sum values (accumulator pattern) and then compute the average as total ÷ N.

Edge risk: If N = 0, avoid dividing by zero. With a single element, ensure loops still run exactly once.

Two-dimensional arrays (tables)

A 2D array stores data in rows and columns. You use nested iteration: an outer loop for rows, an inner loop for columns. This structure is essential for printing grids, counting values that match a condition, or computing row/column totals.

Goal: Use nested loops to input values into table[row][col]. Reset the column counter for each new row.

Goal: Traverse all cells and output each row on a new line. This reinforces how the inner loop completes one full pass per row.

Diagonal highlight: When row = col (main diagonal), print a marker. Ensure row/col ranges match so the condition makes sense in non-square tables.

Common array algorithms

Task Key idea Pseudocode sketch
Find maximum Track a running best value; update when a larger element is found. best ← first element; FOR each element IF element > best THEN best ← element
Linear search Check each element until found or end reached. found ← FALSE; FOR i IN indexes IF a[i] = target THEN found ← TRUE
Row totals (2D) Inner loop adds across columns; outer loop moves to next row. FOR row … sum ← 0; FOR col … sum ← sum + table[row][col]

Deep Dive: Choosing array sizes and bounds

Decide whether your array's size is fixed (known at design time) or input-driven (depends on a value N read at runtime). For fixed tasks, declare an exact size and use all positions. For input-driven tasks, read N, allocate or plan to use N positions, and ensure your loops match the chosen indexing convention. Always test first and last positions.

Deep Dive: Preventing off-by-one errors

Write loop headers carefully. With 1-based arrays of size N, iterate FOR i ← 1 TO N. With 0-based arrays of size N, iterate FOR i ← 0 TO N−1. When moving between conventions, adjust both your initial value and your final value together, not just one of them.

Copy-ready worked examples

// CAIE-style pseudocode: Input N marks (1-based) and compute average
DECLARE N : INTEGER
DECLARE i : INTEGER
DECLARE marks : ARRAY OF INTEGER
DECLARE total : INTEGER
DECLARE avg : REAL

INPUT N
// assume marks has N positions, indexed 1..N
total ← 0
FOR i ← 1 TO N
    INPUT marks[i]
    total ← total + marks[i]
NEXT i
IF N > 0 THEN
    avg ← total / N
    OUTPUT "Average = " & avg
ELSE
    OUTPUT "No data"
ENDIF
# Python equivalents (0-based lists)
# Input N marks and compute average
N = int(input())
marks = []
total = 0
for _ in range(N):
    m = int(input())
    marks.append(m)
    total += m
if N > 0:
    avg = total / N
    print("Average =", avg)
else:
    print("No data")

# 2D traversal: print 3x4 grid with diagonal "+"
rows, cols = 3, 4
grid = [[0 for _ in range(cols)] for _ in range(rows)]
for r in range(rows):
    for c in range(cols):
        ch = "+" if r == c else "*"
        print(ch, end="")
    print()

 Key Takeaways

  • Arrays store multiple values of the same type under one name and are accessed by numeric indexes.
  • Know and use both indexing conventions: 1..N and 0..N−1. Align your loop bounds with your chosen start index.
  • Use variables as indexes inside loops to read/write array elements reliably.
  • Traverse 2D arrays with nested loops: outer for rows, inner for columns.
  • Test boundary cases to avoid off-by-one errors, especially the first and last positions.
  • Apply common patterns like sum/average, linear search, and row/column totals confidently.