谁能帮忙找出这段代码中的错误?我无法得到错误,它给出了运行时错误。我检查了所有用于矩阵的循环,但没有发现错误。
int n, arr[n][n];
cin>>n;
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cin>>arr[i][j];
}
}
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
sum = 0;
prev = ne;
ne = 0;
if(arr[i][j] == 1)
{
ne = i+j;
prev = i+j;
sum = ne - prev;
if(sum<0)
sum=-sum;
steps+=sum;
}
c++;
}
}
最佳答案
您正在使用变量 n 来创建数组但未对其进行初始化。 You must always use a constant for initializing a static array
做这样的事情:
const int n = SOME_VALUE;
int arr[n][n];
//rest remains same
此外,您应该使用 < n 而不是 John 指出的 <= n。
如果你想要一个动态数组,那么使用STL vector会更好。
关于c++ - SIGSEGV 错误(可能是数组索引越界),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34156806/