【问题标题】:How to convert fixed size array to pointer on pointer array如何将固定大小的数组转换为指针数组上的指针
【发布时间】:2019-05-14 17:05:35
【问题描述】:

我正在尝试计算行列式,但构建失败。 有代码,错误下降的地方。

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 传递给determinant 作为double **?使用double mat[N][N],或者更好的是,使用std::arraystd::vector
  • 您可以通过创建一个单独的指针数组来解决此问题,并将它们初始化为指向二维数组的等效行,然后传递它。 double ** 是指向指针类型的指针。这与二维数组不同。

标签: c++ function multidimensional-array


【解决方案1】:

二维数组和一个指向另一个指针的指针不是一回事。为什么getCofactor()函数中的double mat[N][N]determinant()函数中的double **mat等两个不同的函数需要以不同的方式使用mat?由于您知道N 变量的值是什么,因此您也可以在determinant() 函数中将其用作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;
    }

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

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

二维数组和一个指向另一个指针的指针不是 一样的东西。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2014-11-06
    • 2015-05-24
    相关资源
    最近更新 更多