2014-03-13 14 views
0

這是矩陣行列式的C代碼,但它給出了編譯錯誤。在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. 

我認爲錯誤是在2-d陣列的傳遞。當我將它作爲指針傳遞時,它會發出警告但沒有錯誤,但它不會給出正確的結果,因爲它始終將行列式作爲零。所以我猜這個數組並沒有被傳遞,當我在函數行列式中打印它時,它也不打印。 請幫助,因爲我在我的項目中被卡住了。

+0

[2D數組作爲參數的函數]的可能重複(http://stackoverflow.com/questions/12652598/2d-array-as-argument-to-function) – Mauren

回答

0

可以在C99聲明數組動態像這樣(變長數組,指出了haccks),但不是在早期版本:

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]; 
+0

是的,你可以。這被稱爲可變長度數組。 – haccks

+0

編輯,謝謝指出! – brokenfoot

0

在您的代碼中,

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

此處arr是一個靜態內存分配的二維數組,因此您不能在運行時讀取m並像這樣聲明arr。
所以如果你想要動態地定義陣列然後在聲明函數的原型作爲

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

使用動態存儲器分配的方法,如在malloc() C.

+0

你的回答是錯誤的。閱讀有關可變長度數組。 – haccks

0

然後編譯器將其解釋爲

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

即,您的函數期望第一個參數的類型爲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); 
0

宣告以明確的界限float b[m][m]您的陣列;編譯器不知道float b[][]中的空邊界(空邊界只適用於一維數組,因爲其他答案中解釋了原因)。

所以,你的決定作用應該是這樣的:

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

還有其他的方法可以讓你的代碼的工作,但我認爲這種方式是最接近於你已經擁有。

+0

我試過這個,但沒有工作,並給出了一個更多的錯誤,'米'未申報?你能告訴別的解決辦法嗎? – user2696258

0

我同意你不能用這種語法數組初始化的移動,U也可以使用

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

,然後訪問你的元素: -

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

希望它能幫助:)