1.
Introduction
2.
C++ Program for Multiplication of Square Matrices:
2.1.
C++
3.
C++ Program for Multiplication of Rectangular Matrices
3.1.
C++
4.
4.1.
What is the condition for matrix multiplication to be possible?
4.2.
What is the time complexity of matrix multiplication using nested loops?
4.3.
Can we multiply matrices of different dimensions?
5.
Conclusion
Last Updated: Jul 15, 2024
Easy

# Matrix Multiplication in C++

Rinki Deka
0 upvote

## Introduction

Matrix multiplication is a fundamental operation in mathematics and computer science. It involves multiplying two matrices together to produce a resulting matrix. In C++, we can perform matrix multiplication using nested loops to iterate over the rows and columns of the matrices.

This article will discuss the basics of matrix multiplication, including how to multiply square matrices and rectangular matrices using C++ programs.

## C++ Program for Multiplication of Square Matrices:

To multiply two square matrices in C++, we need to ensure that the number of columns in the first matrix is 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 square matrices:

• C++

### C++

``#include <iostream>using namespace std;const int SIZE = 3;void multiplyMatrices(int A[][SIZE], int B[][SIZE], int C[][SIZE]) {    for (int i = 0; i < SIZE; i++) {        for (int j = 0; j < SIZE; j++) {            C[i][j] = 0;            for (int k = 0; k < SIZE; k++) {                C[i][j] += A[i][k] * B[k][j];            }        }    }}void printMatrix(int matrix[][SIZE]) {    for (int i = 0; i < SIZE; i++) {        for (int j = 0; j < SIZE; j++) {            cout << matrix[i][j] << " ";        }        cout << endl;    }}int main() {    int A[SIZE][SIZE] = {{1, 2, 3},                         {4, 5, 6},                         {7, 8, 9}};    int B[SIZE][SIZE] = {{9, 8, 7},                         {6, 5, 4},                         {3, 2, 1}};    int C[SIZE][SIZE];    cout << "Matrix A:" << endl;    printMatrix(A);    cout << "Matrix B:" << endl;    printMatrix(B);    multiplyMatrices(A, B, C);    cout << "Resulting Matrix (A * B):" << endl;    printMatrix(C);    return 0;}``

Output

``````Matrix A:
1 2 3
4 5 6
7 8 9
Matrix B:
9 8 7
6 5 4
3 2 1
Resulting Matrix (A * B):
30 24 18
84 69 54
138 114 90 ``````

In this program:

1. We define a constant SIZE to represent the size of the square 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 is performed using nested loops. The outer two loops iterate over the rows and columns of the resulting matrix C, while the innermost loop calculates the dot product of the corresponding row of matrix A and column of matrix B.

4. The printMatrix function is used to display a matrix on the console.

5. In the main function, we create two square matrices A and B and initialize them with some values.

6. We print matrices A and B using the printMatrix function.

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.

## 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++

### 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;}``

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.

### 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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass