2012-12-02 149 views
1

我在這裏有一些c程序存在奇怪的問題。我在矩陣中發現了一些錯誤的值,我發現了它的行列式,於是我開始打印變量 - 但是發現通過打印出值代碼中的實際值發生了變化。在c中打印變量會更改變量的值

我最終將其縮小到一個特定的printf聲明 - 在下面的代碼中突出顯示。如果我註釋掉這一行,然後我開始越來越不正確的值在我的行列式的計算,但通過它打印出來,我得到的數值超出我預期低於

代碼:

#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define NUMBER 15 

double determinant_calculation(int size, double array[NUMBER][NUMBER]); 

int main() { 
    double array[NUMBER][NUMBER], determinant_value; 
    int size; 

    array[0][0]=1; 
    array[0][1]=2; 
    array[0][2]=3; 
    array[1][0]=4; 
    array[1][1]=5; 
    array[1][2]=6; 
    array[2][0]=7; 
    array[2][1]=8; 
    array[2][2]=10; 

    size=3; 

    determinant_value=determinant_calculation(size, array); 
    printf("\n\n\n\n\n\nDeterminant value is %lf \n\n\n\n\n\n", determinant_value); 
    return 0; 
} 

double determinant_calculation(int size, double array[NUMBER][NUMBER]) 
{ 
    double determinant_matrix[NUMBER][NUMBER], determinant_value; 
    int x, y, count=0, sign=1, i, j; 


    /*initialises the array*/ 
    for (i=0; i<(NUMBER); i++) 
    { 
     for(j=0; j<(NUMBER); j++) 
     { 
      determinant_matrix[i][j]=0; 
     } 
    } 

    /*does the re-cursion method*/ 
    for (count=0; count<size; count++) 
    { 
     x=0; 
     y=0; 
     for (i=0; i<size; i++) 
     { 
      for(j=0; j<size; j++) 
      { 
       if (i!=0&&j!=count) 
       { 
        determinant_matrix[x][y]=array[i][j]; 
        if (y<(size-2)) { 
         y++; 
        } else { 
         y=0; 
         x++; 
        } 
       } 
      } 
     } 

     //commenting this for loop out changes the values of the code determinent prints -7 when commented out and -3 (expected) when included! 
     for (i=0; i<size; i++) { 
      for(j=0; j<size; j++){ 
       printf("%lf ", determinant_matrix[i][j]); 
      } 
      printf("\n"); 
     } 

     if(size>2) { 
      determinant_value+=sign*(array[0][count]*determinant_calculation(size-1 ,determinant_matrix)); 
     } else { 
      determinant_value+=sign*(array[0][count]*determinant_matrix[0][0]); 
     } 
     sign=-1*sign; 
    } 
    return (determinant_value); 
} 

我知道它不是最漂亮的(或者最好的方式)做我在做這個代碼的事情,但這是我得到的 - 所以不能做出巨大的改變。我不認爲有人可以解釋爲什麼打印出變量實際上可以更改這些值?或如何解決它,因爲理想情況下我不想!

+2

爲什麼不給一個自包含的程序,這樣有人可以重新創建結果,然後調試它?幫助別人幫你 – necromancer

+1

該矩陣的行列式是-3,而不是-1。 – interjay

+2

您的變量determinant_value未初始化。 – ylc

回答

1

您的變量determinant_value未初始化爲0,因此會導致問題。 這是測試用例的修訂版。

#include <stdio.h> 

#define NUMBER 3 

double determinant_calculation(int size, double array[NUMBER][NUMBER]) 
{ 
    double determinant_matrix[NUMBER][NUMBER], determinant_value = 0; 
    int x, y, count=0, sign=1, i, j; 

    /*initialises the array*/ 
    for (i=0; i<(NUMBER); i++) 
    { 
     for(j=0; j<(NUMBER); j++) 
     { 
      determinant_matrix[i][j]=0; 
     } 
    } 

    /*does the re-cursion method*/ 
    for (count=0; count<size; count++) 
    { 
     x=0; 
     y=0; 
     for (i=0; i<size; i++) 
     { 
      for(j=0; j<size; j++) 
      { 
       if (i!=0&&j!=count) 
       { 
        determinant_matrix[x][y]=array[i][j]; 
        if (y<(size-2)) { 
         y++; 
        } else { 
         y=0; 
         x++; 
        } 
       } 
      } 
     } 

     if(size>2) { 
      determinant_value+=sign*(array[0][count]*determinant_calculation(size-1,determinant_matrix)); 
     } else { 
      determinant_value+=sign*(array[0][count]*determinant_matrix[0][0]); 
     } 
     sign=-1*sign; 
    } 
    return (determinant_value); 
} 

int main() 
{ 
    int i, j; 
    double ans; 
    double array[NUMBER][NUMBER] = {{1,2,3},{4,5,6},{7,8,10}}; 

    ans = determinant_calculation(3, array); 

    printf("the matrix\n"); 
    for (i = 0; i < NUMBER; ++i) { 
     for (j = 0; j < NUMBER; ++j) { 
       printf("%lf ", array[i][j]); 
     } 
     printf("\n"); 
    } 
    printf("determinant : %lf\n", ans); 
    return 0; 
} 

和輸出是:

the matrix 
1.000000 2.000000 3.000000 
4.000000 5.000000 6.000000 
7.000000 8.000000 10.000000 
determinant : -3.000000 

但我不知道在評論你的第二個問題。