How to find product of given two matrices in C++

In this program, we are going to find the product of given two matrices in C++ with some basic commands and nested loop. This problem is based on the application of array. The logic to perform the task is almost similar to that in mathematics Following is a short example program to perform this task. Here we will utilize the same algorithm that we use for manual computation of matrix product.

Find the product of given two matrices in C++

Problem statement

User enters two matrices of any size and any values. Write a program in C++ to find the product of these two matrices (if possible).

Definition

The product of two matrices is possible with all matrix types expect with the null matrix.
The product of these matrices calculated as  per the definition in mathematics

Problem approach

• Initialize two arrays to act as two matrices.
• Accept element values for them.
• If possible, find the sum of matrices.
• Print the result on the screen.

Program/ Source code

Following is a complete C++ program to find the matrix multiplication of given two matrices. This code is tested and is working correctly.

```/* C++ program to find product of given two matrices**
***give different sizes for array & the their values to get different result***
** enter array values in the limit -2,147,483,648 to 2,147,483,647 only **
note: please enter values for 3x3 matrix only...
*/

#include <iostream>
using namespace std;

int main()
{

cout<<"Enter matrix 1 elements: ";
int matrix1[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
cin>>matrix1[i][j];             // matrix 1 elements
}

cout<<"Enter matrix 2 elements: ";
int matrix2[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
cin>>matrix2[i][j];             // matrix 2 elements
}

cout<<endl;
cout<<"Matrix 1 is: "<<endl;
for(int i=0;i<3;i++){                   // print matrix 1
for(int j=0;j<3;j++)
cout<<matrix1[i][j]<<"\t";
cout<<endl;
}

cout<<"Matrix 2 is: "<<endl;
for(int i=0;i<3;i++){                   // print matrix 2
for(int j=0;j<3;j++)
cout<<matrix2[i][j]<<"\t";
cout<<endl;
}

cout<<endl;

int product[3][3];

for(int i=0;i<3;i++){                   // calculate matrix product
for(int j=0;j<3;j++){
product[i][j]=matrix1[i][0]*matrix2[0][j]+
matrix1[i][1]*matrix2[1][j]+\
matrix1[i][2]*matrix2[2][j];
}
}

cout<<"Product of the matrices is: "<<endl;
for(int i=0;i<3;i++){                   // print matrix product
for(int j=0;j<3;j++)
cout<<product[i][j]<<"\t";
cout<<endl;
}

return 0;
}
```

Sample output

```Enter matrix 1 elements: 1
0
0
0
1
0
0
0
1
Enter matrix 2 elements: 5
6
2
9
4
6
2
3
1

Matrix 1 is:
1 0 0
0 1 0
0 0 1
Matrix 2 is:
5 6 2
9 4 6
2 3 1

Product of the matrices is:
5 6 2
9 4 6
2 3 1

Process returned 0 (0x0) execution time : 27.474 s
Press any key to continue.```

Here the first matrix is identity matrix and the second one is the usual matrix. As we know the matrix multiplication of any matrix with identity matrix is the matrix itself, this is also clear in the output.

```Enter matrix 1 elements: 2
3
5
3
1
2
6
3
4
Enter matrix 2 elements: 6
1
3
2
1
2
1
2
3

Matrix 1 is:
2 3 5
3 1 2
6 3 4
Matrix 2 is:
6 1 3
2 1 2
1 2 3

Product of the matrices is:
23 15 27
22 8 17
46 17 36

Process returned 0 (0x0) execution time : 109.818 s
Press any key to continue.```

Program explanation

1. Declare two arrays of size 3×3 which act as matrix of this size.
2. Take input for these array elements from user.
3. Declare another similar array to hold the result.
4. To calculate the product of matrices, use nested for loop. Inside the loop calculate the first element (say (0,0) position of product matrix) as per definition.
5. Store the result using appropriate assignment operators.
6. Print given matrices along with the result on the screen.