Lesson 5 +30 XP

Multidimensional Arrays & Address Calculation

Multidimensional Arrays & Address Calculation

Tables, matrices and images have rows and columns, so they are stored in multidimensional arrays. Memory, however, is a single line of bytes. This lesson explains how a 2D or 3D array is laid out in that line (row-major and column-major order) and how to calculate the address of any element - a favourite numerical question in IPU papers.

1. Two-Dimensional Arrays

A two-dimensional array is an array of arrays: a collection of m × n same-type elements arranged in m rows and n columns. Element A[i][j] is in row i and column j.

C
int A[3][4];      /* 3 rows, 4 columns = 12 integers */
DIAGRAM
              col 0   col 1   col 2   col 3
            +-------+-------+-------+-------+
   row 0    | a00   | a01   | a02   | a03   |
            +-------+-------+-------+-------+
   row 1    | a10   | a11   | a12   | a13   |
            +-------+-------+-------+-------+
   row 2    | a20   | a21   | a22   | a23   |
            +-------+-------+-------+-------+

Number of elements of A[LB₁ : UB₁, LB₂ : UB₂] is L1 × L2, where L1 = UB1 - LB1 + 1 (rows) and L2 = UB2 - LB2 + 1 (columns).

Example: A(−2:2, 2:22) has (2 − (−2) + 1) × (22 − 2 + 1) = 5 × 21 = 105 elements.

2. Row-Major and Column-Major Order

The 2D table must be flattened into one line of memory. There are two standard ways.

Row-major order

Store row 0 completely, then row 1, then row 2, … Used by C, C++, Java, Python (NumPy default).

Column-major order

Store column 0 completely, then column 1, … Used by Fortran, MATLAB, R.

DIAGRAM
 Row-major:     a00 a01 a02 a03 | a10 a11 a12 a13 | a20 a21 a22 a23
                <---- row 0 ---> <---- row 1 ---> <---- row 2 --->

 Column-major:  a00 a10 a20 | a01 a11 a21 | a02 a12 a22 | a03 a13 a23
                <- col 0 ->  <- col 1 ->  <- col 2 ->  <- col 3 ->

3. Address Formulas for a 2D Array

Let the array be A[LB₁ : UB₁, LB₂ : UB₂] with base address Base(A) and element size w. Define

  • L1 = UB1 - LB1 + 1 (number of rows), L2 = UB2 - LB2 + 1 (number of columns)
  • E1 = i - LB1 and E2 = j - LB2 (the effective indices - how far A[i][j] is from the first row and first column)

Row-major:

LOC(A[i][j]) = Base(A) + w × (E1 × L2 + E2)

Derivation: to reach A[i][j] we skip E1 complete rows, each holding L2 elements, and then E2 more elements in row i. So E1 L2 + E2 elements come before it; multiply by w for bytes.

Column-major:

LOC(A[i][j]) = Base(A) + w × (E2 × L1 + E1)

Derivation: skip E2 complete columns, each holding L1 elements, then E1 elements down column j.

For a C array int A[m][n] (LB = 0), row-major simplifies to Base + w × (i × n + j).

Worked example (2D)

A(1:10, 1:5), Base = 2000, w = 2. Find LOC(A[4][3]).

StepValue
L1 (rows)10 − 1 + 1 = 10
L2 (columns)5 − 1 + 1 = 5
E14 − 1 = 3
E23 − 1 = 2
Row-major2000 + 2 × (3 × 5 + 2) = 2000 + 34 = 2034
Column-major2000 + 2 × (2 × 10 + 3) = 2000 + 46 = 2046

Check by counting: in row-major order, rows 1, 2, 3 (15 elements) come first, then A[4][1] and A[4][2] - 17 elements, 17 × 2 = 34 bytes. ✓

4. Three-Dimensional and N-Dimensional Arrays

A 3D array B[LB₁ : UB₁, LB₂ : UB₂, LB₃ : UB₃] can be pictured as L1 &quot;pages&quot;, each a 2D table of L2 rows and L3 columns.

Row-major (last index changes fastest):

offset = (E1 × L2 + E2) × L3 + E3

Column-major (first index changes fastest):

offset = (E3 × L2 + E2) × L1 + E1

and LOC = Base + w × offset in both cases.

