#include <iostream>
using namespace std;
class matadd
{
public :
int a[30][30],b[30][30],r1,r2,l,c1,c2,i,j;
void getdata ();
void display ();
void add();
};
void matadd::getdata()
{
cout <<"Enter the row size for first matrix : ";
cin>>r1;
cout <<r1<<endl;
cout <<"Enter the column size for first matrix: ";
cin >>c1;
cout <<c1<<endl;
cout <<"Enter the row size for second matrix : ";
cin>>r2;
cout <<r2<<endl;
cout <<"Enter the column size for second matrix: ";
cin >>c2;
cout <<c2<<endl;
if (r1==r2&&c1==c2)
{
cout <<"Enter the elements for first matrix:"<<endl;
for (i=0;i<r1;i++)
{
for (j=0;j<c1;j++)
{
cin >>a[i][j];
}
}
cout <<"Enter the elements for second matrix:"<<endl;
for (i=0;i<r2;i++)
{
for (j=0;j<c2;j++)
{
cin >>b[i][j];
}
}
}
else
{
cout <<"matrix addition is not performed "<<endl;
cout <<"Note : row size and column size of two matrices must be equal "<<endl;
}
}
void matadd::display()
{
if (r1==r2&&c1==c2)
{
cout <<"First matrix is "<<endl;
for (i=0;i<r1;i++)
{
cout <<endl;
for (j=0;j<c1;j++)
{
cout <<"\t";
cout <<a[i][j];
}
}
cout <<"\n second matrix is "<<endl;
for (i=0;i<r2;i++)
{
cout <<endl;
for (j=0;j<c2;j++)
{
cout <<"\t";
cout <<b[i][j];
}
}
}
}
void matadd::add()
{
if (r1==r2&&c1==c2)
{
cout <<"\n Resultant matrix is :"<<endl;
for (i=0;i<r1;i++)
{
cout <<endl;
for (j=0;j<c1;j++)
{
cout <<"\t";
cout <<a[i][j]+b[i][j];
}
}
}
}
int main() {
matadd m;
cout <<"Addition of two matrices "<<endl;
m. getdata();
m. display ();
m. add();
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: