Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c中打印出变量会更改变量的值_C_Matrix_Printf - Fatal编程技术网

在c中打印出变量会更改变量的值

在c中打印出变量会更改变量的值,c,matrix,printf,C,Matrix,Printf,我对一些c语言程序有一个奇怪的问题。我在一个矩阵中得到了一些错误的值,我在寻找行列式,所以我开始打印变量,但发现通过打印值,代码中的实际值发生了变化 我最终把它缩小到一个特定的printf语句——在下面的代码中突出显示。如果我注释掉这一行,那么我开始在我的确定计算中得到错误的值,但是通过打印出来,我得到了我期望的值 代码如下: #include <math.h> #include <stdio.h> #include <stdlib.h> #define NU

我对一些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);
}
#包括
#包括
#包括
#定义数字15
双行列式_计算(整数大小,双数组[数][数]);
int main(){
双数组[NUMBER][NUMBER],行列式_值;
整数大小;
数组[0][0]=1;
数组[0][1]=2;
数组[0][2]=3;
数组[1][0]=4;
数组[1][1]=5;
数组[1][2]=6;
数组[2][0]=7;
数组[2][1]=8;
数组[2][2]=10;
尺寸=3;
行列式_值=行列式_计算(大小、数组);
printf(“\n\n\n\n\n确定值为%lf\n\n\n\n\n”,确定值);
返回0;
}
双行列式_计算(整数大小,双数组[数字][数字])
{
双行列式矩阵[数][数],行列式值;
整数x,y,计数=0,符号=1,i,j;
/*初始化数组*/

对于(i=0;i变量行列式_值未初始化为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;
}

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

您的变量行列式_值未初始化为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;
}

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

为什么不提供一个自包含的程序,以便有人可以重新创建结果,然后调试它?帮助有人帮助你矩阵的行列式是-3,而不是-1。你的变量行列式值没有初始化。好的,我将发布一个测试用例。我们只能推测since声明其未定义的行为,但很可能声明常量字符串
“\n”
%lf”
行列式\u值的内存位置上移动,移动方式恰好是
0
,但在它指向恰好是
-4
的内存位置之前。为什么不提供一个自包含的程序,以便有人可以重新创建结果,然后调试它?帮助有人帮助您确定行列式该矩阵是-3,而不是-1。您的变量行列式_值未初始化。好的,我将用一个测试用例发布修订版。我们只能推测它的未定义行为,但最有可能声明常量字符串
“\n”
%lf”
行列式\u值的内存位置周围移动,移动方式恰好是
0
,但在它指向恰好是
-4
的内存位置之前。