![]() |
VOOZH | about |
Given two matrices, the task is to multiply them. Matrices can either be square or rectangular:
Examples:
(Square Matrix Multiplication)
Input: m1[m][n] = { {1, 1}, {2, 2} }
m2[n][p] = { {1, 1}, {2, 2} }
Output: res[m][p] = { {3, 3}, {6, 6} }(Rectangular Matrix Multiplication)
Input: m1[3][2] = { {1, 1}, {2, 2}, {3, 3} }
m2[2][3] = { {1, 1, 1}, {2, 2, 2} }
Output: res[3][3] = { {3, 3, 3}, {6, 6, 6}, {9, 9, 9} }
- The number of columns in Matrix-1 must be equal to the number of rows in Matrix-2.
- Output of multiplication of Matrix-1 and Matrix-2, results with equal to the number of rows of Matrix-1 and the number of columns of Matrix-2 i.e. rslt[R1][C2]
Below is the implementation of the multiplication of two matrices:
Multiplication of given two matrices is: 3 3 6 6
Time complexity: O(R1 * C2 * R2) for given matrices mat1[R1][C1] and mat2[R2][C2]
Auxiliary space: O(R1 * C2)
To solve the problem follow the below idea:
We use pointers in C/C++ to multiply matrices
Prerequisite:How to pass a 2D array as a parameter in C?
Below is the implementation of the above approach:
3 3 6 6
Related Article