Search:

Submit Article

# Program that performs addition of 2 matrix using friend function

Posted By: Easy Tutor     Category: C++ Programming     Views: 13025

## Code for Program that performs addition of 2 matrix using friend function in C++ Programming

```#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class matrix
{
int **p,row,col;
public:
void getdata(void);
friend void matrixadd(matrix &,matrix &);
void display(void);
};

void matrix :: getdata(void)
{
clrscr();
cout<<"Enter Size of Row:-";
cin>>row;
p=newint *[row];
cout<<"Enter size of Coulumn:-";
cin>>col;
cout<<"\nEnter Data for Matrix of size "<<row<<"*"<<col<<endl;
for(int i=0;i<row;i++)
{
p[i]=newint [col];
}
//scaning valuefor(int a=0;a<row;a++)
{
for(int b=0;b<col;b++)
{
cin>>p[a][b];
}
}
}

void matrix :: display(void)
{
cout<<"\n\n\n\n";
cout<<"Display Function\n\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<setw(5)<<p[i][j];
}
cout<<endl;
}
}

void matrixadd(matrix &a,matrix &b)
{
int result[10][10];
if(a.row==b.row && a.col==b.col)
{
for(int i=0;i<a.row;i++)
{
for(int j=0;j<a.col;j++)
{
result[i][j]=a.p[i][j]+b.p[i][j];
}
}
//displayingfor(int x=0;x<a.row;x++)
{
for(int y=0;y<a.col;y++)
{
cout<<setw(5)<<result[x][y];
}
cout<<endl;
}
}
else
cout<<"Invalid Matrix Addition Occurs as size differs\n";
}

void main()
{
matrix o1,o2;
o1.getdata();
o2.getdata();
clrscr();
o1.display();
o2.display();
getch();
clrscr();
cout<<"\n\nAfter Adition Has been Performed\n\n";