Pages

Friday, September 23, 2016

C++ Program for Matrix Addition, Multiplication, Inverse and Transpose using Operator Overloading

Program-

#include<iostream>
using namespace std;

#define MAX 10
class Matrix
{
   int matrix[MAX][MAX],x,y;
 
public:
   void get();
   void put();
   Matrix operator+(Matrix);
   Matrix operator-(Matrix &m);
   Matrix operator*(Matrix);
   Matrix operator^(Matrix &m);
};
void Matrix::get()
{
   cin>>x>>y;
   cout<<"Enter the Matrix :\n";
   for(int i=0;i<x;i++)
      for(int j=0;j<y;j++)
         cin>>matrix[i][j];

}

void Matrix::put()
{
   for(int i=0;i<x;i++)
   {
      cout<<"\n\t";
      for(int j=0;j<y;j++)
         cout<<matrix[i][j]<<" ";
   }
   cout<<endl;
}

Matrix Matrix::operator+(Matrix b)
{
   Matrix temp;
   if((x!=b.x)||(y!=b.y))    //a.x!=b.x
   {
      cout<<"\n\tMatrix Addition is not possible the result is incorrect\n\n";
      temp.x=0;
      temp.y=0;
   }
   else
   {
      temp.x=x;
      temp.y=y;
   }
   for(int i=0;i<x;i++)
      for(int j=0;j<y;j++)
         temp.matrix[i][j]=matrix[i][j]+b.matrix[i][j];
   return temp;
}

Matrix Matrix::operator -(Matrix &m)
{
 Matrix r,r2;
 int ch;
 float determinant=0;

  cout<<"\n1. 2*2 matrix ";
  cout<<"\n2. 3*3 matrix ";
  cout<<"\n enter choice ";
  cin>>ch;
 if(ch==1)  
 {
  cout<<"To Find Inverse Of Matrix \n";
  cout<<"Enter elements of 2*2 matrix:\n";
  for(int i=0;i<2;i++)
{
        for(int j=0;j<2;j++)
          {
              cin>>r2.matrix[i][j];
            }
  }

  //swap the a and d
  float temp=r2.matrix[0][0];
  r2.matrix[0][0]=r2.matrix[1][1];
  r2.matrix[1][1]=temp;

//take determinant
  float d1=r2.matrix[0][0]*r2.matrix[1][1];
  float d2=r2.matrix[0][1]*r2.matrix[1][0];

  //put negtive in front of b and c
  r2.matrix[0][1]=r2.matrix[0][1]-(r2.matrix[0][1]*2);
  r2.matrix[1][0]=r2.matrix[1][0]-(r2.matrix[1][0]*2);

  for(int i=0;i<2;i++)
  {
          for(int j=0;j<2;j++)
         {
              cout<<(r2.matrix[i][j]/(d1-d2))<<"  ";
         }
 cout<<"\n";
}

   r2.x=2;
   r2.y=2;
   return r2;
}
else          
{
 cout<<"To Find Inverse Of Matrix \n";
 cout<<"Enter elements of 3x3 matrix:\n";
 for(int i=0;i<3;i++)
 {
    for(int j=0;j<3;j++)
    {
       cin>>r.matrix[i][j];
    }
 }
 cout<<"\nThe entered matrix is:\n";
 for(int i=0;i<3;i++)
 {
   for(int j=0;j<3;j++)
   {
     cout<<r.matrix[i][j]<<"  ";
    }
  cout<<"\n";
 }
 for(int i=0;i<3;i++)
 {
      determinant = determinant + (r.matrix[0][i]*(r.matrix[1][(i+1)%3]*
      r.matrix[2][(i+2)%3] - r.matrix[1][(i+2)%3]*r.matrix[2][(i+1)%3]));
 }
 if(determinant==0)
 {
  cout<<"Inverse does not exist (Determinant=0).\n";
 }
 else
 {
  cout<<"\nInverse of matrix is: \n";
 }
 for(int i=0;i<3;i++)
 {
  for(int j=0;j<3;j++)
  {
   cout<<((r.matrix[(i+1)%3][(j+1)%3] *
    r.matrix[(i+2)%3][(j+2)%3]) - (r.matrix[(i+1)%3][(j+2)%3]*
     r.matrix[(i+2)%3][(j+1)%3]))/ determinant<<"\t";
  }
  cout<<"\n";
  }
  r.x=3;
   r.y=3;
   return r;
  }
}

