#include<iostream.h>
#include<conio.h>
class mat
{
int a[3][3],i,j,k;
public:
void get()
{
cout<<"enter 3*3 matrixs ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
}
void show ()
{
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
}
}
mat operator +(mat x)
{
mat t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
t.a[i][j]=a[i][j]+x.a[i][j];
}
return t;
}
mat operator*(mat x)
{
mat t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
t.a[i][j]=0;
for(k=0;k<3;k++)
t.a[i][j]=t.a[i][j]+(a[i][k]*x.a[k][j]);
}
}
return t;
}
};
void main()
{
clrscr();
mat p,q,r;
p.get();
q.get();
r=p+q;
cout<<"\n add :";
r.show();
r=p*q;
cout<<" \n\nmul :";
r.show();
getch();
}
Sunday, 7 December 2014
Program to perform addition and multiplication of two 3x3 matrices in C++
Posted by Madan
Posted on 13:30





