Matrix Multiplication
Matrix Multiplication in C++ (Multiply Two Matrix)
C++ Matrix Multiplication Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
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}};
// Matrix multiplication
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];
}
}
}
cout << "Result matrix:" << endl;
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}Result matrix: 58 64 139 154
Understanding Matrix Multiplication
This program teaches you how to multiply two matrices in C++. Matrix multiplication is a fundamental operation in linear algebra, used extensively in mathematics, computer graphics, machine learning, and scientific computing. Unlike element-wise multiplication, matrix multiplication follows specific rules and produces a result matrix with dimensions determined by the input matrices.
---
1. What This Program Does
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).
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Matrix Multiplication Rules
Dimension Requirement
:
For matrices A (m×n) and B (p×q) to be multipliable:
Result Dimensions
:
Multiplication Process
:
---
4. 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}};
---
5. 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
---
6. Understanding the Dot Product
Dot Product Concept
:
Each element in the result is the dot product of:
Example
(result[0][0]):
---
7. Displaying the Result
The program displays the result matrix:
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
}
cout << endl;
}
Output:
58 64
139 154
---
cout << result[i][j] << " ";8. Important Matrix Multiplication Properties
Not Commutative
:
Associative
:
Distributive
:
Identity Matrix
:
---
9. When to Use Matrix Multiplication
Linear Algebra
:
Computer Graphics
:
Machine Learning
:
Scientific Computing
:
---
10. Important Considerations
Dimension Compatibility
:
Initialization
:
Time Complexity
:
Memory Requirements
:
---
11. Common Mistakes
Wrong Loop Order
:
Not Initializing Result
:
Dimension Mismatch
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning matrix operations, understanding nested loops, and preparing for advanced topics in linear algebra, computer graphics, machine learning, and scientific computing in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Matrix Multiplication
This C++ program is part of the "Array Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.