1

我在这里对一些 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);
}

我知道这不是用这段代码做我正在做的事情的最漂亮(或最好的方式),但这是我得到的——所以不能做出巨大的改变。我想没有人可以解释为什么打印出变量实际上可以改变值?或如何解决它,因为理想情况下我不想!

4

1 回答 1

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

但我不知道你在评论中的第二个问题。

于 2012-12-02T17:01:24.407 回答