Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#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();
}
Posted
Updated 5-Oct-15 22:25pm
v3
Comments
Andreas Gieriet 6-Oct-15 4:38am    
Is this the original formatting?
I generally refuse to read such carelessly formatted code.
If someone does not bother to present the code in at least half way readable form, I think he does not deserve that anyone spends time on it...
Make it easy to anyone to help you by at least format the program in a decent way.
Regards
Andi
Richard MacCutchan 6-Oct-15 8:55am    
It is very badly written (single letter variable names, no comments ...), and not indented. So I doubt if anyone will even try to read it. Best go back where you found it and ask the person that wrote it.

1 solution

Looks a not so robust (and bit ugly) program for computing various operations with matrices.
 
Share this answer
 
Comments
Maciej Los 7-Oct-15 2:08am    
Short and to the point!
CPallini 7-Oct-15 4:43am    
Too good, man, too good. :-)
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900