Friday, 27 September 2013

Write the Classes of Matrix in C++


You will implement a matrix calculator. Design a class matrix which implements the basic functionality of mathematical objects Matrix. Use static two dimensional array of floats to store the elements of the matrix. You can assume that no matrix would have dimensions more than 10x10. Define the following functions for the users of matrix calculator

These are the classes of Matrix and we have define every thing in function and make easier for C++ students. We have define default constructor with name of "matrix()" and over loaded constructor.

#include<iostream>

using  namespace std;
class matrix{
private:
int row;
int col;
int a[10][10];
public:
matrix();
matrix(int r,int c);
matrix add(matrix m);
matrix mul(matrix m);
matrix sub(matrix m);
bool notequal(matrix m);
void mulfloat(int i,int,float j);
void trans();
void inverse();
void input();
void output();
void swape(int i,int j);
};
matrix::matrix()
{
row=10;
col=10;


}
matrix::matrix(int r,int c)
{
row=r;
col=c;


}
matrix matrix::add(matrix m)
{ matrix d;
int i,j,c;
if(row==m.row&&col==m.col)
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
d.a[i][j]=a[i][j]+m.a[i][j];

}
}
d.row=row;
d.col=col;
}

return d;



}
matrix matrix::sub(matrix m)
{ matrix d;
int i,j,c;
if(row==m.row&&col==m.col)
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
d.a[i][j]=a[i][j]-m.a[i][j];

}
}
d.row=row;
d.col=col;
}
return d;

}
matrix matrix::mul(matrix m)
{
matrix d;

for(int i=0;i<row;i++)
{
for(int j=0;j<m.col;j++)
{
d.a[i][j]=0;
        for(int k=0;k<row;k++)
{
d.a[i][j]=d.a[i][j]+(a[i][k] * m.a[k][j]);
}

}

}
d.row=row;
d.col=m.col;

return d;
}
void matrix::trans()
{


int temp;
int i;
int j=0;
for(i=0;i<row;i++)
{
for(j=i;j<col;j++)
{
temp=a[i][j];
              a[i][j]=a[j][i];
a[j][i]=temp;


}
}

}
bool matrix::notequal(matrix m)
{

if(col!=m.row)
return false;
else
return true;



}
void matrix::swape(int i,int j)
{
int temp,k;
for(k=0;k<col;k++)
{
temp=a[i][k];
a[i][k]=a[j][k];
a[j][k]=temp;
}
}
void matrix::mulfloat(int i,int j,float f)
{
int k;
for(k=0;k<col;k++)
a[i][k]*=f;
for(k=0;k<col;k++)
a[j][k]+=a[i][k];
}
void matrix::input()
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
cin>>a[i][j];
}
}
void matrix::output()
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)

cout<<a[i][j]<<"\t";

cout<<"\n";
}
}
void main()
{
int k,l,x;
cout<<"\t*********WelCOME TO THE MATRIX CALCULATOR***********\n\n\n";
cout<<"Enter the Size of Rows and COl\n\n ";
cin>>k>>l;
matrix a(k,k),b(k,l),c;
cout<<"Press the following keyword which is shown in the screen :\n1 Addition\n2 subtraction\n3 Multiplication\n4 Transpose\n5 Inverse\n\n";
cin>>x;
if(x==1)
{
cout<<"Enter the matrix A\n";
a.input();
cout<<"Enter the matrix B\n";
b.input();

cout<<"Matrix Addition\n";
c=a.add(b);
c.output();
}
   if(x==2)
{
cout<<"Enter the matrix A\n";
a.input();
cout<<"Enter the matrix B\n";
b.input();

cout<<"Matrix Subtraction\n";
c=a.sub(b);
c.output();
}
if(x==3)
{
cout<<"Enter the matrix A\n";
a.input();
cout<<"Enter the matrix B\n";
b.input();
cout<<"Matrix Multi\n";
c=a.mul(b);
c.output();
}
if(x==4)
{
cout<<"Enter the matrix A\n";
a.input();
cout<<"Enter the matrix B\n";
b.input();
cout<<"Given Matrix \n\n";

a.output();
a.trans();
cout<<"resultant Matrix:\n\n";
a.output();

}

system("pause");



}

No comments:

Post a Comment