我的代码遍历一个矩阵,该矩阵转换为2D数组,但是当我尝试根据某些规则更改值时,代码永远不会到达最后一行。没有错误,程序在到达最后一行之前就结束了。可能是该数组在获取相邻单元格的计数时超出范围,但是我认为会由此产生一条错误消息,并且我不确定为什么这行不通,我认为我已经if语句中的条件正确,但可能不正确。
我用来测试代码的2D数组看起来像这样(我认为这会缩短代码并更好地了解2D数组的使用方式和外观)
行0:1000
第1行:0111
第2行:0010
以及
行0:1000
第1行:0111
第2行:0010
第3行:0100
第4行:0001
两者总是会在到达最后一行之前停止。
下面是我的代码,虽然有点多,但是我认为诊断问题是必要的。
getNeighbors函数基本上只是查看行和列的位置并计算周围1的数量,每个单元格最多具有8个邻居(世界边缘的单元格会更少)。
int getNeighbors(int **Bacteria, int rows, int columns, int row, int column) {
int count = 0;
if (row==0 && column==0) {
if (Bacteria[row+1][column+1] == 1) {
count++;
}
if (Bacteria[row][column+1] == 1) {
count++;
}
if (Bacteria[row+1][column] == 1) {
count++;
}
return count;
} else if (row==rows && column==columns) {
if (Bacteria[row-1][column-1] == 1) {
count++;
}
if (Bacteria[row][column-1] == 1) {
count++;
}
if (Bacteria[row-1][column] == 1) {
count++;
}
return count;
} else if (row==rows && column==0) {
if (Bacteria[row-1][column+1] == 1) {
count++;
}
if (Bacteria[row][column+1] == 1) {
count++;
}
if (Bacteria[row-1][column] == 1) {
count++;
}
return count;
} else if (row==0 && column==columns) {
if (Bacteria[row+1][column-1] == 1) {
count++;
}
if (Bacteria[row][column-1] == 1) {
count++;
}
if (Bacteria[row+1][column] == 1) {
count++;
}
return count;
} else if (row==0) {
if (Bacteria[row+1][column+1] == 1) {
count++;
}
if (Bacteria[row][column-1] == 1) {
count++;
}
if (Bacteria[row][column+1] == 1) {
count++;
}
if (Bacteria[row+1][column] == 1) {
count++;
}
if (Bacteria[row+1][column-1] == 1) {
count++;
}
return count;
} else if (row==rows) {
if (Bacteria[row-1][column+1] == 1) {
count++;
}
if (Bacteria[row][column-1] == 1) {
count++;
}
if (Bacteria[row][column+1] == 1) {
count++;
}
if (Bacteria[row-1][column] == 1) {
count++;
}
if (Bacteria[row-1][column-1] == 1) {
count++;
}
return count;
} else if (column==0) {
if (Bacteria[row+1][column+1] == 1) {
count++;
}
if (Bacteria[row][column+1] == 1) {
count++;
}
if (Bacteria[row+1][column] == 1) {
count++;
}
if (Bacteria[row-1][column] == 1) {
count++;
}
if (Bacteria[row-1][column+1] == 1) {
count++;
}
return count;
} else if (column==columns) {
if (Bacteria[row-1][column-1] == 1) {
count++;
}
if (Bacteria[row][column-1] == 1) {
count++;
}
if (Bacteria[row-1][column] == 1) {
count++;
}
if (Bacteria[row+1][column] == 1) {
count++;
}
if (Bacteria[row+1][column-1] == 1) {
count++;
}
return count;
} else {
if (Bacteria[row-1][column+1] == 1) {
count++;
}
if (Bacteria[row][column-1] == 1) {
count++;
}
if (Bacteria[row][column+1] == 1) {
count++;
}
if (Bacteria[row-1][column] == 1) {
count++;
}
if (Bacteria[row+1][column] == 1) {
count++;
}
if (Bacteria[row+1][column+1] == 1) {
count++;
}
if (Bacteria[row+1][column-1] == 1) {
count++;
}
if (Bacteria[row-1][column-1] == 1) {
count++;
}
return count;
}
}
void changeGeneration(int **Bacteria, int rows, int columns) {
for (int x = 0; x < rows; x++) {
for (int y = 0; y < columns; y++) {
int count = getNeighbors(Bacteria, rows, columns, x, y);
if (Bacteria[x][y] == 1 && count < 2) {
Bacteria[x][y] = 0;
} else if (Bacteria[x][y] == 1 && count > 3) {
Bacteria[x][y] = 0;
} else if (Bacteria[x][y] == 0 && count == 3) {
Bacteria[x][y] = 1;
}
}
}
}
答案 0 :(得分:3)
首先,有一种更简单的方法来表达您要执行的操作:
int count = 0;
for (int n_row = row - 1; n_row <= row + 1; ++n_row) {
for (int n_col = col - 1; n_col <= col + 1; ++n_col) {
if (n_row >= 0 && n_row < rows && n_col >= 0 && n_col < columns
&& (n_row != row || n_col != col)) {
if (Bacteria[n_row][n_col] == 1) {
++count;
}
}
}
}
第二,当您说它没有到达最后一行时,示例中rows
的值是什么?请记住,总行数比最高行索引大一。因此,如果您有0、1、2、3和4行,则rows
应该为5。