Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Implement a Matrix class representing 2-dimensional matrix with the following functionalities i.e. operators + , - , * , == , += << , >> etc

Posted By: Alfonsine Miller     Category: C++ Programming     Views: 4107

Implement a Matrix class representing 2-dimensional matrix with the following functionalities.

1. operators + , - , * , == , += << , >>
2. Appropriate constructors for the new matrix.
3. Destructors for the matrix.

Code for Implement a Matrix class representing 2-dimensional matrix with the following functionalities i.e. operators + , - , * , == , += &lt;&lt; , &gt;&gt; etc in C++ Programming

    # include<iostream.h>
    # include<conio.h>
    # include<process.h>
    # include<ctype.h>
    # include<iomanip.h>

    class Matrix
    {
        float row,col;
        float **arr;
      public:
        Matrix();
        Matrix(float,float);
        Matrix(Matrix &);
//        ~Matrix();void inputDim(float,float);
        Matrix inverse();
        Matrix operator+(Matrix &);
        Matrix operator-(Matrix &);
        Matrix operator*(Matrix &);
        floatoperator==(Matrix &);
        voidoperator+=(Matrix &);
        friend voidoperator<<(ostream &,Matrix &);
        friend voidoperator>>(istream &,Matrix &);
    };

    Matrix::Matrix()
    {
        row=col=0;
        arr=NULL;
    }

    Matrix::Matrix(float row,float col)
    {
        this->row=row;
        this->col=col;
        arr=newfloat*[row];
        for(float i=0;i<row;i++)
            arr[i]=newfloat[col];
    }

    Matrix::Matrix(Matrix &t)
    {
        this->row=t.row;
        this->col=t.col;
        this->arr=newfloat*[t.row];
        for(float i=0;i<t.row;i++)
            this->arr[i]=newfloat[t.col];

        for(float j=0;j<t.row;j++)
            for(float k=0;k<col;k++)
                this->arr[j][k]=t.arr[j][k];
    }