General N dimensions (Lk = UBk - LBk + 1, Ek = Kk - LBk):

  • Row-major: start with E1; for k = 2 … N repeat &quot;multiply by Lk, add Ek&quot;.
  • Column-major: start with EN; for k = N − 1 down to 1 repeat &quot;multiply by Lk, add Ek&quot;.
PSEUDO
Algorithm ROW_MAJOR_OFFSET(E, L, N)
// E[1..N] effective indices, L[1..N] lengths of the dimensions
OFFSET ← E[1]
for K ← 2 to N do
    OFFSET ← OFFSET × L[K] + E[K]
return OFFSET              // LOC = Base + w × OFFSET

Worked example (3D) - appears often in CIC-209 papers

B(1:8, −5:5, −10:5), Base(B) = 400, w = 4. Find LOC(B[3, 3, 3]).

Step 1 - lengths and effective indices (always write these first):

DimensionLkEk
18 − 1 + 1 = 83 − 1 = 2
25 − (−5) + 1 = 113 − (−5) = 8
35 − (−10) + 1 = 163 − (−10) = 13

Total elements = 8 × 11 × 16 = 1408.

Step 2 - row-major:

  • offset = (2 × 11 + 8) × 16 + 13 = 30 × 16 + 13 = 493
  • LOC = 400 + 4 × 493 = 2372

Step 3 - column-major:

  • offset = (13 × 11 + 8) × 8 + 2 = 151 × 8 + 2 = 1210
  • LOC = 400 + 4 × 1210 = 5240

Variant asked in the notes: the same array with Base = 300 gives 300 + 1972 = 2272 (row-major) and 300 + 4840 = 5140 (column-major) - only the base changes.

5. Operations on 2D Arrays (Matrices)

Matrix addition

PSEUDO
Algorithm MATRIX_ADD(A, B, C, M, N)
for I ← 0 to M - 1 do
    for J ← 0 to N - 1 do
        C[I][J] ← A[I][J] + B[I][J]

Every one of the m × n cells is computed once: O(mn).

Transpose

PSEUDO
Algorithm TRANSPOSE(A, T, M, N)
// T becomes the N × M transpose of the M × N matrix A
for I ← 0 to M - 1 do
    for J ← 0 to N - 1 do
        T[J][I] ← A[I][J]

O(mn) time.

Matrix multiplication

For A (m × p) and B (p × n), each C[i][j] is the dot product of row i of A and column j of B.

Algorithm MATRIX_MULTIPLY(A, B, C, M, P, N)
for I ← 0 to M - 1 do
    for J ← 0 to N - 1 do
        C[I][J] ← 0
        for K ← 0 to P - 1 do
            C[I][J] ← C[I][J] + A[I][K] × B[K][J]

Three nested loops: O(mnp), i.e. O(n3) for square matrices. Multiplication is possible only when the number of columns of A equals the number of rows of B.

6. 2D Arrays and Pointers in C

int M[3][4] is an array of 3 rows, each an array of 4 ints.

  • M[i] is the address of row i; M[i][j] is the same as *(*(M + i) + j).
  • When passing a 2D array to a function, all dimensions except the first must be given: void print(int m[][4], int rows);

Why row-major matters for speed: looping over j in the inner loop visits neighbouring memory cells, which the CPU cache loads together. Looping over i in the inner loop jumps a whole row each time and runs noticeably slower on large matrices.

💡 Tip

Key points to remember - Always compute Lk (lengths) and Ek (effective indices) first. - Row-major: offset = E1 L2 + E2 (2D); the last index varies fastest. C uses row-major. - Column-major: offset = E2 L1 + E1 (2D); the first index varies fastest. Fortran uses column-major. - LOC = Base + w × offset. - B(1:8, −5:5, −10:5), Base 400, w 4: B[3,3,3] is at 2372 (row-major) and 5240 (column-major).

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Derive the address calculation formulas for row-major and column-major order of a 2D array A[LB₁:UB₁, LB₂:UB₂]. (5 Marks)
  • Write: define L1, L2, E1, E2; the two memory diagrams; both formulas with the &quot;skip complete rows/columns&quot; reasoning; one numeric example.
  • Q2. Given B(1:8, −5:5, −10:5), Base = 400, w = 4, calculate LOC(B[3,3,3]) in row-major and column-major order. (7.5 Marks)
  • Write: the table of Lk and Ek, both offset calculations, and box the answers 2372 and 5240.
  • Q3. Write an algorithm to multiply two matrices and state its complexity. (5 Marks)