Skip to content

Instantly share code, notes, and snippets.

@subhajitbn
Created September 16, 2011 20:18
Show Gist options
  • Save subhajitbn/1223039 to your computer and use it in GitHub Desktop.
Save subhajitbn/1223039 to your computer and use it in GitHub Desktop.
Calculate the value of a 3 x 3 determinant
#include<stdio.h>
#include<math.h>
int determinant(int[3][3]);
void create(int[3][3]);
void display(int[3][3]);
int main()
{
int mat[3][3];
int d;
printf("\n Enter elements of the matrix: ");
create(mat);
printf("\n The Matrix: \n");
display(mat);
d=determinant(mat);
printf("\n The determinant for given matrix: %d",d);
if(d==0)
printf("\n Matrix is singular.");
else
printf("\n Matrix is not singular.");
return 0;
}
/* Function used to create a matrix */
void create(int mat[3][3])
{
int i,j,element;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the element");
scanf("%d",&element);
mat[i][j]=element;
}
}
printf("\n\n");
}
/* Function used to display the contents of a matrix */
void display(int mat[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
printf("\n\n");
}
/* Function to calculate the determinant value of a 3x3 matrix */
int determinant(int mat[3][3])
{
int i,j,k;
int sum,p;
sum=0;
j=1;
k=2;
for(i=0;i<3;i++)
{
p=pow(-1,i);
if(i==2)
k=1;
sum=sum+p*(mat[0][i]*(mat[1][j]*mat[2][k]-mat[2][j]*mat[1][k]));
j=0;
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment