Multiply Two Matrix

Multiply two matrix” is a standard operation which can be applied over two or more matrices. Here, we are given two matrices of dimension r1*c1 and r2*c2 respectively. Our task is to multiply the following matrices and store the result in resultant matrix.

CONDITION : The two matrices can only be added if the number of rows of first matrix is equal to number of columns of second matrix.

Example:

R1 = 2, C1 =3

R2 = 3 C2 = 2

123
123
Matrix 1

45
45
45
Matrix 2

2430
2430
Resultant Matrix

C++ Program to add two matrices is as follows:

#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int r1,c1,r2,c2;  
    // Scan Dimensions of the matrix  
    cout<<"Enter the dimensions of first matrix:\n";  
    cin>>r1>>c1;  
    cout<<"Enter the dimensions of second matrix:\n";  
    cin>>r2>>c2;  
      
    int first[r1][c1];  
    int second[r2][c2];  
    int resultant[r1][c2];  
      
    // Scan Matrix1 Elements  
    cout<<"Enter the elements of first matrix:\n";  
    for(int i = 0 ; i < r1 ; i++)  
    for(int j = 0 ; j < c1 ; j++)  
    cin>>first[i][j];  
      
    // Scan Matrix2 Elements  
    cout<<"Enter the elements of second matrix:\n";  
    for(int i = 0 ; i < r2 ; i++)  
    for(int j = 0 ; j < c2 ; j++)  
    cin>>second[i][j];  
      
      
    // Initialize the resultant matrix with 0  
    for(int i = 0 ; i < r1 ; i++)  
    for(int j = 0 ; j < c2 ; j++)  
    resultant[i][j] = 0;  
      
    // Multiply matrix  
    for (int i = 0; i < r1; ++i) {  
        for (int j = 0; j < c2; ++j) {  
            for (int k = 0; k < c1; ++k) {  
                resultant[i][j] += first[i][k] * second[k][j];  
            }  
        }  
    }  
      
    //Print Resultant matrix  
    cout<<"The Resultant Matrix is:\n";  
    for(int i = 0 ; i < r1 ; i++)  
    {  
        for(int j = 0 ; j < c2 ; j++)  
        {  
            cout<<resultant[i][j]<<" ";  
        }  
        cout<<endl;  
    }  
}  

OUTPUT: 
Enter the dimensions of first matrix: 2 3
Enter the elements of second matrix:3 2
Enter the elements of first matrix:
1 2 3
1 2 3
Enter the elements of second matrix:
4 5
4 5
4 5
The Resultant Matrix is:
24 30
24 30

Related Posts:

  1. Program to check Identity Matrix.
  2. Program to check Idempotent Matrix.
  3. Program to check Involuntary Matrix.
  4. Program to print matrix in zig-zag order.
  5. Program to print matrix in spiral order.
  6. Program to sort a matrix.
  7. Program to Add two Matrix.
  8. Program to Transpose a Matrix.
  9. Program to find smallest and largest word of the String.
  10. Program to find Most Frequent Character of the String.
  11. Program to Remove all the blank spaces from the String.
  12. Program to check if String is isogram or not.
  13. Program to Reverse Each word of the String.
  14. Program to Print All the Substring of the Given String.
  15. Program to find longest palindromic Substring.
  16. Program to check Anagram Strings.
  17. Program to check whether two Strings are Rotation of each other or not.
  18. Program to check Palindromic Anagram.
  19. Program to print all the Palindromic Substring of the String.
  20. Program to check Panagram String.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *