Matrix multiplication in C++ is the process of multiplying two matrices to obtain a new matrix. It involves multiplying corresponding elements and summing them up to form the resulting matrix.
Example:
Suppose we have two matrices:
Matrix A: \(
\begin{bmatrix}
1 & 2\\
3 & 4
\end{bmatrix}\) and Matrix B: \(
\begin{bmatrix}
4 & 3\\
2 & 1
\end{bmatrix}\)
To perform matrix multiplication, we multiply corresponding elements and sum them up:
Resultant Matrix C:
\( \begin{bmatrix}1*4 + 2*2 & 1*3 + 2*1\\
3*4 + 4*2 & 3*3 + 4*1
\end{bmatrix}\) = \( \begin{bmatrix}
8 & 5\\
20 & 13
\end{bmatrix}\)
The resultant matrix C is the product of matrices A and B.
Write a C++ Program to Perform Matrix Multiplication.
1. The program takes two matrices and multiplies them.
2. If number of columns of matrix A is not equal to number of rows of matrix B, then matrices cannot be added.
3. The program is exited.
4. Else they are multiplied and the result is printed.
5. Exit.
Here is the source code of C++ Program to Multiply Two Matrices. The program output is shown below.
/*
* C++ program to perform matrix multiplication
*/
#include<iostream>
using namespace std;
int main ()
{
int r1, c1, r2, c2, i, j, k;
int A[5][5], B[5][5], C[5][5];
cout << "Enter number of rows and columns of matrix A : ";
cin >> r1 >> c1;
cout << "Enter number of rows and columns of matrix B : ";
cin >> r2 >> c2;
if (c1 != r2)
{
cout << "Matrices cannot be multiplied!";
exit(0);
}
cout << "Enter elements of matrix A : ";
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> A[i][j];
cout << "Enter elements of matrix B : ";
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
cin >> B[i][j];
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
C[i][j] = 0;
for (k = 0; k < r2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product of matrices\n";
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << "\n";
}
return 0;
}
1. The user is asked to enter number of rows and columns of matrix A and matrix B.
2. If number of columns of matrix A is not equal to number of rows of matrix B, then matrices cannot be multiplied.
3. The program is exited using the function exit().
4. Else if they are equal, matrices are multiplied using for loops and the product is stored in matrix C.
5. The result is then printed.
Time Complexity: O(r1 * c1 * r2)
The time complexity for matrix multiplication is O(r1 * c1 * r2), where r1, c1, and r2 are the number of rows and columns of matrices A and B, respectively. The three nested loops contribute to this time complexity.
Space Complexity: O(r1 * c2)
The space complexity of the code is O(r1 * c2) because it requires a new matrix C to store the product of matrices A and B. The size of matrix C depends on the number of rows of matrix A (r1) and the number of columns of matrix B (c2).
Testcase 1: We enter the number of rows and columns for both matrices A and B. We provide the elements for both matrices and check the product.
Enter number of rows and columns of matrix A : 2 2 Enter number of rows and columns of matrix B : 2 2 Enter elements of matrix A : 1 2 3 4 Enter elements of matrix B : 4 3 2 1 Product of matrices 8 5 20 13
Testcase 2: In this case, the columns of the first matrix are not equal to the rows of the second matrix.
Enter number of rows and columns of matrix A : 1 3 Enter number of rows and columns of matrix B : 2 3 Matrices cannot be multiplied!
Testcase 3: In this case, we input the number of rows and columns for both matrices A and B. Provide elements for Matrix A and Matrix B. Program performs multiplication and outputs the product matrix.
Enter number of rows and columns of matrix A : 1 1 Enter number of rows and columns of matrix B : 1 3 Enter elements of matrix A : 3 Enter elements of matrix B : 1 2 3 Product of matrices 3 6 9
To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Apply for Information Technology Internship
- Practice Computer Science MCQs
- Buy C++ Books
- Buy Computer Science Books