0

这是矩阵行列式的 C 代码,但它给出了编译错误。

代码是:

#include<stdio.h>
#include<math.h>
int m;

float determinant(float b[][]);

int main(void)
{
int i,j,m;
printf("enter a no: ");
scanf("%d",&m);
//printf("%d",m);
float arr[m][m];
for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {
        scanf("%f",&arr[i][j]);
        //printf("%f",arr[i][j]);
    }
}


for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {
        printf("%f ",arr[i][j]);
    }

    printf("\n");
}



float det = determinant(arr);

printf("Determinant= %f ", det);




}

float determinant(float b[][])
{
int i,j;
int p;
float sum = 0;
float c[m][m];

for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {
        printf("%f ",b[i][j]);
    }

    printf("\n");
}



if(m==2)
{
    printf("Determinant for m=2");
    sum = b[0][0]*b[1][1] - b[0][1]*b[1][0];
    return sum;
}

for(p=0;p<m;p++)
{
    int h = 0,k = 0;
    for(i=1;i<m;i++)
    {
        for( j=0;j<m;j++)
        {
            if(j==p)
                continue;
            c[h][k] = b[i][j];
            k++;
            if(k == m-1)
            {   
                h++;
                k = 0;
            }
        }
    }

    m=m-1;
    sum = sum + b[0][p]*pow(-1,p) * determinant(c);

}




return sum;
}

编译错误是:

det.c:5:25: error: array type has incomplete element type
det.c: In function ‘main’:
det.c:36:2: error: type of formal parameter 1 is incomplete
det.c: At top level:
det.c:45:25: error: array type has incomplete element type
det.c: In function ‘determinant’:
det.c:91:3: error: type of formal parameter 1 is incomplete
det.c:99: confused by earlier errors, bailing out
Preprocessed source stored into /tmp/cc1Kp9KD.out file, please attach this to your bug report.

我认为错误在于二维数组的传递。当我将它作为指针传递时,它会给出警告但没有错误,但它不会给出正确的结果,因为总是将行列式设为零。所以我猜这个数组并没有被传递,当我在函数行列式中打印它时它也不会打印。请帮忙,因为我在我的项目中被困住了。

4

5 回答 5

0

当您将函数的原型声明为

int foo(int arr[], int n);

然后编译器将其解释为

int foo(int (*arr), int n);  // and that's why you can omit the first dimension!

即,您的函数期望第一个参数是 type int *。同样,当参数为多维数组时为

int foo(int arr[][col], int n); // Only first dimension can be omitted. You need to specify the second dimension.   

然后编译器将其解释为

int foo(int (*arr)[col], int n);   

即,您的函数期望第一个参数是类型int (*)[col](指向int数组的指针)。
由于当传递给函数(在大多数情况下)时,数组名称衰减为指向其第一个元素的指针,在您的情况下arr将衰减为指向其第一个元素的指针,即第一行。因此它的类型将变为float (*)[m]. 如果您将其声明为,它与您的函数参数兼容

float determinant(int m, float b[][m]);     

电话应该是

float det = determinant(m, arr);
于 2014-03-13T16:35:43.870 回答
0

您可以在 C99 中像这样动态声明数组(变长数组,hacks 指出),但在早期版本中不能:

float arr[m][m];

因此,如果它给您带来麻烦,那么请为它声明一个指针和 malloc 内存:

float* arr = malloc(sizeof(float)*m*m);

此外,该定义将不起作用(在任何一种情况下):

float determinant(float b[][]);

您需要在传递给函数的数组中定义列。

如果您像我展示的那样声明并分配指针,那么您可以在函数中传递一个指针:

float determinant(float *b, int size); //here size is your row dimension, in this case equal to m

在函数内部,访问您的元素,例如:

*(b + size*i + j) = value // equivalent to b[i][j];
于 2014-03-13T16:34:07.133 回答
0

在你的代码中,

scanf("%d",&m);
//printf("%d",m);
float arr[m][m];

这里 arr 是一个具有静态内存分配的二维数组,因此您不能在运行时读取 m 并像这样声明 arr 。
因此,如果您想动态定义数组,请使用malloc()C 中的动态内存分配方法。

于 2014-03-13T16:34:21.927 回答
0

用明确的界限声明你的数组float b[m][m];编译器不理解空边界float b[][](空边界仅适用于一维数组,原因在其他答案中解释)。

所以你的行列式函数应该是这样的:

float determinant(int m, float b[m][m])
{
    ...
}

还有其他方法可以使您的代码正常工作,但我认为这种方法最接近您已有的方法。

于 2014-03-13T17:01:15.540 回答
0

我同意你不能使用这种数组初始化语法,你可以使用

int *p=(int*)calloc(n*n,sizeof(float));

然后访问您的元素: -

*(p+j+n*i);//for p[i][j] element

希望能帮助到你 :)

于 2016-12-04T20:40:44.337 回答