I am having trouble making this recursive C function work for square matrices of size greater than 3 rows and columns.
[code=c]
#include<stdio. h>
#include<conio. h>
#include<math.h >
int M[100];
float DET(float A[][100],int dim)
{
int i=100,j,k,l;
static int det=0;
static int DIM=dim;
if(dim>2)
for(i=0;i<DIM;i ++)
{
if(i==0&&DIM==d im)for(j=0;j<10 0;j++)M[j]=100;
for(j=DIM-1;j>=2;j--)
if(M[j]==i)continue;
M[dim-1]=i;
det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1);
}
if(i==DIM-1)M[dim-1]=100;
if(dim==2)
{
for(i=0;i<DIM;i ++)
{
k=0;
for(j=DIM-1;j>=2;j--)
if(i!=M[j])k++;
if(k==DIM-2)break;
}
for(j=0;j<DIM;j ++)
{
k=0;
for(l=DIM-1;l>=2;l--)
if(j!=M[l])k++;
if(k==DIM-2&&j!=i)break ;
}
return(A[0][i]*A[1][j]-A[0][j]*A[1][i]);
}
if(dim==1)retur n(A[0][0]);
return(det);
}
void main()
{
float A[100][100];
int n,k,l;
puts("Input size of square matrix:");
scanf("%d",&n);
puts("Input square matrix:");
for(k=0;k<n;k++ )
{
printf("\n");
for(l=0;l<n;l++ )
{
printf("Input a%d%d: ",k+1,l+1);
scanf("%f",&A[k][l]);
}
}
printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n) );
getch();
}
[/code]
If anyone can tell me where I went wrong in writing the program I would be grateful.
P.S.:Here are some points to make the program clearer:
_This program works by using method of minor.
_M[100] is an array of integers used to store in decreasing order of its elements which column in the matrix does not belong to the minor associated with the element chosen on a certain row.
_In the function, dim represents wich row is being used, while i(in the first for loop) represents which column is being used.
[code=c]
#include<stdio. h>
#include<conio. h>
#include<math.h >
int M[100];
float DET(float A[][100],int dim)
{
int i=100,j,k,l;
static int det=0;
static int DIM=dim;
if(dim>2)
for(i=0;i<DIM;i ++)
{
if(i==0&&DIM==d im)for(j=0;j<10 0;j++)M[j]=100;
for(j=DIM-1;j>=2;j--)
if(M[j]==i)continue;
M[dim-1]=i;
det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1);
}
if(i==DIM-1)M[dim-1]=100;
if(dim==2)
{
for(i=0;i<DIM;i ++)
{
k=0;
for(j=DIM-1;j>=2;j--)
if(i!=M[j])k++;
if(k==DIM-2)break;
}
for(j=0;j<DIM;j ++)
{
k=0;
for(l=DIM-1;l>=2;l--)
if(j!=M[l])k++;
if(k==DIM-2&&j!=i)break ;
}
return(A[0][i]*A[1][j]-A[0][j]*A[1][i]);
}
if(dim==1)retur n(A[0][0]);
return(det);
}
void main()
{
float A[100][100];
int n,k,l;
puts("Input size of square matrix:");
scanf("%d",&n);
puts("Input square matrix:");
for(k=0;k<n;k++ )
{
printf("\n");
for(l=0;l<n;l++ )
{
printf("Input a%d%d: ",k+1,l+1);
scanf("%f",&A[k][l]);
}
}
printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n) );
getch();
}
[/code]
If anyone can tell me where I went wrong in writing the program I would be grateful.
P.S.:Here are some points to make the program clearer:
_This program works by using method of minor.
_M[100] is an array of integers used to store in decreasing order of its elements which column in the matrix does not belong to the minor associated with the element chosen on a certain row.
_In the function, dim represents wich row is being used, while i(in the first for loop) represents which column is being used.
Comment