/*
Matrix::~Matrix()
{
delete arr;
}
*/
Matrix Matrix::inverse() { float temp,tt; Matrix t,x; x=*this; t.inputDim(this->row,this->col); for(int i=0;i<this->row;i++) { for(int j=0;j<this->col;j++) { if(i==j) t.arr[i][j]=1; else t.arr[i][j]=0; } } for(i=0;i<t.row;i++) { temp=x.arr[i][i]; if(temp==0) { for(int j=0;j<t.col;j++) { tt=x.arr[i][j]; x.arr[i][j]=x.arr[i+1][j]; x.arr[i+1][j]=tt; tt=t.arr[i][j]; t.arr[i][j]=t.arr[i+1][j]; t.arr[i+1][j]=tt; } } //Step 1:for(int j=0;j<t.col;j++) { x.arr[i][j]/=temp; t.arr[i][j]/=temp; } cout<<endl<<"MATRIX A"<<endl; cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"<<endl; cout<<x; cout<<endl<<"MATRIX B"<<endl; cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"<<endl; cout<<t; getch(); //Step 2:for(int k=0;k<t.row;k++) { temp=x.arr[k][i]; if(k!=i) { for(j=0;j<t.col;j++) { t.arr[k][j]+=(t.arr[i][j]*(-temp)); x.arr[k][j]+=(x.arr[i][j]*(-temp)); } } } cout<<endl<<"MATRIX A"<<endl; cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"<<endl; cout<<x; cout<<endl<<"MATRIX B"<<endl; cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"<<endl; cout<<t; getch(); } return t; } void Matrix::inputDim(float row,float col) { this->row=row; this->col=col; this->arr=newfloat*[row]; for(float i=0;i<row;i++) this->arr[i]=newfloat[col]; } float Matrix::operator==(Matrix &t) { if(this->row!=t.row || this->col!=t.col) { cout<<endl<<"Comparison not possible"; getch(); // exit(1); } for(float i=0;i<this->row;i++) { for(float j=0;j<this->col;j++) { if(this->arr[i][j]!=t.arr[i][j]) return 0; //Unequal Matrix; } } return 1; //Equal Matrix; } Matrix Matrix::operator+(Matrix &m) { if(this->row==m.row && this->col==m.col) { Matrix t; t.inputDim(m.row,m.col); for(float i=0;i<m.row;i++) for(float j=0;j<m.col;j++) t.arr[i][j]=this->arr[i][j]+m.arr[i][j]; return t; } else { cout<<endl<<"Addition is not possible : "; getch(); // exit(1); } } void Matrix::operator+=(Matrix &m) { if(this->row==m.row && this->col==m.col) { for(float i=0;i<m.row;i++) for(float j=0;j<m.col;j++) this->arr[i][j]+=m.arr[i][j]; } else { cout<<endl<<"Addition is not possible : "; getch(); // exit(1); } } Matrix Matrix::operator-(Matrix &m) { if(this->row==m.row && this->col==m.col) { Matrix t; t.inputDim(m.row,m.col); for(float i=0;i<m.row;i++) for(float j=0;j<m.col;j++) t.arr[i][j]=this->arr[i][j]-m.arr[i][j]; return t; } else { cout<<endl<<"Subtraction is not possible : "; getch(); // exit(1); } } Matrix Matrix::operator*(Matrix &m) { if(this->col==m.row) { Matrix t; t.inputDim(this->row,m.col); for(float i=0;i<this->row;i++) { for(float j=0;j<m.col;j++) { t.arr[i][j]=0; for(float k=0;k<m.row;k++) t.arr[i][j]+=this->arr[i][k]*m.arr[k][j]; } } return t; } else { cout<<endl<<"Addition is not possible : "; getch(); // exit(1); } } voidoperator<<(ostream &mycout,Matrix &t) { for(float i=0;i<t.row;i++) { cout.width(4); cout.precision(2); for(float j=0;j<t.col;j++) mycout<<setw(5)<<t.arr[i][j]<<" "; mycout<<endl; } } voidoperator>>(istream &mycin,Matrix &t) { for(float i=0;i<t.row;i++) { for(float j=0;j<t.col;j++) { cout<<endl<<"Enter m["<<i+1<<"]["<<j+1<<"] : "; mycin>>t.arr[i][j]; } } } void main() { int r,c,choice,DataEntered=0; Matrix a,b,x; void getDimension(int &,int &); void showResult(Matrix &,Matrix &,Matrix &); void showResult1(Matrix &,Matrix &); char ch='Y'; do { clrscr(); gotoxy(20,6); cout<<"Matrix Operations"; gotoxy(20,7); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(20,8); cout<<"Create two new matricies : 1"; gotoxy(20,9); cout<<"Matrix addition : 2"; gotoxy(20,10); cout<<"Matrix addition using += : 3"; gotoxy(20,11); cout<<"Matrix subtraction : 4"; gotoxy(20,12); cout<<"Matrix multiplicaton : 5"; gotoxy(20,13); cout<<"Matrix Equality checking : 6"; gotoxy(20,14); cout<<"Show both Matrices : 7"; gotoxy(20,15); cout<<"Inverse Matrix : 8"; gotoxy(20,16); cout<<"Exit : 0"; gotoxy(20,17); cout<<"Select your choice : "; cin>>choice; clrscr(); /* if(!DataEntered && choice!=1 && choice !=0)
{
cout<<endl<<"Matrices are empty";
getch();
continue;
}
*/
switch(choice) { case 1: cout<<endl<<"Matrix-A Details"; cout<<endl<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; getDimension(r,c); a = Matrix(r,c); cin>>a; cout<<endl<<"Matrix-B Details"; cout<<endl<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; getDimension(r,c); b = Matrix(r,c); cin>>b; DataEntered=1; break; case 2: x=a+b; showResult(a,b,x); break; case 3: a+=b; showResult1(a,b); break; case 4: x=a-b; showResult(a,b,x); break; case 5: Matrix x=a*b; showResult(a,b,x); break; case 6: if(a==b) cout<<"Both matrix are equal"; else cout<<"Both matrix are unequal"; break; case 7: showResult1(a,b); break; case 8: cout<<endl<<"Matrix-A Details"; cout<<endl<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; getDimension(r,c); a = Matrix(r,c); cin>>a; b = a.inverse(); showResult1(a,b); case 0: exit(1); default: gotoxy(20,16); cout<<"Invalid selection"; } cout<<endl<<"continue (y/n) : "; cin>>ch; ch=toupper(ch); }while(ch=='Y'); getch(); } void getDimension(int &r,int &c) { cout<<endl<<"Enter row dimension : "; cin>>r; cout<<endl<<"Enter col dimension : "; cin>>c; } void showResult(Matrix &x,Matrix &y,Matrix &z) { clrscr(); cout<<endl<<"Matrix A"; cout<<endl<<"ÍÍÍÍÍÍÍÍ"<<endl; cout<<x; cout<<endl<<"Matrix B"; cout<<endl<<"ÍÍÍÍÍÍÍÍ"<<endl; cout<<y; cout<<endl<<"Result"; cout<<endl<<"ÍÍÍÍÍÍ"<<endl; cout<<z; } void showResult1(Matrix &x,Matrix &y) { clrscr(); cout<<endl<<"Matrix A"; cout<<endl<<"ÍÍÍÍÍÍÍÍ"<<endl; cout<<x; cout<<endl<<"Matrix B"; cout<<endl<<"ÍÍÍÍÍÍÍÍ"<<endl; cout<<y; } /* OUTPUT

Matrix Operations
ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
Create two new matricies : 1
Matrix addition : 2
Matrix addition using += : 3
Matrix subtraction : 4
Matrix multiplicaton : 5
Matrix Equality checking : 6
Show both Matrices : 7
Exit : 0
Select your choice : 5

Multiplication.

Matrix A
ÍÍÍÍÍÍÍÍ
1 2 3
4 5 6

Matrix B
ÍÍÍÍÍÍÍÍ
1 2
3 4
5 6

Result
ÍÍÍÍÍÍ
22 28
49 64

continue (y/n) :
*/
  
Share: 



Alfonsine Miller
Alfonsine Miller author of Implement a Matrix class representing 2-dimensional matrix with the following functionalities i.e. operators + , - , * , == , += << , >> etc is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!