c++ - 如何将固定大小数组转换为指针数组上的指针

标签 c++ function multidimensional-array

我正在尝试计算行列式,但构建失败。 有代码,错误就会减少。

void getCofactor(double mat[N][N], double temp[N][N], int p, int q, int n)
    {
        int i = 0, j = 0;

        // Looping for each element of the matrix
        for (int row = 0; row < n; row++)
        {
            for (int col = 0; col < n; col++)
            {
                //  Copying into temporary matrix only those element
                //  which are not in given row and column
                if (row != p && col != q)
                {
                    temp[i][j++] = mat[row][col];

                    // Row is filled, so increase row index and
                    // reset col index
                    if (j == n - 1)
                    {
                        j = 0;
                        i++;
                    }
                }
            }
        }
    }

    double determinant(double **mat, int n)
    {
        double D = 0; // Initialize result

        //  Base case : if matrix contains single element
        if (n == 1)
            return mat[0][0];

        double temp[N][N]; // To store cofactors

        int sign = 1;  // To store sign multiplier

        // Iterate for each element of first row
        for (int f = 0; f < n; f++)
        {
            // Getting Cofactor of mat[0][f]
            getCofactor(mat, temp, 0, f, n);    //ERORRRRRRRRRRR
            D += sign * mat[0][f] * determinant(temp, n - 1);

            // terms are to be added with alternate sign
            sign = -sign;
        }

        return D;
    }

如何解决这个问题? ma​​in.cpp:49:50: 错误:无法将参数“1”的“double (*)[4]”转换为“double”到“double 行列式(double**, int)”* * D += 符号 * mat[0][f] * 行列式(temp, n - 1);

最佳答案

二维数组和指向另一个指针的指针不是一回事。为什么两个不同的函数需要以不同的方式使用mat,例如函数getCofactor()中的double mat[N][N] > 和 determinant() 函数中的 double **mat ?由于您知道 N 变量的值是多少,因此您可以将其用作 行列式 () 中的 double mat[N][N]功能也一样。

double determinant(double mat[N][N], int n)
    {
        double D = 0; // Initialize result

        //  Base case : if matrix contains single element
        if (n == 1)
            return mat[0][0];

        double temp[N][N]; // To store cofactors

        int sign = 1;  // To store sign multiplier

        // Iterate for each element of first row
        for (int f = 0; f < n; f++)
        {
            // Getting Cofactor of mat[0][f]
            getCofactor(mat, temp, 0, f, n);    //ERORRRRRRRRRRR
            D += sign * mat[0][f] * determinant(temp, n - 1);

            // terms are to be added with alternate sign
            sign = -sign;
        }

        return D;
    }

一旦您进行了我提到的更改,您的错误就会得到修复。希望对您有所帮助。

补充:我想纠正一些可能引起误解的内容;

Array with two dimensions and a pointer to another pointer are not the same things.

请注意,您可以拥有类似于带有指针的多维数组的东西。但它们在RAM中的占用和存储方式并不相同。请注意,多维数组是单个内存块。为了更好地理解它,请检查以下问题;

Why can't we use double pointer to represent two dimensional arrays?

关于c++ - 如何将固定大小数组转换为指针数组上的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53762574/

相关文章:

c++ - Windbg源代码调试,在本地窗口中看不到本地变量

c++ - C++ 命名空间后的类名

javascript - 在 IE 11 中获取未定义的 javascript 函数

PHP从表单传递和接收二维数组

php - 按自定义顺序对数组的php数组进行排序

c++ - 无法使用GCC在Ubuntu中编译C++ —包含/库问题(collect2 : ld returned 1 exit status)

c++ - 使用pimpl习语设计类的实例化和销毁

计算字符串中的字符 (C) - 问题

c++ - 通过引用返回已销毁的局部变量的成员

perl - 在 Perl 中遍历多维散列