Write C++ program to create class matrix which contains 3 functions 1) Accept 2 Matrix 2) Display 2 Matrix 3) Sum of 2 Matrix
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<conio>
#include<iostream>
void accept(void);
void display(void);
void sum(void);
class matrix
{
int i,j,a[10][10],b[10][10],r1,c1,r2,c2; // a & b = 2d array , r1= rows1 , c1=columns1 , r2= rows2 , //c2=columns2
public:
void accept(void);
void display(void);
void sum(void);
};
void matrix::accept(void)
{
cout<<"\nEnter how many rows in 1st matrix :";
cin>>r1;
cout<<"\nEnter how many columns in 1st matrix :";
cin>>c1;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<"enter element of "<<": ";
cin>>a[i][j];
cout<<endl;
}
}
cout<<"\nEnter how many rows in 2nd matrix :";
cin>>r2;
cout<<"\nEnter how many columns in 2nd matrix :";
cin>>c2;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cout<<"\nEnter element of ";
cin>>b[i][j];
cout<<endl;
}
}
}
void matrix::display(void)
{
cout<<"\n1st matrix is : "<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\n2nd matrix is : "<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<endl;
}
cout<<endl;
}
void matrix::sum(void) // sum of 2D array function
{
int c[10][10]={0,0};
cout<<"\nSum of matrix is : "<<endl;;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
matrix s; // object created
s.accept();
s.display();
s.sum();
getch();
return(0);
}
0 Comments