PROGRAM TO MULTIPLY TWO MATRICES

/*PROGRAM TO MULTIPLY TWO MATRICES*/

#include<conio.h>

#include<iostream.h>

class multiply

{

int m,n,p,q,i,j,k,a[5][5],b[5][5],c[5][5];

public:

void input();

void output();

};

void multiply::input()

{

cout<<“Enter the order of the first matrix: “;

cin>>m>>n;

cout<<“Enter the order of second matrix: “;

cin>>p>>q;

}

void multiply::output()

{

if(n==p)

{

cout<<“Enter the elements of first matrix: \n”;

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cin>>a[i][j];

}

cout<<“Enter the elements of second matrix: \n”;

for(j=0;j<p;j++)

{

for(k=0;k<q;k++)

cin>>b[j][k];

}

cout<<“Elements of first matrix are:\n”;

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cout<<a[i][j]<<”   “;

cout<<endl;

}

cout<<“Elements of second matrix are:\n”;

for(j=0;j<n;j++)

{

for(k=0;k<q;k++)

cout<<b[j][k]<<”   “;

cout<<endl;

}

//MULTIPLYING THE MATRICES

cout<<“Multiplication of matrices is: “<<endl;

for(i=0;i<m;i++)

{

for(k=0;k<q;k++)

{

c[i][k]=0;

for(j=0;j<n;j++)

c[i][k]=c[i][k]+a[i][j]*b[j][k];

cout<<c[i][k]<<”   “;

}

cout<<endl;

}

}

else

cout<<“Matrices cannot be multiplied”;

}

void main()

{

multiply m1;

clrscr();

m1.input();

m1.output();

getch();

}

Leave a Reply

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