Matrix Matrix::operator*(Matrix b)
{
   Matrix r;
   if((x!=b.y)||(y!=b.x))
   {
      cout<<"\n\tMatrix 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<MAX;i++)
      for(int j=0;j<MAX;j++)
         r.matrix[i][j]=0;
   for(int i=0;i<x;i++)
      for(int j=0;j<b.y;j++)
         for(int k=0;(k<y)||(k<b.x);k++)
            r.matrix[i][j]+=matrix[i][k]*b.matrix[k][j];

   return r;
}

Matrix Matrix::operator ^(Matrix &m)
{
   Matrix r;
   for(int i=0;i<x;i++)
      for(int j=0;j<y;j++)
         r.matrix[i][j]=matrix[j][i];
   r.x=x;
   r.y=y;
 
   return r;
}

int main()
{
   char option;
   Matrix a,b,c;
   int temp=1;
   while(temp)
   {
      cout<<"\tSelect Option\n\n1.Matrix Addition\n2.Matrix Inverse\n3.Matrix Multiplication\n4.Matrix Transponse\n5.Exit\n";
      cin>>option;
      switch(option)
      {
        case '1':
         cout<<"\n\tMatrix Addition\n";
         cout<<"Enter the order of first Matrix :\n";
         a.get();
         cout<<"Enter the order of second Matrix :\n";
         b.get();
         c=a+b;
         cout<<"The Addition is:\n";
         c.put();
         break;
        case'2':
         c=a-a;  
         break;
        case'3':
         cout<<"\n\tMatrix Multpication\n";
         cout<<"Enter the order of first Matrix :\n";
         a.get();
         cout<<"Enter the order of second Matrix :\n";
         b.get();
         c=a*b;
         cout<<"The Multiplication is:\n";
         c.put();
         break;
        case'4':
         cout<<"\n\tMatrix Transpose\n";
         cout<<"Enter the order of Matrix :\n";
         a.get();
         c=a^a;
         
         cout<<"The Transpose is:\n";
         c.put();
         break;
         case'5':
         cout<<"\n\tPress any key to exit\n";
         temp=0;
         break;
         default:
         cout<<"\n\tEnter a valid option\n";
      }
     
   }
   return 0;
}

Output-

        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
1

        Matrix Addition
Enter the order of first Matrix :
2 2
Enter the Matrix :
1 2
3 4
Enter the order of second Matrix :
2 2
Enter the Matrix :
1 2
3 4
The Addition is:

        2 4
        6 8
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
2

1. 2*2 matrix
2. 3*3 matrix
 enter choice 1
To Find Inverse Of Matrix
Enter elements of 2*2 matrix:
4 7
2 6
0.6  -0.7
-0.2  0.4
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
2

1. 2*2 matrix
2. 3*3 matrix
 enter choice 2
To Find Inverse Of Matrix
Enter elements of 3x3 matrix:
1 2 3
0 1 4
5 6 0

The entered matrix is:
1  2  3
0  1  4
5  6  0

Inverse of matrix is:
-24     20      -5
18      -15     4
5       -4      1
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
3

        Matrix Multpication
Enter the order of first Matrix :
2
2
Enter the Matrix :
1 2
3 4
Enter the order of second Matrix :
2 2
Enter the Matrix :
1 2
3 4
The Multiplication is:

        7 10
        15 22
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
4

        Matrix Transpose
Enter the order of Matrix :
3 3
Enter the Matrix :
1 2 3
4 5 6
7 8 9
The Transpose is:

        1 4 7
        2 5 8
        3 6 9
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
5

        Press any key to exit

--------------------------------
Process exited after 150.3 seconds with return value 0
Press any key to continue . . .

No comments:

Post a Comment