Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Printing Inverse of a 3x3 Matrix
- 1. Matrix Mirror
- 2. Determinant
- 3. Cofactor
- 4. Adjoint
- 5. Divide by Determinant*/
- #include <stdio.h>
- int main()
- {
- int i, j, k;
- float det,temp;
- float a[3][3],b[3][3];
- printf("Enter 9 elements in the 3x3 matrix:-\n");
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- scanf("%f",&a[i][j]);
- printf("\n");
- }
- //Matrix Mirror
- b[0][0] = a[1][1]*a[2][2]-a[1][2]*a[2][1];
- b[0][1] = a[1][0]*a[2][2]-a[2][0]*a[1][2];
- b[0][2] = a[1][0]*a[2][1]-a[2][0]*a[1][1];
- b[1][0] = a[0][1]*a[2][2]-a[2][1]*a[0][2];
- b[1][1] = a[0][0]*a[2][2]-a[0][2]*a[2][0];
- b[1][2] = a[0][0]*a[2][1]-a[0][1]*a[2][0];
- b[2][0] = a[0][1]*a[1][2]-a[0][2]*a[1][1];
- b[2][1] = a[0][0]*a[1][2]-a[0][2]*a[1][0];
- b[2][2] = a[0][0]*a[1][1]-a[0][1]*a[1][0];
- //Determinant
- det = a[0][0]*b[0][0]-a[0][1]*b[0][1]+a[0][2]*b[0][2];
- //Cofactor matrix
- b[0][1] = -b[0][1];
- b[1][0] = -b[1][0];
- b[2][1] = -b[2][1];
- b[1][2] = -b[1][2];
- //Adjoint
- temp = b[0][1];
- b[0][1] = b[1][0];
- b[1][0] = temp;
- temp = b[0][2];
- b[0][2] = b[2][0];
- b[2][0] = temp;
- temp = b[1][2];
- b[1][2] = b[2][1];
- b[2][1] = temp;
- //Printing elemets divided by determinant
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- printf("\t%.1f\t",b[i][j]/det);
- printf("\n");
- }
- fseek(stdin,0,SEEK_END);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement