C++ Program to Multiply Two Matrices
for loops to calculate each element of the result matrix by summing the products of corresponding elements from the rows of the first matrix and columns of the second matrix, as in result[i][j] += matrix1[i][k] * matrix2[k][j]; inside three nested loops.Examples
How to Think About It
Algorithm
Code
#include <iostream> using namespace std; 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++) { result[i][j] = 0; for (int k = 0; k < c1; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } cout << "Result matrix:\n"; for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { cout << result[i][j] << " "; } cout << endl; } return 0; }
Dry Run
Let's trace multiplying matrix1 [[1, 2], [3, 4]] by 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: Nested loops for rows and columns
The outer loops go through each row i and column j of the result matrix to fill every element.
Step 3: Inner loop for sum of products
The innermost loop k multiplies corresponding elements and adds them to get the final value for result[i][j].
Alternative Approaches
#include <iostream> using namespace std; int main() { int r1, c1, r2, c2; cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1; cout << "Enter rows and columns for second matrix: "; cin >> r2 >> c2; if (c1 != r2) { cout << "Matrix multiplication not possible." << endl; return 0; } int matrix1[r1][c1], matrix2[r2][c2], result[r1][c2]; cout << "Enter first matrix elements:\n"; for (int i = 0; i < r1; i++) for (int j = 0; j < c1; j++) cin >> matrix1[i][j]; cout << "Enter second matrix elements:\n"; for (int i = 0; i < r2; i++) for (int j = 0; j < c2; j++) cin >> 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]; } } } cout << "Result matrix:\n"; for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { cout << result[i][j] << " "; } cout << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int r1 = 2, c1 = 3, r2 = 3, c2 = 2; vector<vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}}; vector<vector<int>> matrix2 = {{7, 8}, {9, 10}, {11, 12}}; vector<vector<int>> result(r1, vector<int>(c2, 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]; } } } cout << "Result matrix:\n"; for (auto &row : result) { for (auto &val : row) { cout << val << " "; } cout << endl; } 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 (or rows of second). This results in O(r1 * c2 * c1) time.
Space Complexity
The result matrix requires space proportional to the number of rows of the first matrix and columns of the second matrix, O(r1 * c2).
Which Approach is Fastest?
All approaches have the same time complexity; using vectors adds flexibility but may have slight overhead compared to raw arrays.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Fixed-size arrays | O(r1*c2*c1) | O(r1*c2) | Simple, small matrices |
| Dynamic arrays with user input | O(r1*c2*c1) | O(r1*c2) | User-defined sizes, input validation |
| std::vector | O(r1*c2*c1) | O(r1*c2) | Flexible size, safer memory management |