错误:不能把“int(*)((((sizetype)(((ssizetype)n)+ 1))+ 1)]”到“int(*)[100]”参数' 1 ' ' int矩阵行列式值(int,int(*)[100])的|

时间:2021-12-30 21:17:31

I have that error but I'm sure I have the same data type and I didn't do anything wrong I suppose. It's for calculating the determinant of a matrix. Someone help. I really can't think of why I have this error :(

我有这个错误,但我确定我有相同的数据类型,我没有做错任何事情。它是用来计算矩阵的行列式的。有人帮助。我真的想不出我为什么会犯这样的错误

#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;

double determinant(double matrix[100][100], int order)
{
    double det, temp[100][100]; int row, col;

    if (order == 1)
        return matrix[0][0];
    else if (order == 2)
        return ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
    else
    {
        for (int r = 0; r < order; r++)
        {
            row = 0;
            col = 0;
            for (int i = 1; i < order; i++)
            {
                for (int j = 0; j < order; j++)
                {
                    if (j == r)
                        continue;

                    temp[row][col] = matrix[i][j];
                    col++;
                }
                row++;
            }
            det += (matrix[0][r] * pow(-1, r) * determinant(temp, order - 1));
        }
        return det;
    }
}


int main()
{
    int n;
    cout << "Enter the dimension: ";
    cin >> n;
    double elem[n][n];

    for (int i = 0; i < n; i++)
    {
        cout << "Enter row " << i << ": ";
        for (int j = 0; j < n; j++)
        {
            cin >> elem[i][j];
        }
        cout << endl;
    }

    cout << determinant(elem, n);
    return 0;
}

3 个解决方案

#1


1  

your prototype is double determinant(double matrix[100][100], int order)

您的原型是双行列式(双矩阵[100][100],int order)

and you call it with determinant(elem, n); when double elem[n][n]; that is a "dynamic" array size so not 100x100

你叫它行列式(elem, n);当双elem[n][n];这是一个“动态”数组大小,所以不是100x100。

it seam compiler assumes n is 1 at compile time so obviously double array [1][1] can't be converted to [100][100]

[1][1]不能转换为[100][100]。

as you wrote it even if your input matrix data is 1x1 you have to store it in 100x100 array. just declare double elem[100][100]; finally at run time ensure user input n < 100 to avoid a bug

当你写它的时候,即使你的输入矩阵数据是1x1你必须把它存储在100x100数组中。刚刚宣布双elem[100][100];最后,在运行时确保用户输入n < 100以避免错误。

#2


1  

You have three problems.

你有三个问题。

First, the size of elem is unknown at compile time. You should use elem[100][100] if you really want the variable on the stack and the size of the matrix really is 100x100.

首先,elem的大小在编译时是未知的。如果您确实想要堆栈上的变量,那么您应该使用elem[100][100],而矩阵的大小实际上是100x100。

Second, your determinant function creates a 10 thousand element matrix on the stack and it is recursive, which means you'll get a lot of them and likely run out stack space. You should consider using a single temp matrix and reusing this for each recursive step.

第二,您的行列式函数在堆栈上创建了一个10,000个元素矩阵,它是递归的,这意味着您将得到许多它们,并且可能耗尽堆栈空间。您应该考虑使用单个的temp矩阵,并在每一个递归步骤中重用这个矩阵。

Third, since you need the matrix size it to be dynamic, declare it on the heap. Something like:

第三,由于您需要矩阵大小它是动态的,在堆上声明它。喜欢的东西:

   double* elem = new double[n * n];

Strictly speaking you do not need to do this, but it will not waste as much memory as a 100x100 matrix if you are calculating the determinant of small matrices.

严格地说,你不需要这样做,但是如果你计算小矩阵的行列式,它不会像100x100矩阵那样浪费那么多的内存。

If you use a one dimensional array, you can pass in an array of any size to determinant (the determinant function should also take a one-dimensional array or double* instead of double[100][100]). You will have to calculate the index yourself using matrix[order*j+i].

如果使用一维数组,则可以将任意大小的数组传递给行列式(行列式函数也应该采用一维数组或double*而不是double[100][100])。你需要用矩阵[order*j+i]来计算索引。

#3


1  

double elem[n][n]; is illegal in C++. Arrays must have dimensions known at compiletime.

双elem[n][n];在c++中是违法的。数组必须在compiletime中有已知的维度。

Your bizarre error message is a result of a compiler attempting to support double elem[n][n] as an extension, but not doing a very good job of it.

您奇怪的错误消息是编译器试图支持double elem[n][n]作为扩展的结果,但并不能很好地完成它。

One way to fix this would be to change your code to be double elem[100][100]; .

解决这个问题的一种方法是将代码改为double elem[100][100];。

To fix it without wasting memory and sticking to Standard C++, you should use std::vector instead of a C-style array. It is simpler to code to use a vector of vectors, although for performance reasons you may want to use a 1-D vector.

要修复它而不浪费内存并坚持标准c++,您应该使用std::vector而不是C-style数组。使用向量向量的代码更简单,尽管出于性能的考虑,您可能希望使用一维向量。

Also, you would need to refactor determinant slightly as you don't really want to be allocating new memory each time you do another step of the recursion. The determinant function needs to know what dimension of memory is allocated, as well as what dimension you want to calculate the determinant on.

此外,您还需要稍微重构行列式,因为在每次执行递归的步骤时,您并不真正想要分配新的内存。行列式函数需要知道分配了什么维度的内存,以及你想要计算行列式的尺寸。

#1


1  

your prototype is double determinant(double matrix[100][100], int order)

您的原型是双行列式(双矩阵[100][100],int order)

and you call it with determinant(elem, n); when double elem[n][n]; that is a "dynamic" array size so not 100x100

你叫它行列式(elem, n);当双elem[n][n];这是一个“动态”数组大小,所以不是100x100。

it seam compiler assumes n is 1 at compile time so obviously double array [1][1] can't be converted to [100][100]

[1][1]不能转换为[100][100]。

as you wrote it even if your input matrix data is 1x1 you have to store it in 100x100 array. just declare double elem[100][100]; finally at run time ensure user input n < 100 to avoid a bug

当你写它的时候,即使你的输入矩阵数据是1x1你必须把它存储在100x100数组中。刚刚宣布双elem[100][100];最后,在运行时确保用户输入n < 100以避免错误。

#2


1  

You have three problems.

你有三个问题。

First, the size of elem is unknown at compile time. You should use elem[100][100] if you really want the variable on the stack and the size of the matrix really is 100x100.

首先,elem的大小在编译时是未知的。如果您确实想要堆栈上的变量,那么您应该使用elem[100][100],而矩阵的大小实际上是100x100。

Second, your determinant function creates a 10 thousand element matrix on the stack and it is recursive, which means you'll get a lot of them and likely run out stack space. You should consider using a single temp matrix and reusing this for each recursive step.

第二,您的行列式函数在堆栈上创建了一个10,000个元素矩阵,它是递归的,这意味着您将得到许多它们,并且可能耗尽堆栈空间。您应该考虑使用单个的temp矩阵,并在每一个递归步骤中重用这个矩阵。

Third, since you need the matrix size it to be dynamic, declare it on the heap. Something like:

第三,由于您需要矩阵大小它是动态的,在堆上声明它。喜欢的东西:

   double* elem = new double[n * n];

Strictly speaking you do not need to do this, but it will not waste as much memory as a 100x100 matrix if you are calculating the determinant of small matrices.

严格地说,你不需要这样做,但是如果你计算小矩阵的行列式,它不会像100x100矩阵那样浪费那么多的内存。

If you use a one dimensional array, you can pass in an array of any size to determinant (the determinant function should also take a one-dimensional array or double* instead of double[100][100]). You will have to calculate the index yourself using matrix[order*j+i].

如果使用一维数组,则可以将任意大小的数组传递给行列式(行列式函数也应该采用一维数组或double*而不是double[100][100])。你需要用矩阵[order*j+i]来计算索引。

#3


1  

double elem[n][n]; is illegal in C++. Arrays must have dimensions known at compiletime.

双elem[n][n];在c++中是违法的。数组必须在compiletime中有已知的维度。

Your bizarre error message is a result of a compiler attempting to support double elem[n][n] as an extension, but not doing a very good job of it.

您奇怪的错误消息是编译器试图支持double elem[n][n]作为扩展的结果,但并不能很好地完成它。

One way to fix this would be to change your code to be double elem[100][100]; .

解决这个问题的一种方法是将代码改为double elem[100][100];。

To fix it without wasting memory and sticking to Standard C++, you should use std::vector instead of a C-style array. It is simpler to code to use a vector of vectors, although for performance reasons you may want to use a 1-D vector.

要修复它而不浪费内存并坚持标准c++,您应该使用std::vector而不是C-style数组。使用向量向量的代码更简单,尽管出于性能的考虑,您可能希望使用一维向量。

Also, you would need to refactor determinant slightly as you don't really want to be allocating new memory each time you do another step of the recursion. The determinant function needs to know what dimension of memory is allocated, as well as what dimension you want to calculate the determinant on.

此外,您还需要稍微重构行列式,因为在每次执行递归的步骤时,您并不真正想要分配新的内存。行列式函数需要知道分配了什么维度的内存,以及你想要计算行列式的尺寸。