Create a class MATRIX of size mxn. Overload + and – operators for addition and subtraction of the MATRIX.
CODING:
#include<iostream.h>
#include<conio.h>
class matrix
{
int r,c,m[30][30];
int i,j;
public:
matrix(){}
matrix(int a,int b)
{
r=a;c=b;
}
void get();
void disp();
matrix operator +(matrix );
matrix operator -(matrix);
};
void matrix::get()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<'['<<i<<','<<j<<"]:";
cin>>m[i][j];
}
}
}
void matrix::disp()
{
for(i=0;i<r;i++)
{
cout<<endl;
for(j=0;j<c;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
matrix matrix::operator +(matrix k)
{
matrix add(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
add.m[i][j]=m[i][j]+k.m[i][j];
}
}
return add;
}
matrix matrix::operator -(matrix k)
{
matrix sub(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sub.m[i][j]=m[i][j]-k.m[i][j];
}
}
return sub;
}
void main()
{
int a,b;
clrscr();
cout<<"Enter the ROW SIZE of Matrix :";
cin>>a;
cout<<"Enter the COLUMN SIZE of Matrix:";
cin>>b;
matrix o1(a,b);
matrix o2(a,b);
cout<<"Enter the Number of MATRIX A:\n";o1.get();
cout<<"Enter the Number of MATRIX B:\n";o2.get();
matrix o3;
o3=o1+o2;
cout<<"MATRIX [A+B]:";o3.disp();
o3=o1-o2;
cout<<"\nMATRIX [A-B]:";o3.disp();
getch();
}
CODING:
#include<iostream.h>
#include<conio.h>
class matrix
{
int r,c,m[30][30];
int i,j;
public:
matrix(){}
matrix(int a,int b)
{
r=a;c=b;
}
void get();
void disp();
matrix operator +(matrix );
matrix operator -(matrix);
};
void matrix::get()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<'['<<i<<','<<j<<"]:";
cin>>m[i][j];
}
}
}
void matrix::disp()
{
for(i=0;i<r;i++)
{
cout<<endl;
for(j=0;j<c;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
matrix matrix::operator +(matrix k)
{
matrix add(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
add.m[i][j]=m[i][j]+k.m[i][j];
}
}
return add;
}
matrix matrix::operator -(matrix k)
{
matrix sub(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sub.m[i][j]=m[i][j]-k.m[i][j];
}
}
return sub;
}
void main()
{
int a,b;
clrscr();
cout<<"Enter the ROW SIZE of Matrix :";
cin>>a;
cout<<"Enter the COLUMN SIZE of Matrix:";
cin>>b;
matrix o1(a,b);
matrix o2(a,b);
cout<<"Enter the Number of MATRIX A:\n";o1.get();
cout<<"Enter the Number of MATRIX B:\n";o2.get();
matrix o3;
o3=o1+o2;
cout<<"MATRIX [A+B]:";o3.disp();
o3=o1-o2;
cout<<"\nMATRIX [A-B]:";o3.disp();
getch();
}
Comments
Post a Comment