Many kinds of data, such as tables, images and matrices, have rows and columns. Two-dimensional arrays store them.

Two-Dimensional Arrays

A two-dimensional array A[m][n] has m rows and n columns, and an element is written A[i][j]. In C it is declared as int a[3][4]. It is used for matrices, tables of marks, game boards and images. Arrays with three or more indices, such as a[2][3][4], are multi-dimensional arrays, used for cubes of data and colour images.

Representation in Memory

Memory is a single line, so a 2D array must be laid out in a sequence. In row-major order, used by C, the rows are stored one after another. In column-major order, used by FORTRAN and MATLAB, the columns are stored one after another. With base address B, element size w, n columns and zero-based indices, the address in row-major order is B + w × (i × n + j). With m rows in column-major order, it is B + w × (j × m + i). For int a[3][4] at base 2000 with w = 4, a[2][1] in row-major order is at 2000 + 4 × (2 × 4 + 1) = 2036.

Definition: Row-major order. A way of storing a two-dimensional array in memory in which all the elements of the first row are stored, followed by all those of the second row, and so on.

Matrix Operations

Addition and subtraction of two m × n matrices add or subtract corresponding elements, taking O(mn) time. Multiplication of an m × n matrix by an n × p matrix gives an m × p matrix, where C[i][j] = Σ A[i][k] × B[k][j]. It needs three nested loops and takes O(mnp) time, which is O(n³) for square matrices. The transpose swaps rows and columns.

Sparse Matrices

A sparse matrix is one in which most elements are zero. Storing all the zeros wastes memory. Instead, we store only the non-zero elements, as a list of triples (row, column, value), usually with a header row giving the number of rows, columns and non-zero values. A 1000 × 1000 matrix with 500 non-zero values needs a million cells as an array, but only about 1500 numbers as triples. Special forms are diagonal, tridiagonal, and lower and upper triangular matrices. Sparse matrices are common in graphs, scientific computing and machine learning.

Table 1.6: Storage of a 2D array

Order Stores Address of A[i][j]
Row-major Row after row B + w(i × n + j)
Column-major Column after column B + w(j × m + i)
Sparse triples Only non-zero values Row, column, value list

Example 1.6: Sparse matrix as triples

Problem: Write the triple form of the 4 × 5 matrix whose only non-zero entries are A[0][2] = 3, A[1][4] = 7, A[3][1] = 5.

Solution:

  1. Header: 4 rows, 5 columns, 3 non-zero values.
  2. Triples: (0, 2, 3), (1, 4, 7), (3, 1, 5).

Result: 4 rows of 3 numbers replace 20 cells.

#include <stdio.h>
int main(void) {
    int a[4][5] = {{0,0,3,0,0},{0,0,0,0,7},{0,0,0,0,0},{0,5,0,0,0}};
    int count = 0;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 5; j++)
            if (a[i][j]) count++;
    printf("%d %d %d\n", 4, 5, count);
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 5; j++)
            if (a[i][j]) printf("%d %d %d\n", i, j, a[i][j]);
    return 0;
}

Output

4 5 3
0 2 3
1 4 7
3 1 5

Common mistakes

  • Using the row-major formula for a column-major language, or the reverse.
  • Multiplying matrices whose inner dimensions do not match.
  • Storing a large sparse matrix as a full array.

Key takeaways

  • A 2D array A[m][n] has m rows and n columns.
  • C uses row-major order: address = B + w(i × n + j).
  • Matrix multiplication takes O(n³) for n × n matrices.
  • A sparse matrix is stored as (row, column, value) triples.

Practice questions

  1. What is row-major order?
  2. What is a sparse matrix?
  3. What condition is needed to multiply matrices A (m × n) and B?

Answers to the practice questions

  1. Storing a 2D array row after row in memory.
  2. A matrix in which most of the elements are zero.
  3. B must have n rows; the result is m × p if B is n × p.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit I