1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include<iostream>
using namespace std;
void input_mat(int x[][3],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cin>>x[i][j]; } }
void show_mat(int x[][3],int m,int n)
{
int i,j;
for (i=0;i<m;i++)
{ for(j=0;j<n;j++)
{
cout<<x[i][j]<<" ";
} cout<<endl; } }
void add_mat(int x[][3],int y[][3],int z[][3],int r1,int c1,int r2,int c2)
{int i,j;
show_mat(x,r1,c1);
show_mat(y,r1,c1);
if(r1==r2&&c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
z[i][j]=x[i][j]+y[i][j];
}
cout<<"Sum of matrices is given by the following matrix."<<endl;
show_mat(z,r1,c1);
cout<<endl;
}
else
cout<<"Matrices can't be added.";
cout<<endl;
}
int main()
{
int p,q,r,s;
cout<<"Enter number of rows and columns of matrix and all the elements."<<endl;
cin>>p>>q;
int a[][3]={0}; input_mat(a,p,q); show_mat(a,p,q);
cout<<"Enter number of rows and columns of matrix and all elements."<<endl;
cin>>r>>s;
int b[][3]={0}; input_mat(b,r,s); show_mat(b,r,s);
int c[][3]={0};
add_mat(a,b,c,p,q,r,s);
return 0;
}
| |