'C'程序:多维数组问题

时间:2014-03-11 06:19:56

标签: c++ c arrays multidimensional-array

请帮我解决这个问题,我已对其进行了测试并对其进行了测试,并重新阅读并重写了过去11个小时,我放弃了。我发现了其他人写的一个工作代码,但它仍然没有向我解释为什么他的作品和我的作品不是因为我在他的作品上有问题,而不是我的作品

得到了人,为任何有类似问题的人编辑了代码...... 我的原始代码在这里http://pastebin.com/h7fXHKzf 我遇到的问题是它一直挂在if(board [x] [y - 1] =='。')支票上。

说得太快了....程序有时会崩溃......这种情况很少见,但是之前已连续3次崩溃......大部分时间我运行时一切正常。

    // Chapter 8 Programming Project #9

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

    #define SIZE 10
    #define PATH_SIZE ((int) (sizeof(brd_path)))
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

    int main(void)
    {
        char board[SIZE][SIZE] = {};
        char brd_path[25] = {'B', 'C', 'D', 'E', 'F', 'G', 'H',
                             'I', 'J', 'K', 'L', 'M', 'N', 'O',
                             'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                             'W', 'X', 'Y', 'Z'};
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        bool path_dir_chk[4] = {false};
        bool blocked = false;
        unsigned short i, j, x = 0, y = 0;
        // Generate a random number
        srand((unsigned) time(NULL));
        int dir = rand() % 4;
        // Set all positions of board to '.'
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';

        }
        x = 0;
        y = 0;
        board[0][0] = 'A';
        // Generate the path
        while (blocked != true && i != PATH_SIZE) {
            for (i = 0; i < PATH_SIZE;) {
                // Reset path_dir_chk values if empty
                for (j = 0; j < 4; j++) {
                    if (board[x][y - 1] == '.')
                        path_dir_chk[0] = false;
                    if (board[x][y + 1] == '.')
                        path_dir_chk[1] = false;
                    if (board[x - 1][y] == '.')
                        path_dir_chk[2] = false;
                    if (board[x + 1][y] == '.') 
                        path_dir_chk[3] = false;
                }
                // Check the direction and replace that char
                switch (dir) {
                    case 0: if ((y - 1) >= 0 && board[x][y - 1] == '.') {
                        board[x][--y] = brd_path[i];
                        path_dir_chk[0] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 1: if ((y + 1) >= 0 && board[x][y + 1] == '.') {
                        board[x][++y] = brd_path[i];
                        path_dir_chk[1] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 2: if ((x - 1) >= 0 && board[x - 1][y] == '.') {
                        board[--x][y] = brd_path[i];
                        path_dir_chk[2] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 3: if ((x + 1) >= 0 && board[x + 1][y] == '.') {
                        board[++x][y] = brd_path[i];
                        path_dir_chk[3] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                }
            // if all path's are true exit
            if (path_dir_chk[0] == true &&
                path_dir_chk[1] == true &&
                path_dir_chk[2] == true &&
                path_dir_chk[3] == true)
                blocked = true;
            // Reset the random direction
            dir = rand() % 4;
            }
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%c ", board[x][y]);
            printf("\n");
        }

        return 0;
    }

好的我已经做了改变以反映我到目前为止的情况,不是它打印'我现在:'数字1 - 25然后它重新开始但它在第二次停止在12并且冻结成某种形式环

下面是我在网上找到的工作代码,你可以比较两者并看到它们的相似之处,但是他的代码行与我的代码行完全不同我的......

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

    #define ROWS 10
    #define COLS 10
    int main (void)
    {
        int i, j, k, direction;
        char board[ROWS][COLS];
        const char letters[] = {'A', 'B', 'C', 'D', 'E', 'F',
                                'G', 'H', 'I', 'J', 'K', 'L',
                                'M', 'N', 'O', 'P', 'Q', 'R',
                                'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z'};

        srand ((unsigned) time(NULL));

        for (i = 0; i < ROWS; i++)
          for (j = 0; j < COLS; j++)
            board[i][j] = '.';

        i = 0;
        j = 0;
        k = 1;
        //set array[0][0] to first element
        board[i][j] = letters[0];
        while (k < 26) {
            direction = rand() % 4;

            if (board[i][j] == '.')
                board[i][j] = letters[k++];
            if ((board[i][j + 1] != '.' || j == ROWS - 1 )&&
                (board[i + 1][j] != '.' || i == COLS -1) &&
                (board[i - 1][j] != '.' || i == 0) &&
                (board[i][j - 1] != '.' || j == 0))
                break;
            switch (direction) {
              case 0: if (j < ROWS - 1 && board[i][j + 1] == '.'){  //move right
                      j++;
                      break;     }
              case 1: if (i < COLS -1 && board[i + 1][j] == '.') {  //move down
                      i++;
                      break; }
              case 2: if (i > 0 && board[i - 1][j] == '.'){  //move up
                      i--;
                      break;  }
              case 3: if (j > 0 && board[i][j - 1] == '.') { //move left
                      j--;
                      break; }
            }
        }
        for (i = 0; i < ROWS; i++) {
          for (j = 0; j < COLS; j++)
            printf ("%4c", board[i][j]);
          printf ("\n");
        }

        return 0;
    }

3 个答案:

答案 0 :(得分:1)

此代码

后,您没有将x和y设置为0
for (x = 0; x < ROW_SIZE; x++) {
        for (y = 0; y < ROW_SIZE; y++)
            board[x][y] = '.';
    }

因此,x和y将从10开始。此外,您不会检查x和y的范围,这意味着x和y可能会离开棋盘。

此代码

    if ((board[x][y - 1] != '.' || y - 1 < 0) &&
        (board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
        (board[x - 1][y] != '.' || x - 1 < 0) &&
        (board[x + 1][y] != '.' || x + 1 > ROW_SIZE))

应该是这个

    if ((y - 1 <  0        || board[x][y - 1] != '.') &&
        (y + 1 >= ROW_SIZE || board[x][y + 1] != '.') &&
        (x - 1 <  0        || board[x - 1][y] != '.') &&
        (x + 1 >= ROW_SIZE || board[x + 1][y] != '.'))

有两个细微差别。

第一个y + 1和x + 1不允许等于ROW_SIZE,因为ROW_SIZE是10,但有效数组索引是0到9.

其次,秩序很重要。在评估逻辑OR时,首先评估左侧,如果是,则右侧评估。这很重要,因为在某些机器上,即使在数组边界外读取也会导致崩溃。

答案 1 :(得分:0)

for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';
        }

初始化后,您没有重置x和y的值。当X = ROW_SIZE的值时,你试图访问板[x + 1] [y]和板[x] [y + 1]。

x = 0;
y = 0;

答案 2 :(得分:0)

感谢user3386109,您的输入非常宝贵,其余的y&#39; alls帮助我很感激,工作代码如下:)

    // Chapter 8 Programming Project #9

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

    #define SIZE 10
    #define PATH_SIZE 25
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

    int main(void)
    {
        char board[SIZE][SIZE];
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        unsigned short i = 0, x, y;
        // Generate a random number
        srand((unsigned) time(NULL));
        int dir = rand() % 4;
        // Set all positions of board to '.'
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';
        }
        x = 0;
        y = 0;
        board[0][0] = 'A';
        // Generate the path
        for (i = 0; i < PATH_SIZE;) {
                // Check that the last character has not been cornered
            if ((board[x][y - 1] != '.' || y - 1 < 0) &&
                (board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
                (board[x - 1][y] != '.' || x - 1 < 0) &&
                (board[x + 1][y] != '.' || x + 1 > ROW_SIZE))
                break;
            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0
                            && board[x][y - 1] == '.') {
                            board[x][--y] = i + 'B';
                            ++i;
                        } break;
                case 1: if ((y + 1) < ROW_SIZE
                            && board[x][y + 1] == '.') {
                            board[x][++y] = i + 'B';
                            ++i;
                        } break;
                case 2: if ((x - 1) >= 0
                            && board[x - 1][y] == '.') {
                            board[--x][y] = i + 'B';
                            ++i;
                        } break;
                case 3: if ((x + 1) < ROW_SIZE
                            && board[x + 1][y] == '.') {
                            board[++x][y] = i + 'B';
                            ++i;
                        } break;
                default: if (board[x][y] == '.')
                             board[x][y] = i + 'B';
                             break;
            }
        // Reset the random directions
        dir = rand() % 4;
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%4c ", board[x][y]);
            printf("\n");
        }

        return 0;
    }