C语言 二维数组打印搞砸了



我正在制作一个程序,该程序将打印出元素周期表,[ ]元素,并[*]您正在搜索的元素...

 void printmap(int x, int y)
{
    int a, b, table[9][18] = {
        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
        1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
        1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 
    };   // 0 - Empty, 1 - [ ], 2 - [*]
    table[x][y] = 2;
        b = 0;
    printf("n*******************nn");
    for( a = 1, printf("  t"); a < 19; a++)
    {
        printf("%3d", (b + a));
    }
    printf("n");
    for(a = 0; a < 9; a++)
    {
        printf("n%2dt", a + 1);
        for(b = 0; b < 18; b++)
        {
            switch(table[a][b])
            {
                case 0 :
                printf("   ");
                case 1 : 
                printf("[ ]");
                break;
                case 2 : 
                printf("[*]");
                break;
                default : 
                printf("");
            }
        }
    }
}

由于某种原因,输出被搞砸了...(如果我通过氦的论点,这应该是0的,18这就是我得到的:http://pastebin.com/YjhjzSi8

我做错了什么?谢谢

这不是一个2D数组,仅仅因为你让它看起来像一个2D数组,并不意味着它是一个。

这是什么你拥有的

 table[9][18] = {
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
    ...
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
 };

您应该相应地添加更多括号

 table[9][18] = {
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
    {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
    ....
    {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}};

自从我使用C++以来已经有一段时间了,但是,这不是您初始化 2D 数组的方式。事实上,我很惊讶它甚至编译。正确的方法是这样的:

table[0] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
table[1] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
table[2] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
...

另一个示例:2D 数组值C++

正如其他受访者所说,您需要正确初始化表数组。

此外,您错过了

开关/机箱状态中的"中断",并且还错过了在每行末尾打印换行符。 我冒昧地将您的变量 a,b 重命名为 row,col 并修复了您的代码,这应该可以满足您的需求,

void printmap(int x, int y)
{
    int row=0, col=0;
    int was = table[x][y];
    table[x][y] = 2;
    col = 0;
    printf("n*******************nn");
    printf("  t");
    row=0; //you were not initializing row
        for( col = 0; col < 18; col++)
        {
            printf("%3d", (col+row+1));
        }
    printf("n");
    for(row = 0; row < 9; row++)
    {
        printf("n%2dt", row + 1);
        char* rc = "   ";
        for(col = 0; col < 18; col++)
        {
            switch(table[row][col])
            {
            case 0 : rc=("   "); break; //was missing break
            case 1 : rc=("[ ]"); break;
            case 2 : rc=("[*]"); break;
            default: rc=("   "); break; //was missing break
            }
            printf("%s",rc);
        }
        printf("n"); //missing print newline
    }
    table[x][y] = was;
}

正如其他人所建议的那样,这是表格,

int table[9][18] = {
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
    { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }
};  // 0=Empty, 1=[ ], 2=[*]

最新更新