/* Program of matrix using class */
#include<iostream>
using namespace std;
class matrix
{
int a[100][100],r,c;
public:
matrix(){}
matrix(int m,int n)
{
r=m;c=n;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=0;
}
void read();
void show();
friend matrix addmatrix(matrix,matrix);
friend matrix multiplication(matrix,matrix);
friend matrix transpose(matrix);
};
void matrix::read()
{
cout<<"\nEnter row number=";
cin>>r;
cout<<"\nEnter column number=";
cin>>c;
cout<<"\nEnter the elements of the matrix"<<endl;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>a[i][j];
}
void matrix:: show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
matrix addmatrix(matrix x,matrix y)
{
matrix temp(x.r,x.c);
if(x.r!=y.r || x.c!=y.c)
cout<<"\nCannot added";
else
{
for(int i=0;i<x.r;i++)
{
for(int j=0;j<x.c;j++)
{
temp.a[i][j]=temp.a[i][j]+x.a[i][j]+y.a[i][j];
}
}
}
return temp;
}
matrix multiplication(matrix x,matrix y)
{
matrix temp(x.r,y.c);
if(x.r!=y.c)
cout<<"\nCannot multiply";
else
{
for(int i=0;i<x.r;i++)
{
for(int j=0;j<y.c;j++)
{
for(int k=0;k<temp.c;k++)
{
temp.a[i][j]=temp.a[i][j]+x.a[i][k]*y.a[k][j];
}
}
}
}
return temp;
}
matrix transpose(matrix x)
{
matrix temp(x.c,x.r);
for(int i=0;i<temp.r;i++)
for(int j=0;j<temp.c;j++)
temp.a[i][j]=x.a[j][i];
return temp;
}
main()
{
matrix m1,m2,sum,mul,tran;
m1.read();
m2.read();
cout<<"\nThe 1st matrix "<<endl;
m1.show();
cout<<"\nThe 2st matrix "<<endl;
m2.show();
sum=addmatrix(m1,m2);
cout<<"\nThe sum matrix "<<endl;
sum.show();
mul=multiplication(m1,m2);
cout<<"\nThe multiplication matrix "<<endl;
mul.show();
tran=transpose(m1);
cout<<"\nThe transpose matrix "<<endl;
tran.show();
return 0;
}
#include<iostream>
using namespace std;
class matrix
{
int a[100][100],r,c;
public:
matrix(){}
matrix(int m,int n)
{
r=m;c=n;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=0;
}
void read();
void show();
friend matrix addmatrix(matrix,matrix);
friend matrix multiplication(matrix,matrix);
friend matrix transpose(matrix);
};
void matrix::read()
{
cout<<"\nEnter row number=";
cin>>r;
cout<<"\nEnter column number=";
cin>>c;
cout<<"\nEnter the elements of the matrix"<<endl;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>a[i][j];
}
void matrix:: show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
matrix addmatrix(matrix x,matrix y)
{
matrix temp(x.r,x.c);
if(x.r!=y.r || x.c!=y.c)
cout<<"\nCannot added";
else
{
for(int i=0;i<x.r;i++)
{
for(int j=0;j<x.c;j++)
{
temp.a[i][j]=temp.a[i][j]+x.a[i][j]+y.a[i][j];
}
}
}
return temp;
}
matrix multiplication(matrix x,matrix y)
{
matrix temp(x.r,y.c);
if(x.r!=y.c)
cout<<"\nCannot multiply";
else
{
for(int i=0;i<x.r;i++)
{
for(int j=0;j<y.c;j++)
{
for(int k=0;k<temp.c;k++)
{
temp.a[i][j]=temp.a[i][j]+x.a[i][k]*y.a[k][j];
}
}
}
}
return temp;
}
matrix transpose(matrix x)
{
matrix temp(x.c,x.r);
for(int i=0;i<temp.r;i++)
for(int j=0;j<temp.c;j++)
temp.a[i][j]=x.a[j][i];
return temp;
}
main()
{
matrix m1,m2,sum,mul,tran;
m1.read();
m2.read();
cout<<"\nThe 1st matrix "<<endl;
m1.show();
cout<<"\nThe 2st matrix "<<endl;
m2.show();
sum=addmatrix(m1,m2);
cout<<"\nThe sum matrix "<<endl;
sum.show();
mul=multiplication(m1,m2);
cout<<"\nThe multiplication matrix "<<endl;
mul.show();
tran=transpose(m1);
cout<<"\nThe transpose matrix "<<endl;
tran.show();
return 0;
}
No comments:
Post a Comment