我有一个关于使用矩阵行列式的递归定义的问题,我们在其中传递一个矩阵结构,其中包含矩阵及其行数/列数,但每次我尝试调用它时都会出现分段错误:
int det(struct Matrix *A){
/* A recursive definition of the determinant.*/
//If it's a single element, return the element
if( A->row == 1 && A->col == 1) {
return A->mat[0][0];
}
if(A->squareQ == 0){
puts("Matrix isn't square.");
return -999999999;
}
int DET = 0;
int n = A->row;
//Initialize memory for the (n-1)x(n-1) cofactor matrices
int i,j;
struct Matrix *COF = malloc(sizeof(struct Matrix));
COF->row = n-1;
COF->col = n-1;
COF->squareQ = 1;
//Now, begin building cofactors
int I,J;
int k = 0, l = 0;
int temp;
for(I = 0; I < n; I++){
for(J = 0; J < n; J++){
for(i = 0; i < n; i++){
if( i == I ) continue;
for(j = 0; j < n; j++){
if( j == J ) continue;
temp = A->mat[i][j];
COF->mat[k][l] = temp;
l++;
}
l = 0;
k++;
}
k = 0;
DET += pow(-1,I+J)*det(COF);
}
}
free(COF);
return DET;
}
并且它位于COF-> mat [k] [l] =临界发生的临界值。如何将元素分配给COF并将其传递给递归调用?感谢