Advertisement
Shailrshah

Inverse of a 3x3 Matrix

May 26th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. /*Printing Inverse of a 3x3 Matrix
  2. 1. Matrix Mirror
  3. 2. Determinant
  4. 3. Cofactor
  5. 4. Adjoint
  6. 5. Divide by Determinant*/
  7.  
  8. #include <stdio.h>
  9. int main()
  10. {
  11.         int i, j, k;
  12.         float det,temp;
  13.         float a[3][3],b[3][3];
  14.         printf("Enter 9 elements in the 3x3 matrix:-\n");
  15.         for(i=0;i<3;i++)
  16.         {
  17.                 for(j=0;j<3;j++)
  18.                         scanf("%f",&a[i][j]);
  19.                 printf("\n");
  20.         }
  21.         //Matrix Mirror
  22.         b[0][0] =  a[1][1]*a[2][2]-a[1][2]*a[2][1];
  23.         b[0][1] =  a[1][0]*a[2][2]-a[2][0]*a[1][2];
  24.         b[0][2] =  a[1][0]*a[2][1]-a[2][0]*a[1][1];
  25.         b[1][0] =  a[0][1]*a[2][2]-a[2][1]*a[0][2];
  26.         b[1][1] =  a[0][0]*a[2][2]-a[0][2]*a[2][0];
  27.         b[1][2] =  a[0][0]*a[2][1]-a[0][1]*a[2][0];
  28.         b[2][0] =  a[0][1]*a[1][2]-a[0][2]*a[1][1];
  29.         b[2][1] =  a[0][0]*a[1][2]-a[0][2]*a[1][0];
  30.         b[2][2] =  a[0][0]*a[1][1]-a[0][1]*a[1][0];
  31.         //Determinant
  32.         det = a[0][0]*b[0][0]-a[0][1]*b[0][1]+a[0][2]*b[0][2];
  33.         //Cofactor matrix
  34.         b[0][1] = -b[0][1];
  35.         b[1][0] = -b[1][0];
  36.         b[2][1] = -b[2][1];
  37.         b[1][2] = -b[1][2];
  38.         //Adjoint
  39.         temp = b[0][1];
  40.         b[0][1] = b[1][0];
  41.         b[1][0] = temp;
  42.         temp = b[0][2];
  43.         b[0][2] = b[2][0];
  44.         b[2][0] = temp;
  45.         temp = b[1][2];
  46.         b[1][2] = b[2][1];
  47.         b[2][1] = temp;
  48.         //Printing elemets divided by determinant
  49.         for(i=0;i<3;i++)
  50.         {
  51.                 for(j=0;j<3;j++)
  52.                         printf("\t%.1f\t",b[i][j]/det);
  53.                 printf("\n");
  54.         }
  55.         fseek(stdin,0,SEEK_END);
  56.         getchar();
  57.         return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement