Tuesday, February 10, 2015

Matrix Inverse

/* Write a program to find the inverse of a matrix */ #include<stdio.h> int main() { int a[3][3], i, j; float determinant = 0; printf("Enter the 9 elements of matrix:\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d", &a[i][j]); printf("\nThe matrix is\n"); for(i=0; i<3; i++) { printf("\n"); for(j=0; j<3; j++) printf("%d\t", a[i][j]); } for(i=0; i<3; 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])); printf("\n\nInverse of matrix is: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { int t1 = (a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]); int t2 = (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]); int t = (t1 - t2); printf("%+.2f\t", t/determinant); } printf("\n"); } getch(); return 0; }

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar