The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Sunday, September 4, 2011

Various Matrix Operations


#include<conio.h>
#include<iostream.h>
#define max 3

class matrix
{
 private :
  int mx[max][max];
  int det;
  int tmx[max][max];
  float imx[max][max];
  int cf[max][max];
  int adj[max][max];
 public :
  void inp_dat();
  int find_mdet(int,int);
  void find_trns(int);
  void pro_dat();
};

int i,j,k;

void matrix :: inp_dat()
{
 for(i=1;i<=max;i++)
 {
  cout<<"\n Enter the elements for Row #"<<i<<" :";
  for(j=1;j<=max;j++)
   cin>>mx[i][j];
 }
 for(i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(j=1;j<=max;j++)
   cout<<" "<<mx[i][j];
 }
}

void matrix :: find_trns(int choice)
{
 for(int i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(int j=1;j<=max;j++)
  {
  if(choice == 1)
   {
   tmx[i][j] = mx[j][i];
   cout<<" "<<tmx[i][j];
   }
  else
   {
   adj[i][j] = cf[j][i];
   cout<<" "<<adj[i][j];
   }
  }
 }
}

int matrix :: find_mdet(int r,int c)
{
 int ar[4],res;
 k = 0;
 for(i=1;i<=max;i++)
 {
  for(j=1;j<=max;j++)
  {
   if(i != r && j != c)
   {
    ar[k] = mx[i][j];
    k++;
   }
  }
 }
 res = ar[0]*ar[3]-ar[1]*ar[2];
 return res;
}

void matrix :: pro_dat()
{
 int prod = 1;
 det = 0;
 for(i=1;i<=max;i++)
 {
  det += prod*mx[1][i]*find_mdet(1,i);
  prod *= -1;
 }
 cout<<"\n\n Its Determinant is : "<<det;
 cout<<"\n\n Its Transpose is : \n\n ";
 find_trns(1);
 cout<<"\n\n Its co-factor is : \n";
 for(i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(j=1;j<=max;j++)
  {
  cf[i][j] = prod*find_mdet(i,j);
  cout<<" "<<cf[i][j];
  prod *= -1;
  }
 }
 cout<<"\n\n Its Adjoint is : \n ";
 find_trns(2);
 if(det == 0)
  cout<<"\n\n It's Inverse doesn't exists as its Determinant is Zero.";
 else
 {
  cout<<"\n\nIts Inverse is : \n";
  for(i=1;i<=max;i++)
  {
   cout<<"\n Row #"<<i<<" :";
   for(j=1;j<=max;j++)
   {
   imx[i][j] = (float) adj[i][j]/det;
   cout<<" "<<imx[i][j];
   }
  }
 }
}

void main()
{
clrscr();
matrix mat;
mat.inp_dat();
mat.pro_dat();
getch();
}

No comments:

Post a Comment