Matrix Multiplication in C++
Multiply a 2×3 matrix by a 3×2 matrix in C++ with three nested loops. Traces how 58 is computed and explains the rule that inner dimensions must match.
1#include <iostream>2using namespace std;34int main() {5 int rows1 = 2, cols1 = 3;6 int rows2 = 3, cols2 = 2;78 int matrix1[2][3] = {{1, 2, 3}, {4, 5, 6}};9 int matrix2[3][2] = {{7, 8}, {9, 10}, {11, 12}};10 int result[2][2] = {{0, 0}, {0, 0}};1112 // Matrix multiplication13 for (int i = 0; i < rows1; i++) {14 for (int j = 0; j < cols2; j++) {15 for (int k = 0; k < cols1; k++) {16 result[i][j] += matrix1[i][k] * matrix2[k][j];17 }18 }19 }2021 cout << "Result matrix:" << endl;22 for (int i = 0; i < rows1; i++) {23 for (int j = 0; j < cols2; j++) {24 cout << result[i][j] << " ";25 }26 cout << endl;27 }2829 return 0;30}
Sample Output
58 64
Row 0 of the result.
139 154
Row 1 of the result.
In this one
- The rule cols of A = rows of B
- How each result cell is a row-times-column sum
- Why result must start at zero
- The meaning of each of the three loops
How it works
- 1
matrix1 is 2×3, matrix2 is 3×2, result is 2×2 of zeros.
- 2
i picks a row of matrix1, j a column of matrix2.
- 3
k walks along: result[i][j] += matrix1[i][k] × matrix2[k][j].
- 4
result[0][0] = 1×7 + 2×9 + 3×11 = 58.
- 5
Print the 2×2 result.
result[1][1]
Row 1 of A times column 1 of B.
- 1.k = 0: 4 × 8 = 32
- 2.k = 1: 5 × 10 = 50 → 82
- 3.k = 2: 6 × 12 = 72 → 154
Edge cases
Result not zeroed
Declare int result[2][2]; without initialization.
+= adds onto garbage values, so the result is wrong.
Incompatible sizes
Try multiplying 2×3 by 2×3.
The inner dimensions 3 and 2 do not match, so multiplication is undefined. The loop would read past matrix2.
What's going on
The program multiplies two matrices and displays the result. For example:
1 2 3
4 5 6
7 8
9 10
11 12
58 64
139 154
Matrix multiplication is not commutative (A×B ≠ B×A in general).
Header File Used
#include <iostream>
This header provides:
Understanding Matrix Multiplication Rules
Dimension Requirement:
For matrices A (m×n) and B (p×q) to be multipliable:
Result Dimensions:
Multiplication Process:
Declaring Variables
The program declares:
int rows1 = 2, cols1 = 3;
int rows2 = 3, cols2 = 2;
int matrix1[2][3] = {{1, 2, 3}, {4, 5, 6}};
int matrix2[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int result[2][2] = {{0, 0}, {0, 0}};
The Matrix Multiplication Algorithm
The core algorithm uses three nested loops:
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
How it works:
Step-by-step calculation (for result[0][0]):
k = 0: result[0][0] += matrix1[0][0] * matrix2[0][0] = 1 * 7 = 7
k = 1: result[0][0] += matrix1[0][1] * matrix2[1][0] = 2 * 9 = 18
k = 2: result[0][0] += matrix1[0][2] * matrix2[2][0] = 3 * 11 = 33
Final: result[0][0] = 7 + 18 + 33 = 58
Visual Representation:
result[0][0] = (1*7) + (2*9) + (3*11) = 7 + 18 + 33 = 58
result[0][1] = (1*8) + (2*10) + (3*12) = 8 + 20 + 36 = 64
result[1][0] = (4*7) + (5*9) + (6*11) = 28 + 45 + 66 = 139
result[1][1] = (4*8) + (5*10) + (6*12) = 32 + 50 + 72 = 154
Understanding the Dot Product
Dot Product Concept:
Each element in the result is the dot product of:
Example (result[0][0]):
Displaying the Result
The program displays the result matrix:
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
Output:
58 64
139 154
Important Matrix Multiplication Properties
Not Commutative:
Associative:
Distributive:
Identity Matrix:
When to Use Matrix Multiplication
Linear Algebra:
Computer Graphics:
Machine Learning:
Scientific Computing:
Important Considerations
Dimension Compatibility:
Initialization:
Time Complexity:
Memory Requirements:
Common Mistakes
Wrong Loop Order:
Not Initializing Result:
Dimension Mismatch:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
A is 3×4 and B is 4×5. Result shape?
Pick an answer to check it.
Question 2
Which loop variable runs over the shared dimension?
Pick an answer to check it.
Questions people ask
Is A × B the same as B × A?
Usually not. Here B × A would be 3×3, a different size entirely.
What is the time cost?
rows1 × cols2 × cols1 multiplications: O(n³) for square matrices.
Why three loops?
Two pick the result cell, the third sums along the shared dimension.