Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

c++ - Error: cannot convert 'double (*)[(order + -1)]' to 'double (*)[100]

I am writing a code to find the determinant of a given matrix after reading it from a text file which has the same format of providing the data about the matrix. Here is a sample of the matrix.txt file:


3 1 2 3 4 5 6 7 8 9


My code:

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;
#define N 100
double det (double mat [N] [N], int order);

int main ()
{
    ifstream fin;
    fin.open ("matrix.txt");
    int n;
    double readMatrix [N][N];
    fin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            fin >> readMatrix [i][j];
        }
    }
    fin.close();
    cout << det(readMatrix, n) << endl;;


    return 0;
}

double det (double mat [N] [N], int order)
{
    double coff [order - 1][order - 1];
    double delta = 0;

    if (order == 2)
    {
        return ((mat[0][0])*(mat [1][1]) - (mat [0][1])*(mat [1][0]));
    }

    for (int i = 0; i < order; i++)
    {
        for (int k = 1; k < order; k++)
        {
            for(int j = 0; j < k; j++)
            {
                coff [k-1][j] = mat [k][j];
            }
            for (int j = k + 1; j < order; j++)
            {
                coff [k-1][j-1] = mat [k][j];
            }
        }

        delta += (pow(-1, i) * mat[0][i] * det( coff, order - 1 ));
    }
    return delta;
}

I am getting error in delta += (pow(-1, i) * mat[0][i] * det( coff, order - 1 ));. This is the error Error: cannot convert 'double ()[(order + -1)]' to 'double ()[100]. I looked up the internet but people who got this error were generally making the same mistake of passing the size of the array in the function i.e. function(array [n]).

question from:https://stackoverflow.com/questions/65650262/error-cannot-convert-double-order-1-to-double-100

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...