December 22, 2011

Matrix Inverse in CPP


#include
#include
int main(){

  int a[3][3],i,j;
  float determinant=0;
  clrscr();
  cout<<"Enter the 9 elements of matrix: ";
  for(i=0;i<3 br="" i="">      for(j=0;j<3 br="" j="">       cin>>a[i][j];

  cout<<"\nThe matrix is\n";
  for(i=0;i<3 br="" i="">      cout<<"\n";
      for(j=0;j<3 br="" j="">       cout<<"\t"<  }

  for(i=0;i<3 br="" i="">      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

   cout<<"\nInverse of matrix is: \n\n";
   for(i=0;i<3 br="" i="">   cout<<" ";
      for(j=0;j<3 br="" j="">       cout<<" "<<((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant;
       cout<<"\n";
   }

   return 0;
}


Output:

No comments:

Post a Comment