Wednesday, 26 September 2018

Multiplication of two matrices of order m*n and p*q in C++

Multiplication of matrices


The multiplication of two matrices is possible only if the number of columns in the first matrix is equal to the number of rows in the second matrix. The resulting matrix (i.e. product) will have the number of rows equal to the number of rows in the first matrix and no of columns equal to the number of column in the second matrix.

The multiplication of two matrices of order m*n and p*q is possible only if n=p and the order of resultant matrix will be m*q.

Multiplication is obtained as follows:

The product of two matrices of variable size
 MATRIX MULTIPLICATION









The ijth element of the product matrix is obtained by the summation of the product of the corresponding terms of the ith row of the first matrix and jth column of the second matrix.


Write a program in C++ to find the product of two matrices of order m*n and p*q.


In the program, we input two matrices using nested for loop in the form of a two-dimensional array. We need to check the condition required for the multiplication of two matrices. We also need to confirm that the user has entered the rows and columns of matrix smaller than the size of the array. If both conditions satisfy then the multiplication is performed and we get the result.

Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,i,j,k;
clrscr();
cout<<"Enter the number of rows in first matrix=";
cin>>m;
cout<<"Enter the number of column in first matrix=";
cin>>n;
cout<<"Enter the number of rows in second matrix=";
cin>>p;
cout<<"Enter the number of column in second matrix=";
cin>>q;
if ((n!=p)||(m>20)||(n>20)||(p>20)||(q>20))
  {
cout<<"\nInvalid input";
goto x;
}
cout<<"Input of first matrix\n";
        for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter the element of["<<i<<"]["<<j<<"]= ";
cin>>a[i][j];
}
}
cout<<"Input of second matrix\n";
        for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<"Enter the element of["<<i<<"]["<<j<<"]= ";
cin>>b[i][j];
}
}
for(i=0;i<m;i++)                                                                                   //Multiplication of matrix
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
cout<<"\nYour output matrix\n\n";
         for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
x:getch();
}



Output


Program to calculate product of matrix
MATRIX MULTIPLICATION



















Explanation


In the program we are performing multiplication of two matrix of order 3*2 and 2*3. The input of is taken as shown above. The multiplication is calculated as shown below:

i<3          j<3          k<2          c[i][j]          c[i][j]=c[i][j]+a[i][k]*b[k][j]
0             0              0               0               c[0][0]=c[0][0]+a[0][0]*b[0][0]=0+1*5=5
                               1               5               c[0][0]=c[0][0]+a[0][1]*b[1][0]=5+3*4=17      
               1              0               0               c[0][1]=c[0][1]+a[0][0]*b[0][1]=0+1*3=3 
                               1               3               c[0][1]=c[0][1]+a[0][1]*b[1][1]=3+3*2=9  
               2              0               0               c[0][2]=c[0][2]+a[0][0]*b[0][2]=0+1*5=5 
                               1               5               c[0][2]=c[0][2]+a[0][1]*b[1][2]=5+3*6=23.


The process continues till i=2 and calculates all elements of product matrix of order 3*3. We obtain the result and it is printed on the screen. 





No comments:

Post a Comment