ここでいくつかのcプログラムに奇妙な問題があります。行列式を見つけていたマトリックスで間違った値を取得していたので、変数の出力を開始しましたが、値を出力することでコード内の実際の値が変更されていることがわかりました。
printf私は最終的にそれを 1 つの特定のステートメントに絞り込みました- 以下のコードで強調表示されています。この行をコメントアウトすると、決定的な計算で誤った値が得られ始めますが、それを出力することで、期待する値が得られます
以下のコード:
#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);
}
私がこのコードで行っていることを行うのが最も美しい (または最良の) 方法ではないことはわかっていますが、それは私が与えられたものであるため、大きな変更を加えることはできません。変数を出力すると実際に値が変わる理由を誰も説明できないと思いますか? または理想的にはしたくないので、それを修正する方法!!