/*A 'C++' PROGRAM TO IMPLEMENT MATRIX OPERATIONS USING OVERLOADED OPERATORS.
(<<,>>,+,-,*)
THE OPERATIONS SUPPORTED BY THIS ADT ARE:
a)READING OF A MATRIX b)PRINTING OF A MATRIX
c)ADDITION OF MATRICES d)SUBTRACTION OF MATRICES
e)MULTIPLICATION OF MATRICES
NAME :G.SanjeevaReddy
ROLLNO :09121F0026
BRANCH :M.C.A,II SEM A-SEC */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int m1,n1,m2,n2;
class matrix
{
int a[10][10],m,n;
public:
matrix(int a,int b)
{
m=a;
n=b;
}
friend istream & operator >>(istream &get,matrix &m);
friend ostream & operator <<(ostream &put,matrix &m);
matrix operator +(matrix);
matrix operator -(matrix);
matrix operator *(matrix);
};
istream & operator >>(istream &get,matrix &x)
{
for(int i=0;i<x.m;i++)
for(int j=0;j<x.n;j++)
get>>x.a[i][j];
return get;
}
ostream & operator <<(ostream &put,matrix &x)
{
for(int i=0;i<x.m;i++)
{
for(int j=0;j<x.n;j++)
put<<x.a[i][j]<<"\t";
put<<endl;
}
return put;
}
matrix matrix:: operator +(matrix x)
{
matrix c(m1,n1);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]+x.a[i][j];
return c;
}
matrix matrix:: operator -(matrix x)
{
matrix c(m1,n1);
c.m=m;
c.n=n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]-x.a[i][j];
return c;
}
matrix matrix:: operator *(matrix x)
{
matrix c(m1,n2);
for(int i=0;i<m1;i++)
for(int j=0;j<n2;j++)
{
c.a[i][j]=0;
for(int k=0;k<n1;k++)
c.a[i][j]+=(a[i][k]*x.a[k][j]);
}
return c;
}
void main()
{
clrscr();
cout<<"\nEnter the order of first matrix :";
cin>>m1>>n1;
cout<<"\nEnter the order of second matrix :";
cin>>m2>>n2;
matrix a(m1,n1),b(m2,n2),c(m1,n1),m(m1,n1),n(m1,n2);
if(m1==n2)
{
cout<<"\nEnter the elements of first matrix :";
cin>>a;
cout<<"\nEnter the elements of second matix :";
cin>>b;
c=a+b;
cout<<"\nThe first matrix A :\n";
cout<<a;
cout<<"\nThe second matrix B :\n";
cout<<b;
cout<<"\nThe resultant matrix after addition is :\n";
cout<<c;
m=a-b;
cout<<"\nThe resultant matrix after subtraction is :\n";
cout<<m;
n=a*b;
cout<<"\nThe resultant matrix after multiplication is :\n";
cout<<n;
}
else
cout<<"\nMatrix operation is not possible";
getch();
}
/* OUTPUT
------
Enter the order of first matrix :2
2
Enter the order of second matrix :2
2
Enter the elements of first matrix :1
2
3
4
Enter the elements of second matix :1
0
0
1
The first matrix A :
1 2
3 4
The second matrix B :
1 0
0 1
The resultant matrix after addition is :
2 2
3 5
The resultant matrix after subtraction is :
0 2
3 3
The resultant matrix after multiplication is :
1 2
3 4
*/
No comments:
Post a Comment