gpt4 book ai didi

c - 二维字符数组中的邻居数量不正确 (C)

转载 作者:行者123 更新时间:2023-11-30 16:34:37 25 4
gpt4 key购买 nike

我一直在为《生命游戏》开发一个程序,我认为已经快完成了,但是我唯一的挫折是该程序错误地读取了邻居的数量,因此输出是因此不正确。

#include <stdio.h>
#include <stdbool.h>

int numRows;
int numColumns;
int steps;

void printGrid(char grid[numRows][numColumns]) {

int i, j;
for(i=0; i<numRows; i++) {
for(j=0;j<numColumns;j++) {
printf("%c", grid[i][j]);
if (j == numRows - 1) {
printf("\n");
}
}
}printf("\n");
}

int neighbourValue(int row, int column, char grid[row][column]) {
if(row < 0 ||
row > numRows -1 ||
column < 0 ||
column > numColumns -1 ||
grid[row][column] == '.') {
return 0;
} else {
return 1;
}
}

int getNeighbourCount(int row, int column, char grid[row][column]) {
int neighbour = 0;

neighbour += neighbourValue(row-1, column-1, grid); // Above Left
neighbour += neighbourValue(row-1, column, grid); // Above
neighbour += neighbourValue(row-1, column+1, grid); // Above Right
neighbour += neighbourValue(row, column-1, grid); // Left
neighbour += neighbourValue(row, column+1, grid); // Right
neighbour += neighbourValue(row+1, column-1, grid); // Below Right
neighbour += neighbourValue(row+1, column, grid); // Below
neighbour += neighbourValue(row+1, column+1, grid); //Below Left

return neighbour;
}

void calculations(int row, int column, char grid[row][column]) {
char grid2[row][column];
int neighbour, x, y;

for(x = 0; x < numRows; x++) {
for(y = 0; y < numColumns; y++) {
neighbour = getNeighbourCount(x, y, grid);
if(neighbour == 3) {
grid2[x][y] = 'X';
} else if (neighbour == 2 && grid[x][y] == 'X') {
grid2[x][y] = 'X';
} else {
grid2[x][y] = '.';
}
}
} printf("\n %d \n", getNeighbourCount(0,1, grid));

我在这里包含了一些调试打印语句,它打印出上面打印的网格上 (0,1) 处的邻居数量。

for(x = 0; x < numRows; x++) {
for(y = 0; y < numColumns; y++) {
grid[x][y] = grid2[x][y];
}
}
printGrid(grid);
}

int main() {

int counter = 0;

scanf("%d %d %d", &numRows, &numColumns, &steps); //Setting up the array

char grid[numRows][numColumns];

int i, j;
for(i=0; i<numRows; i++) {
for(j=0;j<numColumns;j++) {
scanf(" %c", &grid[i][j]);
}
}
printGrid(grid);

do{
calculations(numRows, numColumns, grid);
counter++;
}while(counter < steps);
}

我已经包含了整个代码,但我认为问题出现在 neighbourValue 周围,因为边界可能是错误的。典型的输入是 3 3 3(行列步长)接下来是 XXX XXX XXX。有人可以测试一下并看看哪里出了问题吗?我偷偷怀疑 0,1 处的位置(在其他位置中,可能是“环绕”并读取网格另一端的邻居。

最佳答案

在OP的代码中,grid被声明为一个可变长度的二维数组:

int numRows;      // global variables
int numColumns;
// ...
scanf("%d %d %d", &numRows, &numColumns, &steps);
char grid[numRows][numColumns];

但它的尺寸仅作为参数正确传递给计算函数:

void calculations(int row, int column, char grid[row][column])
// this signature ^^ ^^^ ^^^ ^^^^
// is common to almost all of the OP's functions

calculations(numRows, numColumns, grid);
// ^^^^^^^ ^^^^^^ correct parameters, the sizes of the grid

在调用其他函数时,传递了错误的参数:

// e.g.
int neighbourValue(int row, int column, char grid[row][column])
// ^^^ ^^^^^^ ^^^ ^^^^^^
neighbour += neighbourValue(row-1, column-1, grid); // Above Left
// ^^^ ^^^^

在这些情况下,OP 应传递网格的实际大小:

int neighbourValue(int row, int column, int numColumns, char grid[][numColumns])
// ^^^^^^^^ ^^^^^^

关于c - 二维字符数组中的邻居数量不正确 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49256540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com