gpt4 book ai didi

c - 动态二维矩阵实现

转载 作者:行者123 更新时间:2023-11-30 19:23:57 26 4
gpt4 key购买 nike

除了静态矩阵之外,我尝试获取二维动态矩阵我想做的就是更改 init 函数,而不是使用定义的高度和宽度它将动态初始化 - 请告诉我如何进行

void init(int board[][WIDTH], int rows) {
int x, y;
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
board[y][x] = 0;
/* Scatter some live cells: */
board[10][25] = 1;
board[10][26] = 1;
board[10][27] = 1;
board[11][25] = 1;
board[12][26] = 1;
}

int main(void) {
int board[HEIGHT][WIDTH];
init(board, HEIGHT);
..
..
}

这是我想使用的代码 - 请告诉我正确的暗示不使用#define WIDTH 50 #define HEIGHT 20

    int **matrix_dyn(int n, int m)
{
int i = 0;
int j = 0;

printf ("please enter the horizontal size of the board \n");
scanf ("%d", &n);

printf ("please enter the vertical size of the board \n");
scanf ("%d", &m);


int **board = (int**)malloc(n * sizeof(int*));

printf("please enter the 0's or 1's to fill the matrix \n");
for (i = 0; i <= n; i++)
board[i] = (int*)malloc(m*sizeof(int));

for(i = 0; i <= n; i++)
{
for(j = 0; j <= m; j++)
scanf ("%d", &board[i][j]);
}
return board;
}

这是我的全部代码:

    #include <stdio.h>
#define WIDTH 50
#define HEIGHT 20

void init(int board[][WIDTH], int rows) {
int x, y;
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
board[y][x] = 0;
/* Scatter some live cells: */
board[10][25] = 1;
board[10][26] = 1;
board[10][27] = 1;
board[11][25] = 1;
board[12][26] = 1;
}


void print(int board[][WIDTH], int rows, int cols)

{
int x, y;
char c;
for (y = 0; y < rows; y++) {
for (x = 0; x < cols; x++) {
if (board[y][x] == 1)
printf("X");
else
printf(" ");
}
printf("\n");
}
printf("Press any key to continue:\n");
getchar();
}


int count_neighbors(int board[][WIDTH], int rows,
int y, int x)
{
int i, j;
int result = 0;
for (i = -1; i <= 1; i++)
if ((y+i >= 0) && (y+i < rows))
for (j = -1; j <= 1; j++)
if ((x+j >= 0) && (x+j < WIDTH))
if ((i != 0) || (j != 0))
result += board[y+i][x+j];
return result;
}



int step(int board[][WIDTH], int rows) { // now returns a bool
int x, y;
int neighbors[HEIGHT][WIDTH];
int changed = 0; // save changes
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
neighbors[y][x] = count_neighbors(board, rows, y, x);
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
if (board[y][x] == 1) { /* Currently alive */
if (neighbors[y][x] < 2)
{
board[y][x] = 0; /* Death by boredom */
changed = 1; // change happened
}
else if (neighbors[y][x] > 3)
{
board[y][x] = 0; /* Death by overcrowding */
changed = 1; // change happened
}
}
else { /* Currently empty */
if (neighbors[y][x] == 3)
{
board[y][x] = 1;
changed = 1; // change happened
}
}
return changed; // return the status (changed yes/no?)
}


int main(void) {
int board[HEIGHT][WIDTH];
init(board, HEIGHT);
while (1) {
print(board, HEIGHT, WIDTH);
if(step(board, HEIGHT) == 0) // no change
break; // leave the loop
}
return 0;
}

最佳答案

像这样声明和分配董事会:

int *board = malloc( n * m * sizeof(int) );

然后,每当您希望访问 board[x][y] 时,请使用以下表达式:

board[y*n+x]

关于c - 动态二维矩阵实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8963963/

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