Bytebook Compiler

Arrays & Strings

2D Arrays & Matrices

Rows and columns: declaring 2D arrays, reading matrices, and the addition, transpose, multiplication and diagonal-sum programs.

4 min read5 runnable examples4-question quiz

A 2D array is an array of arrays — a grid with rows and columns. It is how C represents matrices, game boards, seating charts and images.

C
#include <stdio.h>

int main(void) {
    int m[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    printf("Row 1, column 2: %d\n", m[1][2]);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++)
            printf("%4d", m[i][j]);
        printf("\n");
    }
    return 0;
}
Output
Row 1, column 2: 7
   1   2   3   4
   5   6   7   8
   9  10  11  12
m0123012341567829101112
m[1][2] is row 1, column 2 — the value 7. The first index is the row, the second is the column.

How it sits in memory

Memory is one long line, so C stores 2D arrays in row-major order: all of row 0, then all of row 1, and so on. Element m[i][j] lives at position i * COLS + j from the start. That is why, when you pass a 2D array to a function, you must give the column count: void print(int m[][4], int rows).

Matrix addition

C
#include <stdio.h>
#define R 2
#define C 3

int main(void) {
    int a[R][C] = {{1, 2, 3}, {4, 5, 6}};
    int b[R][C] = {{6, 5, 4}, {3, 2, 1}};
    int sum[R][C];
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            sum[i][j] = a[i][j] + b[i][j];
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) printf("%3d", sum[i][j]);
        printf("\n");
    }
    return 0;
}
Output
  7  7  7
  7  7  7

Transpose

The transpose swaps rows and columns: t[j][i] = a[i][j]. A 2×3 matrix becomes 3×2.

C
#include <stdio.h>

int main(void) {
    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int t[3][2];
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 3; j++)
            t[j][i] = a[i][j];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) printf("%3d", t[i][j]);
        printf("\n");
    }
    return 0;
}
Output
  1  4
  2  5
  3  6

Matrix multiplication

To multiply an r1 × c1 matrix by a c1 × c2 matrix, each result cell is a row × column dot product: c[i][j] = Σ a[i][k] × b[k][j]. Multiplication is only possible when the first matrix's columns equal the second's rows.

C
#include <stdio.h>

int main(void) {
    int r1, c1, r2, c2, a[10][10], b[10][10], c[10][10];
    printf("Rows and columns of A: ");
    scanf("%d %d", &r1, &c1);
    printf("Enter A:\n");
    for (int i = 0; i < r1; i++) for (int j = 0; j < c1; j++) scanf("%d", &a[i][j]);
    printf("Rows and columns of B: ");
    scanf("%d %d", &r2, &c2);
    if (c1 != r2) { printf("Cannot multiply: %d != %d\n", c1, r2); return 0; }
    printf("Enter B:\n");
    for (int i = 0; i < r2; i++) for (int j = 0; j < c2; j++) scanf("%d", &b[i][j]);

    for (int i = 0; i < r1; i++)
        for (int j = 0; j < c2; j++) {
            c[i][j] = 0;
            for (int k = 0; k < c1; k++)
                c[i][j] += a[i][k] * b[k][j];
        }

    printf("A x B =\n");
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) printf("%5d", c[i][j]);
        printf("\n");
    }
    return 0;
}
Output
Rows and columns of A: 2 3
Enter A:
1 2 3
4 5 6
Rows and columns of B: 3 2
Enter B:
7 8
9 10
11 12
A x B =
   58   64
  139  154
Sample input is includedTry it Yourself

The three nested loops make this O(n³) for square matrices.

Diagonal sums

C
#include <stdio.h>

int main(void) {
    int m[3][3] = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}};
    int main_d = 0, anti_d = 0;
    for (int i = 0; i < 3; i++) {
        main_d += m[i][i];
        anti_d += m[i][3 - 1 - i];
    }
    printf("Main diagonal = %d, anti-diagonal = %d\n", main_d, anti_d);
    return 0;
}
Output
Main diagonal = 15, anti-diagonal = 15

(That matrix is a magic square — every row, column and diagonal adds up to 15.)

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    How many elements does int g[4][6]; hold?

  2. 2

    In row-major order, which element comes right after m[0][2] in memory for int m[3][3]?

  3. 3

    Can a 2×3 matrix be multiplied by a 2×3 matrix?

  4. 4

    When passing int m[5][8] to a function, what must the parameter specify?