首页 > 解决方案 > 为什么我在 C 中访问二维数组时得到错误的值?

问题描述

我有以下程序可以计算 NxN 矩阵的行列式,该程序本身可以工作,但是当我尝试在递归结束时计算二阶矩阵行列式时,访问 2d 2x2 数组元素会给我错误的值。

我正在使用递归来计算高阶矩阵的行列式,findDet() 是递归函数,它使用循环为给定矩阵和子矩阵中第一行的每个元素调用自身(如果它们的顺序)大于二。

/******************************************************************************
Program to find the determinant a matrix
*******************************************************************************/
#include <stdio.h>

#define N 3

long int findDet(int arr[][N], size_t order); // To find the determinant
void initNewArr(int newArr[][N-1], int prevArr[][N], size_t order, int exCol); // To create sub matrices

int main()
{
    int order = N;
    printf("This program is to find determinant of a given matrix.\n\
           \rNumber of row and column is going to be equal.\n");

    // printf("Enter the order of the matrix: ");
    // scanf("%d", &order);

    // Read the input matrix from the user
    int matrix[order][order];
    printf("Enter matrix elements...\n");
    for(int i = 0; i < order; i++) {
        for(int j = 0; j < order; j++) {
            printf("Enter matrix[%d][%d]: ", i+1, j+1);
            scanf("%d", &matrix[i][j]);
        }
    }

    // Print the matrix
    printf("New Matrix itself...\n");
    for(int i = 0; i < order; i++) {
        for(int j = 0; j < order; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }

    // Calling findDet() to calculate determinant and store it in "det"
    long int det = findDet(matrix, order);

    // Print the determinant
    printf("\nDeterminant of the matrix = %li", det);

    return 0;
}

long int findDet(int arr[][N], size_t order) {
    // if order is 2 calculate the determinant of 2nd order matrix
    if(order == 2) {
        int detS = ((arr[0][0]) * (arr[1][1])) - ((arr[0][1]) * (arr[1][0]));

        /*
            This following expression shows that accessed values are not correct
            for this reason, the calculated determinant of 2nd order matrix is 
            not correct.
            The whole problem is in this block of code
        */
        printf("=== arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0] = %d * %d - %d * %d\n", arr[0][0], arr[1][1], arr[0][1], arr[1][0]);
        printf("Result of above expression === %d\n", detS);
        return detS;
    } // if order is higher then continue to break down matrix to 2nd order step by step
    long int det = 0;

    /*
        The following for loop is for multiplying each element of  
        first row by determinant of sub-matrix,  
        which in case of below 3x3 matrix:  
        2   -3   1    
        2    0  -1  its determinant = 2 * (det of sub-matrix1) - (-3) * (det of sub-matrix2) + 1 * (det of sub-matrix3)  
        1    4   5  
        sub-matrices are:  
        >>> sub-matrix1  
        0   -1  
        4    5  --> in the first iteration of the array inside findDet()  
                while accessing the   
                arr[0][0] gives value 0  
                arr[0][1] gives value -1  
                arr[1][0] gives value 5  
                arr[1][1] gives value 1  

        >>> sub-matrix2
        2   -1
        1    5  --> in the second iteration of the array inside findDet()
                while accessing the 
                arr[0][0] gives value 2
                arr[0][1] gives value -1
                arr[1][0] gives value 5
                arr[1][1] gives value 1

        >>> sub-matrix3
        2    0
        1    4  --> in the third iteration of the array inside findDet()
                while accessing the 
                arr[0][0] gives value 2
                arr[0][1] gives value 0
                arr[1][0] gives value 4
                arr[1][1] gives value 1

        But since we get wrong values for sub-matrices the final determinant is not correct
    */
    for(int i = 0; i < order; i++) {
        // New sub matrix in each iteration
        int newArr[order-1][order-1];
        initNewArr(newArr, arr, order, i);

        // Print the sub matrix
        printf("\nNewly Created Sub Matrix itself...\n");
        for(int i = 0; i < order-1; i++) {
            for(int j = 0; j < order-1; j++) {
                printf("%d\t", newArr[i][j]);
            }
            printf("\n");
        }

        // Calculate Determinant
        if(i % 2 == 0) { // if i is 0 or even which then becomes odd-th element of matrix add result to det
            det += (arr[0][i] * findDet(newArr, order-1));
        } else { // otherwise subtract the result from the det
            det -= (arr[0][i] * findDet(newArr, order-1));
        }
    }
    return det;
} // ================== findDet()

void initNewArr(int newArr[][N-1], int prevArr[][N], size_t order, int exCol) {
    for(int i = 0; i < order; i++) {
        for(int j = 0; j < order; j++) {
            if(i != 0 && j != exCol) { // When element is not in first row and exCol(excluded column) assign it to sub matrix
                newArr[i-1][j > exCol ? j-1 : j] = prevArr[i][j];  // When column is greater than exCol subtract 1 from it
            }

        }
    }
}

递归完美地工作,问题出在上面的表达式中。
编辑
例如,如果我 在访问 arr[0][0]、arr[0][1]、arr[1][0] 和 arr[1][1 时输入
2 -3 1
2 0 -1
1 4 5
] 在以下子矩阵中
0 -1
4 5 --> 在 findDet() 中数组的第一次迭代中

2 -1
1 5 --> 在 findDet() 内的数组的第二次迭代中

2 0
1 4 --> 在 findDet() 内的数组的第三次迭代中

不要给我所有的原始值,它们会给出一些随机值和矩阵中存在的一些值。

您可以查看代码注释以获取更多详细信息,我知道这个问题很长而且有点令人困惑,但我希望你们中的一些人能帮助我?

你能帮我找出问题吗?

谢谢

标签: carraysfunctionmatrix

解决方案


推荐阅读