WAP USING CONSTRUCTOR OVERLOADING TO ADD ELEMENTS OF TWO MATRICES
#include<conio.h>
#include<iostream.h>
class matrix
{
int m,n;
int arr[100][100];
public:
void getdata(int,int);
void putdata();
friend matrix operator+(matrix &,matrix &);
};
void matrix::getdata(int r,int c)
{
m=r;
n=c;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>arr[i][j];
}
void matrix::putdata()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<arr[i][j]<<"\t";
cout<<endl;
}
}
matrix operator+(matrix &x,matrix &y)
{
matrix z;
z.m=x.m;
z.n=x.n;
for(int i=0;i<x.m;i++)
for(int j=0;j<x.n;j++)
z.arr[i][j]=x.arr[i][j]+y.arr[i][j];
return(z);
}
void main()
{
clrscr();
matrix a,b,c;
int x,y;
cout<<"Enter the size of the matrix::";
cin>>x>>y;
cout<<"\n\nEnter the elements of the matrix 1::"<<endl;
a.getdata(x,y);
cout<<"\n\nEnter the elements of the matrix 2::"<<endl;
b.getdata(x,y);
c=a+b;
cout<<"\n\nElements of the matrix 1::"<<endl;
a.putdata();
cout<<"\n\nElements of the matrix 2::"<<endl;
b.putdata();
cout<<"\n\nElements of the matrix after addition::"<<endl;
c.putdata();
getch();
}
#include<conio.h>
#include<iostream.h>
class matrix
{
int m,n;
int arr[100][100];
public:
void getdata(int,int);
void putdata();
friend matrix operator+(matrix &,matrix &);
};
void matrix::getdata(int r,int c)
{
m=r;
n=c;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>arr[i][j];
}
void matrix::putdata()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<arr[i][j]<<"\t";
cout<<endl;
}
}
matrix operator+(matrix &x,matrix &y)
{
matrix z;
z.m=x.m;
z.n=x.n;
for(int i=0;i<x.m;i++)
for(int j=0;j<x.n;j++)
z.arr[i][j]=x.arr[i][j]+y.arr[i][j];
return(z);
}
void main()
{
clrscr();
matrix a,b,c;
int x,y;
cout<<"Enter the size of the matrix::";
cin>>x>>y;
cout<<"\n\nEnter the elements of the matrix 1::"<<endl;
a.getdata(x,y);
cout<<"\n\nEnter the elements of the matrix 2::"<<endl;
b.getdata(x,y);
c=a+b;
cout<<"\n\nElements of the matrix 1::"<<endl;
a.putdata();
cout<<"\n\nElements of the matrix 2::"<<endl;
b.putdata();
cout<<"\n\nElements of the matrix after addition::"<<endl;
c.putdata();
getch();
}

No comments:
Post a Comment