/* * To find the determinant of a 3 X 3 matrix * @author-Karan Narain(karan@iitk.ac.in) * */ #include #include int main() { int A[3][3],i,j,det=0; printf("Enter the 3 X 3 matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&A[i][j]); /*The direct way to do this is to simply calculate the determinant using constant index values,i.e. * det=(A[0][0] * (A[1][1]*A[2][2]-A[2][1]*A[1][2])) - (A[0][1] * (A[1][0]*A[2][2]-A[2][0]*A[1][2])) + (A[0][2] * (A[1][0]*A[2][1]-A[2][0]*A[1][1])) * This program calculates the determinant using i and j as indices in a for loop,instead of the above method */ //i should denote the first row.so set i=0 i=0; for(j=0;j<3;j++) { if(j==0) { det=det+A[i][j]*(A[i+1][j+1]*A[i+2][j+2]-A[i+2][j+1]*A[i+1][j+2]); } else if(j==1) { det=det-A[i][j]*(A[i+1][j-1]*A[i+2][j+1]-A[i+2][j-1]*A[i+1][j+1]); } else { det=det+A[i][j]*(A[i+1][j-2]*A[i+2][j-1]-A[i+2][j-2]*A[i+1][j-1]); } } printf("The determinant is %d",det); return 0; }