2

I`m trying to calculate determinant, but build fails. There's code, where error drops.

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;
    }

How to fix this problem? main.cpp:49:50: error: cannot convert ‘double (*)[4]’ to ‘double’ for argument ‘1’ to ‘double determinant(double**, int)’** D += sign * mat[0][f] * determinant(temp, n - 1);

2
  • 1
    Why do you pass mat to determinant as double ** if you know it's size? use double mat[N][N], or better yet, use std::array or std::vector. Commented Dec 13, 2018 at 13:12
  • You fix this problem by creating a separate array of pointers, and initializing them to point to the equivalent rows of the two-dimensional array, and then passing it. double ** is a pointer to a pointer type. Which is not the same as an array with two dimensions. Commented Dec 13, 2018 at 13:12

1 Answer 1

2

Array with two dimensions and a pointer to another pointer are not the same things. Why do you need to use mat in different ways for two different functions such as double mat[N][N] in the function getCofactor() and double **mat in the determinant() function ? Since you know what is the value of N variable, you can use it as double mat[N][N] in the determinant() function as well.

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;
    }

Once you make the change I mentioned, your error will be fixed. Hope it helps.

Addition: I want to correct something which can cause a misunderstanding;

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

Please note that you can have something which acts like a multidimensional array with pointers. But the way how they are occupying and storing in the RAM is not same. Note that, a multidimensional array is a single block of memory. To understand it better, check the following question;

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.