Matrix Operations in C
Matrices are used everywhere: image editing, games, Excel sheets and engineering maths. In C, a matrix is simply a 2D array.
In this lesson you will write the three matrix programs that appear in almost every C lab exam: addition, multiplication and transpose. You will also learn the size rules, so you can say when an operation is possible.
What You Will Learn
- When two matrices can be added or multiplied.
- How to add two matrices.
- How to multiply two matrices (the row × column rule).
- How to find the transpose of a matrix.
- How to check if a matrix is symmetric.
Matrix Operations in C Explained
Exam definition: Matrix addition: C[i][j] = A[i][j] + B[i][j]; both matrices must have the same order. Matrix multiplication: C[i][j] = sum of A[i][k] × B[k][j]; the columns of A must equal the rows of B. Transpose: T[j][i] = A[i][j]; rows become columns.
Memory trick: For multiplication, write the orders side by side: (m × n)(n × p). The inside numbers must match, and the outside numbers give the answer's order: m × p. Think "inner match, outer answer".
| Operation | Condition | Result order | Loops |
|---|---|---|---|
| Addition / subtraction | A and B both m × n | m × n | 2 nested |
| Multiplication | A is m × n, B is n × p | m × p | 3 nested |
| Transpose | Any m × n | n × m | 2 nested |
Example 1: Matrix Addition and Transpose in C
#include <stdio.h>
int main(void)
{
int a[2][3] = { {1, 2, 3}, {4, 5, 6} };
int b[2][3] = { {6, 5, 4}, {3, 2, 1} };
int sum[2][3], t[3][2];
int i, j;
/* addition: same position, add */
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
sum[i][j] = a[i][j] + b[i][j];
/* transpose of a: row i becomes column i */
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
t[j][i] = a[i][j];
printf("A + B:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
printf("%4d", sum[i][j]);
printf("\n");
}
printf("Transpose of A (3 x 2):\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
printf("%4d", t[i][j]);
printf("\n");
}
return 0;
}
Output
A + B: 7 7 7 7 7 7 Transpose of A (3 x 2): 1 4 2 5 3 6
Code Explanation
- Addition: two nested loops visit every position
[i][j]and add the elements at the same position. - Transpose:
t[j][i] = a[i][j]swaps the indexes. The element at row 0, column 2 (value 3) goes to row 2, column 0. - A is 2 × 3, so its transpose
tis declared 3 × 2. Using the wrong size would write outside the array. - When printing
t, the loops run over 3 rows and 2 columns.
Subtraction is the same program with - instead of +.
How Matrix Multiplication Works
Each answer element = one row of A "dot" one column of B: multiply matching pairs and add them.
A (2 x 3) B (3 x 2) C = A x B (2 x 2)
| 1 2 3 | | 7 8 |
| 4 5 6 | | 9 10 |
| 11 12 |
C[0][0] = row 0 of A . col 0 of B = 1*7 + 2*9 + 3*11 = 7 + 18 + 33 = 58
C[0][1] = row 0 of A . col 1 of B = 1*8 + 2*10 + 3*12 = 8 + 20 + 36 = 64
C[1][0] = row 1 of A . col 0 of B = 4*7 + 5*9 + 6*11 = 28 + 45 + 66 = 139
C[1][1] = row 1 of A . col 1 of B = 4*8 + 5*10 + 6*12 = 32 + 50 + 72 = 154
So we need three loops: i for the row of A, j for the column of B, and k to walk along the row and column together.
Example 2: Matrix Multiplication Program in C
#include <stdio.h>
int main(void)
{
int a[2][3] = { {1, 2, 3}, {4, 5, 6} }; /* 2 x 3 */
int b[3][2] = { {7, 8}, {9, 10}, {11, 12} }; /* 3 x 2 */
int c[2][2]; /* 2 x 2 */
int m = 2, n = 3, p = 2;
int i, j, k;
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
{
c[i][j] = 0; /* start each sum at 0 */
for (k = 0; k < n; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("A x B:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
printf("%5d", c[i][j]);
printf("\n");
}
return 0;
}
Output
A x B: 58 64 139 154
c[i][j] = 0;must come before thekloop. Otherwise the sum starts from garbage.a[i][k] * b[k][j]:kmoves across row i of A and down column j of B at the same time.- If the user enters the sizes, check
colsA == rowsBfirst and print "Multiplication not possible" otherwise. - Time complexity is O(m × n × p), or O(n³) for two n × n matrices.
- Matrix multiplication is not commutative: A × B is usually not equal to B × A. Here B × A would even be 3 × 3.
Bonus: Check if a Matrix is Symmetric
A square matrix is symmetric if it equals its transpose, that is a[i][j] == a[j][i] for all i, j.
#include <stdio.h>
int is_symmetric(int a[][3], int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++) /* compare above the diagonal only */
if (a[i][j] != a[j][i])
return 0;
return 1;
}
int main(void)
{
int s[3][3] = { {1, 7, 3}, {7, 4, 5}, {3, 5, 6} };
int u[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
printf("s is %s\n", is_symmetric(s, 3) ? "symmetric" : "not symmetric");
printf("u is %s\n", is_symmetric(u, 3) ? "symmetric" : "not symmetric");
return 0;
}
s is symmetric u is not symmetric
Important Points to Remember
- Addition/subtraction: both matrices must have the same order.
- Multiplication: columns of A = rows of B; result is rows of A × columns of B.
- Multiplication needs three nested loops and the sum must start at 0.
- Transpose:
t[j][i] = a[i][j]; an m × n matrix becomes n × m. - A × B ≠ B × A in general (not commutative).
- Symmetric matrix: A = transpose of A (must be square).
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Not setting c[i][j] = 0 | Garbage results | Initialize before the k loop |
Multiplying element by element: a[i][j] * b[i][j] | Wrong answer (not matrix multiplication) | Use the k loop: a[i][k] * b[k][j] |
| Transpose into the same non-square array | Data overwritten / wrong shape | Use a separate n × m array |
| In-place transpose looping over all j | Every pair swapped twice: no change | Swap only for j > i |
| No size check before multiplying | Meaningless result | Check colsA == rowsB |
Matrix Operations Practice Questions
- Can a 2 × 3 matrix be added to a 3 × 2 matrix? Why?
- A is 3 × 4 and B is 4 × 2. Is A × B possible? What is its order? Is B × A possible?
- How many multiplications are needed to multiply two 3 × 3 matrices?
- What is the transpose of
{{1, 2}, {3, 4}, {5, 6}}? - Why does multiplication need three loops but addition only two?
Show answers
- No. Addition needs both matrices to have the same order.
- Yes, the order is 3 × 2. B × A is not possible (B has 2 columns, A has 3 rows).
- 3 × 3 × 3 = 27.
{{1, 3, 5}, {2, 4, 6}}(2 × 3).- Each element of the product is itself a sum over k (row × column), which needs a third loop. Addition uses just one element from each matrix.
Coding Exercise
Write a program that prints the transpose of the square matrix {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} in place (without a second array). Hint: swap a[i][j] and a[j][i] only when j > i.
Show solution
#include <stdio.h>
int main(void)
{
int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int i, j, temp;
for (i = 0; i < 3; i++)
{
for (j = i + 1; j < 3; j++)
{
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
printf("%d ", a[i][j]);
printf("\n");
}
return 0;
}
1 4 7 2 5 8 3 6 9
Quick Quiz: Matrix Operations in C (MCQ)
-
A (2 × 3) × B (3 × 4) gives a matrix of order:
A) 3 × 3 B) 2 × 4 C) 4 × 2 D) Not possible
-
Matrix addition requires:
A) Square matrices B) Same order C) cols of A = rows of B D) Nothing
-
The correct multiplication statement is:
A)
c[i][j] += a[i][k] * b[k][j];B)c[i][j] = a[i][j] * b[i][j];C)c[i][j] += a[k][i] * b[j][k];D)c[k] += a[i] * b[j]; -
Transpose of a 4 × 2 matrix has order:
A) 4 × 2 B) 2 × 4 C) 4 × 4 D) 2 × 2
-
Time complexity of multiplying two n × n matrices with three loops:
A) O(n) B) O(n²) C) O(n³) D) O(log n)
Show answers
- B: 2 × 4.
- B: same order.
- A.
- B: 2 × 4.
- C: O(n³).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Addition rule | Same order; c[i][j] = a[i][j] + b[i][j] |
|---|---|
| Multiplication rule | cols of A = rows of B |
| Product order | (m × n)(n × p) = m × p |
| Product formula | c[i][j] += a[i][k] * b[k][j] |
| Transpose | t[j][i] = a[i][j]; m × n → n × m |
| Multiplication time | O(n³) |
| A × B = B × A? | Not always |
Frequently asked questions
- When is matrix multiplication possible?
- When the columns of the first matrix equal the rows of the second.
- How do you find the transpose of a matrix in C?
- Copy a[i][j] into t[j][i] with two nested loops (rows become columns).
- What is a symmetric matrix?
- A square matrix equal to its transpose: a[i][j] == a[j][i].
Remember
- Multiplication: inner match, outer answer.
- Sum to 0 first, then the k loop.
Next Topic
Next lesson: Strings in C. You will start Module 09 and learn how C stores text as an array of characters.