C++ Program for Multiplication of Rectangular Matrices
Multiplying rectangular matrices is similar to multiplying square matrices, but we need to consider the dimensions of the matrices. The number of columns in the first matrix must be equal to the number of rows in the second matrix. The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.
Let’s see a C++ program that demonstrates the multiplication of two rectangular matrices:
C++
#include <iostream>
using namespace std;
const int ROWS_A = 2;
const int COLS_A = 3;
const int ROWS_B = 3;
const int COLS_B = 2;
void multiplyMatrices(int A[][COLS_A], int B[][COLS_B], int C[][COLS_B]) {
for (int i = 0; i < ROWS_A; i++) {
for (int j = 0; j < COLS_B; j++) {
C[i][j] = 0;
for (int k = 0; k < COLS_A; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
template <int COLS>
void printMatrix(int rows, int cols, int matrix[][COLS]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int A[ROWS_A][COLS_A] = {{1, 2, 3},
{4, 5, 6}};
int B[ROWS_B][COLS_B] = {{7, 8},
{9, 10},
{11, 12}};
int C[ROWS_A][COLS_B];
cout << "Matrix A:" << endl;
printMatrix(ROWS_A, COLS_A, A);
cout << "Matrix B:" << endl;
printMatrix(ROWS_B, COLS_B, B);
multiplyMatrices(A, B, C);
cout << "Resulting Matrix (A * B):" << endl;
printMatrix(ROWS_A, COLS_B, C);
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output
Matrix A:
1 2 3
4 5 6
Matrix B:
7 8
9 10
11 12
Resulting Matrix (A * B):
58 64
139 154
In this program:
1. We define constants `ROWS_A`, `COLS_A`, `ROWS_B`, and `COLS_B` to represent the dimensions of the rectangular matrices.
2. The `multiplyMatrices` function takes three matrices `A`, `B`, and `C` as input. It multiplies matrices `A` and `B` and stores the result in matrix `C`.
3. The multiplication process is similar to the square matrix multiplication, but we use the appropriate dimensions for the loops.
4. The `printMatrix` function is modified to accept the number of rows and columns as parameters, allowing it to print rectangular matrices.
5. In the `main` function, we create two rectangular matrices `A` and `B` and initialize them with some values.
6. We print matrices `A` and `B` using the `printMatrix` function, passing the respective number of rows and columns.
7. We call the `multiplyMatrices` function to multiply matrices `A` and `B` and store the result in matrix `C`.
8. Finally, we print the resulting matrix `C` using the `printMatrix` function, passing the appropriate dimensions.
Frequently Asked Questions
What is the condition for matrix multiplication to be possible?
For matrix multiplication to be possible, the number of columns in the first matrix must be equal to the number of rows in the second matrix.
What is the time complexity of matrix multiplication using nested loops?
The time complexity of matrix multiplication using nested loops is O(n^3), where n is the size of the matrices.
Can we multiply matrices of different dimensions?
Yes, we can multiply matrices of different dimensions as long as the number of columns in the first matrix matches the number of rows in the second matrix.
Conclusion
In this article, we learned about matrix multiplication in C++. We discussed the basics of multiplying square matrices and rectangular matrices using nested loops. We also provided C++ programs that demonstrate how to implement matrix multiplication for both square and rectangular matrices.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.