#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define max 100
void slice(int y,int n,int t_arr[max][max],int arr[max][max])
{
int i,j,j_d;
for(i=1;i<n;i++)
{
for(j=0,j_d=0;j<n;j++)
{
if(j!=y)
{
t_arr[i-1][j_d]=arr[i][j];
j_d++;
}
}
}
}
int determinant(int arr[max][max],int n)
{
int t_arr[max][max],i,sum=0;
if(n==1)
return arr[0][0];
else if(n==2)
return (arr[0][0]*arr[1][1]-arr[1][0]*arr[0][1]);
else
{
for(i=0;i<n;i++)
{
slice(i,n,t_arr,arr);
sum=sum+(pow(-1,i)*arr[0][i]*determinant(t_arr,n-1));
}
return sum;
}
}
int main()
{
int arr[max][max],n,i,j;
printf("Enter the size of the determinant\n");
scanf("%d",&n);
printf("Enter the contents of the determinant\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
if(n>0)
printf("\nDeterminant Value=%d",determinant(arr,n));
else
printf("Invalid Size");
system("pause");
}
0 comments:
Post a Comment