PROGRAM TO FIND SUM OF TWO MATRICES

/*PROGRAM TO FIND SUM OF TWO MATRICES*/

#include<conio.h>

#include<iostream.h>

class sum_matrices

{

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

public:

void input();

void output();

};

void sum_matrices::input()

{

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

cin>>m>>n;

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

cin>>p>>q;

}

void sum_matrices::output()

{

if(n==p)

{

cout<<“Enter “<<m*n<<” elements into first matrix:\n”;

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

{

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

cin>>a[i][j];

}

cout<<“Enter “<<p*q<<” elements into second matrix:\n”;

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

{

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

cin>>b[i][j];

}

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(i=0;i<p;i++)

{

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

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

cout<<endl;

}

cout<<“Multiplication of given two matrices is: \n”;

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<<“Multiplication of matrices is not possible”;

}

void main()

{

sum_matrices s1;

clrscr();

s1.input();

s1.output();

getch();

}

Leave a Reply

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