Slider

Program for the Multiplication of Two 2D Matrix.

C++ Program to for Multiplication of 2D Array (Matrix). Few points to keep in mind before performing Matrix Multiplication.

In this post, we are going to learn the process of multiplication of two 2D arrays and display the resulting array on screen. 

Example:

Multiplication of Two 2D Matrix
Multiplication of Matrices

Below are a few points to keep in mind before performing Matrix Multiplication:

  • Matrix multiplication is only possible when the number of columns of the first matrix should be equal to the number of rows of the second matrix. 
  • In matrix multiplication, the product of an m x k matrix and k x n matrix is an m x n matrix.
  • Matrix multiplication is not commutative in nature, it means the order multiplication of two matrices matters and can change our result.
  • Each entry in the resulting matrix is the dot product of row elements of the first matrix with column elements of the second matrix.

Below is C++ Code Implementation:

//Program to for Multiplication of 2D Array (Matrix)
#include<iostream>
using namespace std;

int main(){
    int arr1[50][50], arr2[50][50], prod[50][50] = {0};
    int row1, col1, row2, col2;
    //column of first matrix should be equal to row of second matrix
    do{
        cout<<"Enter number of rows and columns for first matrix: ";
        cin>>row1>>col1;

        cout<<"Enter number of rows and columns for second matrix: ";
        cin>>row2>>col2;
    }while(col1 != row2);
    
    //Taking input for first matrix
    cout<<"Enter the elements of first Array: "<<endl;
    for(int i = 0; i < row1; i++){
        for(int j = 0; j < col1; j++){
            cout<<"Enter element for position arr["<<i<<"]["<<j<<"] = ";
            cin>>arr1[i][j];
        }
    }
    //Taking input for second matrix
    cout<<"Enter the elements of second Array: "<<endl;
    for(int i = 0; i < row2; i++){
        for(int j = 0; j < col2; j++){
            cout<<"Enter element for position arr["<<i<<"]["<<j<<"] = ";
            cin>>arr2[i][j];
        }
    }
    //Multiplication of Matrix
    cout<<"The Multiplication of two Matrices: "<<endl;
    for(int i = 0; i < row1; i++){
        for(int j = 0; j < col2; j++){
            for(int k = 0; k < col1; k++){
                prod[i][j] = arr1[i][k] * arr2[k][j];
            }
        }
    }

    cout<<"Displaying Product of Two Matrix: "<<endl;
    for(int i = 0; i < row1; i++){
        for(int j = 0; j < col2; j++){
            cout<<prod[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
Output:
Enter number of rows and columns for first matrix: 2 3
Enter number of rows and columns for second matrix: 3 2
Enter the elements of first Array: 
Enter element for position arr[0][0] = 1
Enter element for position arr[0][1] = 2
Enter element for position arr[0][2] = 3
Enter element for position arr[1][0] = 4
Enter element for position arr[1][1] = 5 
Enter element for position arr[1][2] = 6
Enter the elements of second Array: 
Enter element for position arr[0][0] = 6
Enter element for position arr[0][1] = 5
Enter element for position arr[1][0] = 4
Enter element for position arr[1][1] = 3
Enter element for position arr[2][0] = 2
Enter element for position arr[2][1] = 1
The Multiplication of two Matrices: 
6 3
12 6

In the above code, we have taken the first matrix of size 2 x 3 and the second matrix of size 3 x 2 and the resultant matrix that we get after multiplication is of size 2 x 2 which is satisfying our matrix property.

Next:

0

No comments

Post a Comment

both, mystorymag

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson
Table of Contents