gpt4 book ai didi

c - 动态二维阵列打印C : why does my array only print out a single line

转载 作者:行者123 更新时间:2023-11-30 15:08:46 25 4
gpt4 key购买 nike

我需要编写一个动态函数,用 C 语言打印二维数组的指定值。这些值需要能够更改,最好是在其他函数中,并且该函数仍应打印正确的显示。因此,我开始在函数本身中分配值并打印这些值,但 2D 结构不会打印出来,所有内容都只在一行上。

这就是我想要的:

  1 2 3 4 5 6   \n
\n
x x x x x x 1\n
x x x x x x 2\n
x x x x x x 3\n
x x x x x x 4\n
x x x x x x 5\n
x x x x x x 6\n
\n
\n

代码如下:正如你所看到的,我尝试了各种策略......xD

/* assigncleanboard: this function is used to initiate the display board by assigning question marks to all cells in the display board at a new game. */

#include <stdio.h>
#define MAXSIZE 15

void assigncleanboard(int size) {

char displayboard[MAXSIZE][MAXSIZE];
int row=0, col=0;


for(row=0; row<size; row++)
printf("\n");


if(size<=(MAXSIZE-3) && size>=(MAXSIZE-9)) {

//for(row=2; row<(size+2); row++) {
// displayboard[row][MAXSIZE] = 47+row;
// printf(" %c ", displayboard[row][MAXSIZE]);
//}



for(col=0; col<size+3; col++) {
for(row=0; row<MAXSIZE; row++) {
if(col == 0 && col == 1 && col == size && col == (size+2) && row == 0 && row == 1 && row == size && row == (size+2) ) {
displayboard[row][col] = 32;
}
else {
displayboard[row][col] = 88;
}
displayboard[0][col] = 49+col;
displayboard[row][size+3] = 49+row;
printf("| %c |", displayboard[row][col]);
}
}
}
/*

for(row=2; row<(size+2); row++) {
for(col=2; col<(size+2); col++)
displayboard[row][col] = 63;
}

for(row=0; row<size; row++) {
for(col=0; col<size; col++)
displayboard[row][col]=88;
printf("%c", displayboard[row][col]);
}

printf("%c", displayboard[2][MAXSIZE]);

for(row=2; row<(size+2); row++) {
for(col=2; col<(size+2); col++)
printf("%c", displayboard[row][col]);
}

for(row=0; row<size; row++)
printf("\n");
}*/

else
printf("Invalid number:\n");

}

我也尝试过这个:

    if(size<=(MAXSIZE-3) && size>=(MAXSIZE-9)) {

//for(row=2; row<(size+2); row++) {
// displayboard[row][MAXSIZE] = 47+row;
// printf(" %c ", displayboard[row][MAXSIZE]);
//}



for(col=0; col<size+4; col++) {
for(row=0; row<size+4; row++) {
if(col == 0)
displayboard[row][col] = 32;
else if(col == 1)
displayboard[row][col] = 32;
else if(col == size)
displayboard[row][col] = 32;
else if(col == size+3)
displayboard[row][size+3] = 49+row;
else if(row == 0)
displayboard[0][col] = 49+col;
else if(row == 0 && col == size)
displayboard[row][col] = 32;
else if(row == 1 && col == size+3)
displayboard[row][col] = 32;
else
displayboard[row][col] = 88;


printf(" %c ", displayboard[row][col]);
}
}
}

最佳答案

您的代码应该多次运行 printf,否则您的程序可能会崩溃。你能调试你的代码吗?我运行了下面的代码并得到了多个 printf。

#include <stdio.h>
#define MAXSIZE 15

void assigncleanboard(void)
{
char displayboard[MAXSIZE][MAXSIZE];
int row = 0, col = 0;

for(row = 0; row < MAXSIZE; row++)
{
printf("\n");

for(col = 0; col < MAXSIZE; col++)
{
printf("| %c |", displayboard[row][col]);
}
}
}

编辑:我的打印输出:

| 0.9 || 1.0 || 0.2 || 0.4 || 0.7 || 0.6 || 0.8 || 0.4 || 0.4 || 1.0 || 0.3 || 0.4 || 0.1 || 0.4 || 0.7 |
| 0.4 || 0.5 || 0.9 || 0.4 || 0.9 || 1.0 || 0.7 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.1 || 0.1 |
| 0.6 || 0.1 || 0.7 || 0.3 || 0.3 || 0.8 || 0.9 || 0.1 || 0.7 || 0.8 || 0.3 || 0.5 || 0.1 || 0.4 || 0.6 |
| 0.6 || 0.8 || 0.7 || 0.0 || 0.3 || 0.2 || 0.8 || 0.6 || 0.6 || 0.6 || 0.2 || 0.9 || 0.9 || 0.2 || 0.3 |
| 0.0 || 0.2 || 0.3 || 0.3 || 0.4 || 0.2 || 0.7 || 0.8 || 0.0 || 0.9 || 0.5 || 0.4 || 0.3 || 0.7 || 0.6 |
| 0.5 || 0.6 || 0.7 || 0.6 || 0.7 || 0.6 || 0.8 || 0.6 || 0.8 || 0.9 || 0.8 || 0.6 || 0.1 || 0.7 || 0.2 |
| 0.9 || 1.0 || 0.9 || 0.4 || 0.7 || 0.1 || 0.2 || 0.8 || 0.0 || 0.4 || 0.9 || 0.1 || 0.7 || 0.1 || 0.8 |
| 0.8 || 0.6 || 0.9 || 0.9 || 0.1 || 0.5 || 0.2 || 0.3 || 0.5 || 0.7 || 0.2 || 0.6 || 0.5 || 0.4 || 0.6 |
| 0.6 || 0.6 || 0.1 || 0.9 || 0.0 || 0.5 || 0.5 || 0.9 || 0.9 || 0.5 || 0.3 || 0.8 || 0.7 || 0.2 || 0.8 |
| 0.5 || 0.2 || 0.4 || 1.0 || 0.1 || 0.2 || 0.7 || 0.2 || 0.6 || 0.6 || 0.7 || 0.7 || 0.5 || 0.7 || 0.5 |
| 0.5 || 0.7 || 0.4 || 0.4 || 0.8 || 0.2 || 1.0 || 0.8 || 0.1 || 0.3 || 0.3 || 0.7 || 1.0 || 0.4 || 0.9 |
| 0.7 || 0.5 || 0.1 || 0.6 || 0.2 || 0.0 || 0.2 || 0.5 || 0.3 || 0.4 || 0.9 || 0.5 || 0.5 || 0.2 || 0.3 |
| 0.9 || 0.0 || 0.2 || 0.4 || 1.0 || 0.5 || 0.4 || 0.3 || 0.5 || 0.6 || 0.6 || 0.4 || 0.4 || 0.7 || 0.8 |
| 0.5 || 0.7 || 0.7 || 0.7 || 0.9 || 0.2 || 0.6 || 0.8 || 0.5 || 0.5 || 0.3 || 0.2 || 0.2 || 1.0 || 0.8 |
| 0.8 || 0.4 || 0.6 || 0.7 || 0.5 || 0.3 || 0.5 || 0.6 || 0.5 || 0.3 || 0.3 || 0.2 || 0.6 || 0.0 || 0.6 |

关于c - 动态二维阵列打印C : why does my array only print out a single line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150253/

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