gpt4 book ai didi

c - 我试图制作绘制 8x8 chesstable 的 c 数组。请帮帮我吗?

转载 作者:行者123 更新时间:2023-11-30 21:17:29 24 4
gpt4 key购买 nike

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

int main()

{
const char *chess[8][8]; //using 2 dimensional array

chess[0][0] = "X";
chess[0][2] = "X";
chess[0][4] = "X"; //writing values step by step
chess[0][6] = "X";


chess[1][1] = "X";
chess[1][3] = "X"; //same
chess[1][5] = "X";
chess[1][7] = "X";

chess[2][0] = "X";
chess[2][2] = "X";// same
chess[2][4] = "X";
chess[2][6] = "X";

chess[3][1] = "X";
chess[3][3] = "X";
chess[3][5] = "X";
chess[3][7] = "X";

chess[4][0] = "X";
chess[4][2] = "X";
chess[4][4] = "X"; // same
chess[4][6] = "X";

chess[5][1] = "X";
chess[5][3] = "X";
chess[5][5] = "X"; // same
chess[5][7] = "X";

chess[6][0] = "X";
chess[6][2] = "X";
chess[6][4] = "X"; //same
chess[6][6] = "X";

chess[7][1] = "X";
chess[7][3] = "X"; //same
chess[7][5] = "X";
chess[7][7] = "X";

int i;
for (i = 0; i < 8; i++) //printing out array
{
int j;
for (j = 0; j < 8; j++)
printf("%c", &chess[i][j]); //same
}
printf("\n");
system("pause"); //same
return 0;


}

最佳答案

我可以发现五个问题:

1)错误类型:

这个

const char *chess[8][8]; 

是 64 个字符指针,但您可能需要 64 个字符,例如

const char chess[8][8]; 

2) 使用字符串而不是字符初始化

这里

chess[0][0] = "X";

您使用字符串进行初始化,但可能需要一个字符,例如

chess[0][0] = 'X'; // single ' instead of double "

3)您没有初始化完整数组

一些数组元素,例如[0][1],未初始化。因此,当您在循环中打印它们时,您会读取未初始化的值。

请参阅此答案的末尾,以获取更紧凑的初始化板的方法。

4)打印参数错误

这里

printf("%c",  &chess[i][j]);

你获取字符的地址。这是错误的 - 只需这样做:

printf("%c",  chess[i][j]);

5) 使用const作为非常量变量

这里

const char *chess[8][8];

你说chess是一个常数。那么以后就无法更改了。相反,你想要

char *chess[8][8];

所以把它们放在一起就是:

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

int main()

{
char chess[8][8]; //using 2 dimensional array

for(int g=0; g<8; ++g)
for (int t=0; t<8; ++t)
chess[g][t] = 'Z';

chess[0][0] = 'X';
chess[0][2] = 'X';
chess[0][4] = 'X'; //writing values step by step
chess[0][6] = 'X';


chess[1][1] = 'X';
chess[1][3] = 'X'; //same
chess[1][5] = 'X';
chess[1][7] = 'X';

chess[2][0] = 'X';
chess[2][2] = 'X';// same
chess[2][4] = 'X';
chess[2][6] = 'X';

chess[3][1] = 'X';
chess[3][3] = 'X';
chess[3][5] = 'X';
chess[3][7] = 'X';

chess[4][0] = 'X';
chess[4][2] = 'X';
chess[4][4] = 'X'; // same
chess[4][6] = 'X';

chess[5][1] = 'X';
chess[5][3] = 'X';
chess[5][5] = 'X'; // same
chess[5][7] = 'X';

chess[6][0] = 'X';
chess[6][2] = 'X';
chess[6][4] = 'X'; //same
chess[6][6] = 'X';

chess[7][1] = 'X';
chess[7][3] = 'X'; //same
chess[7][5] = 'X';
chess[7][7] = 'X';

int i;
for (i = 0; i < 8; i++) //printing out array
{
int j;
for (j = 0; j < 8; j++)
printf("%c", chess[i][j]); //same
printf("\n");
}
printf("\n");
//system("pause"); //same
return 0;
}

输出:

XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX

对于更紧凑的初始化方式,您可以这样做:

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

int main()

{
char chess[8][8]; //using 2 dimensional array

for(int g=0; g<8; ++g)
for (int t=0; t<8; ++t)
{
if (t % 2 == g % 2)
chess[g][t] = 'X';
else
chess[g][t] = 'Z';
}

int i;
for (i = 0; i < 8; i++) //printing out array
{
int j;
for (j = 0; j < 8; j++)
printf("%c", chess[i][j]); //same
printf("\n");
}
printf("\n");
//system("pause"); //same
return 0;
}

关于c - 我试图制作绘制 8x8 chesstable 的 c 数组。请帮帮我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850688/

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