【发布时间】:2020-12-15 15:33:59
【问题描述】:
伙计们,我想计算任何矩阵 nxn 的行列式,我尝试过这种方式,但它一直向我显示 错误,例如:
56: error: subscripted value is neither array nor pointer nor vector
return((tab[0][0] * tab[1][1])-(tab[0][1] * tab[1][0]));*
代码:
#include <stdlib.h>
#include <math.h>
#define Tmax 100
int determinant (int dim, int tab);
int main(){
int tab[Tmax][Tmax];
int dim, i, j;
int det=0;
do{
printf("Enter the dimention of the matrix n x n /n");
scanf("%d", &dim);
} while (dim<0 || dim>Tmax);
for (i=0; i<dim; i++){
for (j=0; j<dim; j++){
printf("Enter the element in the %d row and %d column:\n", i+1, j+1);
scanf("%d",&tab[i][j]);
}
}
for (i=0; i<dim; i++){
for (j=0; j<dim; j++){
det = det + determinant(dim, tab[i][j]);
}
}
printf("%d", det);
return 0;
}
int determinant (int dim, int tab[][])
int i, j;
if (dim==1)
return tab[0][0];
else if (dim==2)
return((tab[0][0] * tab[1][1])-(tab[0][1] * tab[1][0]));
else
return (pow(-1 , i+1+j+1) * tab[0][j] * determinant(i+1,tab));
}
对此我有什么办法吗?还有有什么简单的方法吗?
注意:请不要破坏只是提示
【问题讨论】:
-
在
determinant中,tab是int,而不是二维数组。 -
C 不允许像
int tab[][]这样的声明,您必须指定除最右边之外的所有维度。 -
提示:不要使用
pow(-1, foo),这太过分了。使用(foo % 2 == 0) ? 1 : -1 -
这个原型:
int determinant (int dim, int tab);与函数定义不匹配:int determinant (int dim, int tab[][]) -
我的理解是你尝试实现cofactor方法。但是,这里的编码并不完全是这样
标签: c