Related Tags

c++
matrix
communitycreator
multiplication

# How to multiply two matrices

### Overview

To multiply two matrices, the rows of the first matrix must be equal to the number of columns of the second matrix. Matrix multiplication is a dot product. Let’s understand this with the help of a diagram.

Matrix multiplication

### Example

#include <iostream>
using namespace std;

#define size 3      // number of rows  and column

void matixMultiplicaion(int MatrixA[][size], int MatrixB[][size]) {
int result[size][size];  // declare result matrix 3 X 3
for (int i = 0; i < size; i++) { // handle rows
for (int j = 0; j < size; j++) { // handle column
result[i][j] = 0; // assign default value
for (int k = 0; k < size; k++) { // compute dot product of matrix
result[i][j] += MatrixA[i][k] * MatrixB[k][j];
}
cout << result[i][j] << "  "; // print the resultan matrix
}
cout << endl;
}
}
int main() {

int MatrixA[size][size] = { // declare Matrix A 3x3
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
};
int MatrixB[size][size] = { // declare Matrix B 3X3
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
};

/*    since it is square matrix the row and
column size are same so we do not need to check
the row and column are equal or not
*/
matixMultiplicaion(MatrixA, MatrixB);

return 0;
}

### Explanation

• Line 6: We implement the function matixMultiplicaion.
• Line 21–30: We declare two matrices, MatrixA and MatrixB.
• Line 36: We call the matixMultiplicaion function.

RELATED TAGS

c++
matrix
communitycreator
multiplication

CONTRIBUTOR

RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time