Program to find the sum of two matrices by using objects | C++ Programming
Sunday, March 01, 2020//program to find the sum of two matrices by using objects
#include<iostream.h>
#include<conio.h>
class MATRIX
{ private:
int a[20][20];
int m;
int n;
public:
void enter();
void display();
void add_mat(MATRIX,MATRIX);
} obj;
void MATRIX::enter()
{cout<<"\n Enter the number of rows:";
cin>>m;
cout<<"\n Enter the number of columns:";
cin>>n;
cout<<"\n Enter the elements of matrix:";
for(int i=0;i<m;i++)
{ for(int j=0;j<n;j++)
{ cout<<"\n Enter a["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
}
void MATRIX::display()
{
cout<<"\n matrix is:";
for(int i=0;i<m;i++)
{ cout<<"\n";
for(int j=0;j<n;j++)
cout<<a[i][j]<<" ";
}
}
void MATRIX::add_mat(MATRIX A1,MATRIX A2)
{ if(A1.m==A2.m && A1.n==A2.n)
{
obj.m=A1.m;
obj.n=A2.n;
for(int i=0;i<A1.m;i++)
for(int j=0;j<A2.n;j++)
{
obj.a[i][j]=A1.a[i][j]+A2.a[i][j];
}
cout<<"\n Addition of matrices:";
obj.display();
}
else
{
cout<<"\n\nMatrices cannot be added!!!";
getch();
}
}
void main()
{ clrscr();
MATRIX a1,a2,a3;
a1.enter();
a1.display();
a2.enter();
a2.display();
a3.add_mat(a1,a2);
getch();
}
#include<iostream.h>
#include<conio.h>
class MATRIX
{ private:
int a[20][20];
int m;
int n;
public:
void enter();
void display();
void add_mat(MATRIX,MATRIX);
} obj;
void MATRIX::enter()
{cout<<"\n Enter the number of rows:";
cin>>m;
cout<<"\n Enter the number of columns:";
cin>>n;
cout<<"\n Enter the elements of matrix:";
for(int i=0;i<m;i++)
{ for(int j=0;j<n;j++)
{ cout<<"\n Enter a["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
}
void MATRIX::display()
{
cout<<"\n matrix is:";
for(int i=0;i<m;i++)
{ cout<<"\n";
for(int j=0;j<n;j++)
cout<<a[i][j]<<" ";
}
}
void MATRIX::add_mat(MATRIX A1,MATRIX A2)
{ if(A1.m==A2.m && A1.n==A2.n)
{
obj.m=A1.m;
obj.n=A2.n;
for(int i=0;i<A1.m;i++)
for(int j=0;j<A2.n;j++)
{
obj.a[i][j]=A1.a[i][j]+A2.a[i][j];
}
cout<<"\n Addition of matrices:";
obj.display();
}
else
{
cout<<"\n\nMatrices cannot be added!!!";
getch();
}
}
void main()
{ clrscr();
MATRIX a1,a2,a3;
a1.enter();
a1.display();
a2.enter();
a2.display();
a3.add_mat(a1,a2);
getch();
}
0 Comments