#include
class matrix
{
private:
int m, z[10][10];
public:
int i, j, k;
void getdata();
{
cout<<"Enter the order of the square matrix: "; cin>m;
cout<<"Enter the "<<m<<"*"<<m<<" elements: "<<endl;
for(i=0;i<m;i++)
for(j=0;j<m;j++) cin>>z[i][j];
}
friend int check(matrix a, matrix b)
{
if(a.m == b.m)
return 1;
else
return 0;
}
void display()
{
cout<<"Size is "<<m<<"*"<<m<<endl;
for(i=0;i<m;i=+)
{
for(j=0; j<m; j++)
cout<<z[i][j]<<"\t";
cout<<endl;
}
}
matrix sum(matrix, matrix);
matrix product(matrix,matrix);
};
matrix matrix :: sum(matrix p, matrix q)
{
int d, e, t;
matrix r;
r.m = p.m;
t = p.m;
for(d=0; d<t; d++)
{
for(e=0; e<t; e++)
{
r.z[d][e] = p.z[d][e] + q.z[d][e];
}
}
return r;
}
matrix matrix :: product(matrix p, matrix q)
{
matrix r;
int f,g,h,t;
r.m = p.m;
t = p.m;
for(f=0; f<t; f++)
{
for(g=0;g<t;g++)
{
r.z[f][g] = 0;
for(h=0; h<t; h++)
{
r.z[f][g] = r.z[f][g] + p.z[f][h]*q.z[h][g];
}
}
}
return(r);
}
int main()
{
matrix a, b,c, d;
cout<<"\n Enter the details of 1st matrix: "<<endl;
a.getdata();
cout<<"\n Enter the details of 2nd matrix: "<<endl;
b.getdata();
cout<<"\n 1st Matrix:"<<endl;
a.display();
cout<<"\n 2nd matrix: "<<endl;
b.display();
int x = check(a, b);
if(x==1)
{
c = c.sum(a,b);
cout<<"\n Sum of 2 matrix: "<<endl;
c.display();
d = d.product(a,b);
cout<<"\n Product of 2 matrix: "<<endl;
d.display();
}
else
cout<<"\n Addition and multiplication not possible!!\n";
return(0);
}
OUTPUT
Enter the details of 1st matrix
Enter the order of the matrix: 2
Enter the 2*2 elements:
1
2
3
4
Enter the details of 2nd matrix
Enter the order of the matrix: 2
Enter the 2*2 elements:
4
3
1
2
1st matrix:
Size is 2*2
1 2
3 4
2nd matrix:
Size is 2*2
4 3
1 2
Sum of 2 matrix:
Size is 2*2
5 5
4 6
Product of 2 matrix:
Size is 2*2
6 7
16 17