Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not invertible if its determinant is

#include<stdio.h>
 int main()
{
  int a[3][3], i, j;
  long determinant;
 //  9 elements of matrix is taken as input from test data
  for(i = 0 ; i < 3; i++)
      for(j = 0; j < 3; j++)
           scanf("%d", &a[i][j]);
int det;
/*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]);*/

for(i=0;i<3;i++)
      det = det + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

   if ( det == 0)
     printf("The given matrix is not invertible");
else
  printf("The given matrix is invertible");
}

2 comments:

  1. For example:
    Input:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Output:
    The given matrix is invertible

    ReplyDelete
  2. For example:
    Input:
    1
    0
    5
    2
    1
    6
    3
    4
    0
    Output:
    The given matrix is invertible

    ReplyDelete