C++ Programming Blog

 
 
 

Image

#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=new int *[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]=new int [col];
 }
//scaning value
for(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];
    }
  }
//displaying
  for(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\";
matrixadd(o1,o2);
getch();
}

 
 
Didn't find what you were looking for? Find more on Program that performs addition of 2 matrix using friend function