# How to multiply two matrices in C++

You have already studied how to multiply two matrices in mathematics. Here, I am going to discuss how to multiply two matrices in C++ and its implementation. User is required to enter order of each of the matrices and the given program will calculate its result.

**NOTE:-**

If one matrix has order m x n and other has order n x o ,then only matrices can be multiplied otherwise not.

## C++ code to multiply two matrices

#include<iostream> using namespace std; int main() { int n,m,n1,m1,i,j,k,sum=0; int mat1[8][8],mat2[8][8],res[8][8]; cout<<"enter the order of the first matrix"<<endl; cin>>n>>m; cout<<"enter first matrix"<<endl; for(i=0;i<n;i++) { for(j=0;j<m;i++) { cin>>mat1[i][j]; } } cout<<"enter the order of the second matrix"<<endl; cin>>n1>>m1; cout<<"enter second matrix"<<endl; for(i=0;i<n1;i++) { for(j=0;j<m1;i++) { cin>>mat2[i][j]; } } if(m!=n1) cout<<"matrices cannot be multiplied"<<endl; for(i=0;i<n;i++) { for(j=0;j<m;j++) { for(k=0;k<m1;k++) { sum+=mat1[i][k]*mat2[k][j]; } res[i][j]=sum; sum=0; } } cout<<"Product of the matrices:"<<endl; for(i =0; i < n; i++){ for(j =0; j < m; j++) cout<<res[i][j]; cout<<endl; } return 0; }

Example1

enter the order of the first matrix 2 3 enter first matrix 1 2 3 4 5 6 enter the order of the second matrix 4 5 matrices cannot be multiplied

Now, what has happened in the example stated above?As I already told you that matrices can be only if order of one matrix is a x b and that of other is c x d, then c must be equal to b.If this condition is not followed, matrices cannot be multiplied in any case.

Example 2

enter the order of the first matrix 3 3 enter first matrix 1 2 3 4 5 6 7 8 9 enter the order of the second matrix 3 2 enter second matrix 1 2 3 4 5 6 product of the matrices: 22 28 49 64 76 100

Now, in this example given above, the condition that I had mentioned was followed. So, program asked user for second matrix values. Then, the program calculated the product of the matrices and the result was printed. Clearly , for the case given in Example 2,output matrix will have order of 3 x 2.

Given program has complexity of O(n x m x m1).

#### Explanation of my code:

- First of all, I have initialized all the variables.
- User will enter the size of first matrix and its values.
- Similarly, for second matrix , user will input the values.
- Order of first matrix is nxm.
- Order of second matrix is n1xm1.
- Then order of output matrix will be nxm1.
- We will now simply multiply the elements of each of the matrices by taking three
**for**loops. - Mat1[0,0]*Mat2[0,0]+Mat1[0,1]*Mat2[1,0]+Mat1[0,2]*Mat2[2,0]
- Above equation computes [0,0] element.
- Similarly, we will compute [0,1] element by :
- Mat1[0,0]*Mat2[1,0]+Mat1[0,1]*Mat2[1,1]+Mat1[0,2]*Mat2[2,1]
- This sum that we have computed corresponds to one element of the output matrix.
- Similarly, we can calculate all other elements in the same way and that is what my code is doing.
- At the end, we have to just write two loops to print the output.

So, this was how to multiply two matrices in C++.

Also read,

## Leave a Reply