2维阵列打印搞砸了

时间:2013-11-13 02:49:34

标签: c multidimensional-array

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

 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*******************\n\n");
    for( a = 1, printf("  \t"); a < 19; a++)
    {
        printf("%3d", (b + a));
    }
    printf("\n");
    for(a = 0; a < 9; a++)
    {
        printf("\n%2d\t", 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("");
            }
        }
    }
}

出于某种原因,输出混乱...(如果我传递氦的参数,应该是018这是我得到的:http://pastebin.com/YjhjzSi8 < / p>

我做错了什么? 谢谢

3 个答案:

答案 0 :(得分:2)

那不是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}};

答案 1 :(得分:2)

我使用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 array values C++

答案 2 :(得分:2)

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

此外,您错过了交换机/案例状态中的“中断”,并且您还错过了在每行末尾打印换行符。我冒昧地将变量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*******************\n\n");
    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%2d\t", 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=[*]
相关问题