gpt4 book ai didi

c - C中数组转换的问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:08:08 25 4
gpt4 key购买 nike

我在使用 C 中的一些初学者程序时遇到问题,特别是数组输出。

#include <stdio.h>
#include <stdlib.h>

#define X 10
#define Y 10

void init_world(char (*)[Y][X]);
void next_gen(char (*)[Y][X], char (*)[Y][X]);
void put_world(char (*matrix)[Y][X]);

int main(void)
{
int x=0;
char welt1[X][Y];
char welt2[X][Y];
init_world(welt1);
put_world(welt1);
do
{
next_gen(welt1,welt2);
put_world(welt1);
x++;

}while((welt2!=welt1)and (x<10));
getchar();
return 0;
}

void init_world(char (*welt)[Y][X])
{
...
}


void next_gen(char (*zelle)[Y][X], char (*neu)[Y][X])
{
...
}

void put_world(char (*matrix)[Y][X])
{
int y, x;
for(y=0; y<X; y++)
for(x=0; x<Y; x++)
if(matrix[y][x] != 0)
printf("%c",'*');
else printf("%c",' ');
printf("\n");
printf("\n--------------\n");
}

最后一个函数应该打印数组元素。这应该发生在 '*' 或 ' ' 的 maxtrix 中,但它只会按行打印这些内容

******* *****   *************** ***     *** *** *** ***  
*********************** *** *** *** * *

--------------

最佳答案

void put_world(char (*matrix)[Y][X])
{
int y, x;
for(y=0; y<X; y++)
{
for(x=0; x<Y; x++)
if(matrix[y][x] != 0)
printf("%c",'*');
else printf("%c",' ');
printf("\n"); //Out of scope of first for loop previously (keep it in).
}
printf("\n--------------\n");
}

关于c - C中数组转换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41242864/

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