C Program to Multiply Two Matrices with Output
result[i][j] += matrix1[i][k] * matrix2[k][j] inside three loops iterating over rows and columns.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> int main() { int r1 = 2, c1 = 2, r2 = 2, c2 = 2; int matrix1[2][2] = {{1, 2}, {3, 4}}; int matrix2[2][2] = {{5, 6}, {7, 8}}; int result[2][2] = {0}; for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { for (int k = 0; k < c1; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } printf("Result:\n"); for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { printf("%d ", result[i][j]); } printf("\n"); } return 0; }
Dry Run
Let's trace multiplying matrix1 [[1, 2], [3, 4]] and matrix2 [[5, 6], [7, 8]] through the code.
Initialize result matrix
result = [[0, 0], [0, 0]]
Calculate result[0][0]
result[0][0] = (1*5) + (2*7) = 5 + 14 = 19
Calculate result[0][1]
result[0][1] = (1*6) + (2*8) = 6 + 16 = 22
Calculate result[1][0]
result[1][0] = (3*5) + (4*7) = 15 + 28 = 43
Calculate result[1][1]
result[1][1] = (3*6) + (4*8) = 18 + 32 = 50
| Element | Calculation | Value |
|---|---|---|
| result[0][0] | 1*5 + 2*7 | 19 |
| result[0][1] | 1*6 + 2*8 | 22 |
| result[1][0] | 3*5 + 4*7 | 43 |
| result[1][1] | 3*6 + 4*8 | 50 |
Why This Works
Step 1: Matrix multiplication rule
Each element in the result matrix is the sum of products of elements from a row of the first matrix and a column of the second matrix using result[i][j] += matrix1[i][k] * matrix2[k][j].
Step 2: Use of three loops
The outer two loops select the position in the result matrix, and the inner loop calculates the sum of products for that position.
Step 3: Initialization of result matrix
The result matrix is initialized to zero to accumulate sums correctly without garbage values.
Alternative Approaches
#include <stdio.h> int main() { int r1, c1, r2, c2; printf("Enter rows and columns of first matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and columns of second matrix: "); scanf("%d %d", &r2, &c2); if (c1 != r2) { printf("Multiplication not possible\n"); return 0; } int matrix1[r1][c1], matrix2[r2][c2], result[r1][c2]; for (int i = 0; i < r1; i++) for (int j = 0; j < c1; j++) scanf("%d", &matrix1[i][j]); for (int i = 0; i < r2; i++) for (int j = 0; j < c2; j++) scanf("%d", &matrix2[i][j]); for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { result[i][j] = 0; for (int k = 0; k < c1; k++) result[i][j] += matrix1[i][k] * matrix2[k][j]; } } printf("Result:\n"); for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) printf("%d ", result[i][j]); printf("\n"); } return 0; }
#include <stdio.h> void multiply(int r1, int c1, int r2, int c2, int m1[r1][c1], int m2[r2][c2], int res[r1][c2]) { for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { res[i][j] = 0; for (int k = 0; k < c1; k++) res[i][j] += m1[i][k] * m2[k][j]; } } } int main() { int m1[2][2] = {{1, 2}, {3, 4}}; int m2[2][2] = {{5, 6}, {7, 8}}; int res[2][2]; multiply(2, 2, 2, 2, m1, m2, res); printf("Result:\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) printf("%d ", res[i][j]); printf("\n"); } return 0; }
Complexity: O(r1 * c2 * c1) time, O(r1 * c2) space
Time Complexity
The program uses three nested loops: outer loops over rows of first matrix and columns of second matrix, and inner loop over columns of first matrix, resulting in O(r1 * c2 * c1).
Space Complexity
The result matrix requires space proportional to the product of rows of first and columns of second matrix, O(r1 * c2).
Which Approach is Fastest?
All approaches have similar time complexity; using functions improves readability but not speed. Dynamic input adds flexibility but no speed gain.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Fixed-size arrays | O(r1*c2*c1) | O(r1*c2) | Simple, fixed matrices |
| Dynamic input | O(r1*c2*c1) | O(r1*c2) | User input, flexible sizes |
| Function-based | O(r1*c2*c1) | O(r1*c2) | Code reuse and clarity |