*******************************************************************************************************
Matrix operations using overloaded operators in C++
*******************************************************************************************************
Program :To implement matrix operations using overloaded operators
(<<,>>,+,-,*)
The operations supported by this ADT are
a.Reading
b.Printing
c.Addition
d.Subtraction
e.Multiplication */
Source Code :
=========
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #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<<"\n Enter the order of first matrix:"; cin>>m1>>n1; cout<<"\n Enter 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<<"\n Enter the elements of first matrix:"; cin>>a; cout<<"\n Enter the elements of second matrix:"; cin>>b; c=a+b; cout<<"\n The first matrix A:\n"; cout<<a; cout<<"\n The second matrix B:\n"; cout<<b; cout<<"\n The resultant matrix after addition is:\n"; cout<<c; m=a-b; cout<<"\n The resultant matrix after subtraction is:\n"; cout<<m; n=a*b; cout<<"\n The resultant matrix after multiplication is:\n"; cout<<n; } else cout<<"\n Matrix operation is not possible"; getch(); } /*---------------INPUT/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 matrix :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 */ |
46 total views, no views today