#include<iostream.h> #include<conio.h> #define s 20 class matrix { int a[s][s],x,y; public: void get(); void put(); matrix operator+(matrix); matrix operator-(matrix); matrix operator*(matrix); matrix transpose(); }; void matrix::get() { cout<<"enter the order of matrix \n"; cin>>x>>y; cout<<"enter the matrix \n"; for(int i=0;i<x;i++)> for(int j=0;j<y;j++)> cin>>a[i][j]; } void matrix::put() { cout<<"the ans is: \n"; for(int i=0;i<x;i++)> { cout<<"\n \t"; for(int j=0;j<y;j++)> cout<<a[i][j]<<" "; } } matrix matrix::operator+(matrix b) { matrix r; if((x!=b.x)||(y!=b.y)) { cout<<"\n \t matrix addition is not possible the result is incorrect \n\n"; r.x=0; r.y=0; } else { r.x=x; r.y=y; } for(int i=0;i<x;i++)> for(int j=0;j<y;j++)> r.a[i][j]=a[i][j]+b.a[i][j]; return r; } matrix matrix::operator-(matrix b) { matrix r; if((x!=b.x)||(y!=b.y)) { cout<<"\n \t matrix subtraction is not possible the result is incorrect \n \n"; r.x=0; r.y=0; } else { r.x=x; r.y=y; } for(int i=0;i<x;i++)> for(int j=0;j<y;j++)> r.a[i][j]=a[i][j]-b.a[i][j]; return r; } matrix matrix::operator*(matrix b) { matrix r; if((x!=b.y)||(y!=b.x)) { cout<<"\n \t matrix multiplication is not possible the result is incorrect \n\n"; r.x=0; r.y=0; } else { r.x=x; r.y=b.y; } for(int i=0;i<s;i++)> for(int j=0;j<s;j++)> r.a[i][j]=0; for(i=0;i<x;i++)> for(j=0;j<b.y;j++)> for(int k=0;(k<y)||(k><b.x);k++)> r.a[i][j]+=a[i][k]*b.a[k][j]; return r; } matrix matrix::transpose() { matrix r; for(int i=0;i<x;i++)> for(int j=0;j<y;j++)> r.a[i][j]=a[j][i]; r.x=x; r.y=y; return r; } void main() { clrscr(); char op; matrix a,b,c; int t=1; while(t) { cout<<"\t select option \n \n 1)matrix addition\n 2)matrix subtraction \n 3)matrix multiplication \n 4)matrix transpose \n 5)exit \n"; op=getch(); switch(op) { case '1': cout<<"\n \t matrix addition \n"; a.get(); b.get(); c=a+b; c.put(); break; case '2': cout<<"\n \t matrix subtraction \n"; a.get(); b.get(); c=a-b; c.put(); break; case'3': cout<<"\n \t matrix multiplication \n"; a.get(); b.get(); c=a*b; c.put(); break; case'4': cout<<"\n \t matrix transpose \n"; a.get(); c=a.transpose(); c.put(); break; case'5': cout<<"\n \t press any key to exit \n"; t=0; break; default: cout<<"\n \t enter a valid option \n"; } getch(); } getch(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)