Thread: Program fail one out of five times, can't find the cause

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    2

    Program fail one out of five times, can't find the cause

    I'm a new C learner and currently solving problems from
    "C Programming: A Modern Approach "

    The goal of the problem is to write a program that print a 10*10 array.
    Starting from the up left corner , take one step at a time in only four directions(up, down, left, right),and can't go outside the 10*10 nor going backwards. The steps are represented by A~Z.

    Here is my code:
    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<time.h>
    #define ROW 10
    #define COLUMN 10
    
    int main()
    {
      char board[ROW][COLUMN], movement;
      int n, i, j, current_row = 0, current_column = 0, randnum, step;
    
      //Building board
      for (i = 0; i < ROW; i++) {
        for (j = 0; j < COLUMN; j++) {
          board[j] = '.';
        }
      }
    
      srand((unsigned) time(NULL));
      movement = 'A';
      board[current_row][current_column] = movement;
      //Moves
      while (movement < 'Z') {
        for (;;) {
          randnum = rand() % 4;
          switch (randnum) {
          case 0:
            if ((current_row - 1) < 0
                || board[current_row - 1][current_column] != '.')
              break;
            else
              current_row -= 1;
            break;
          case 1:
            if ((current_row + 1) > 9
                || board[current_row + 1][current_column] != '.')
              break;
            else
              current_row += 1;
            break;
          case 2:
            if ((current_column - 1) < 0
                || board[current_row][current_column - 1] != '.')
              break;
            else
              current_column -= 1;
            break;
          case 3:
            if ((current_column + 1) > 9
                || board[current_row][current_column + 1] != '.')
              break;
            else
              current_column += 1;
            break;
          default:
            break;
          }
          if (board[current_row][current_column] == '.')
            break;
          if (board[current_row - 1][current_column] != '.'
              && board[current_row + 1][current_column] != '.'
              && board[current_row][current_column - 1] != '.'
              && board[current_row][current_column + 1] != '.')
            goto PRINT;
        }
        ++movement;
        board[current_row][current_column] = movement;
      }
      //Print board
    PRINT:
    
      for (i = 0; i < ROW; i++) {
        for (j = 0; j < COLUMN; j++) {
          printf(" %c", board[j]);
          if ((j + 1) % 10 == 0)
            printf("\n");
        }
      }
      return 0;
    
    }
    One out of five times i run the program, it stops as if it can't jump out of the loop and freezes. I've reread the code several times but still couldn't spot the problem. Can any help?
    Last edited by Salem; 04-10-2020 at 06:34 AM. Reason: removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Between lines 56 and 57, put this
    Code:
    printf("At %d, %d, char=%c\n", current_row, current_column, board[current_row][current_column]);
    Staring at code won't tell you much.

    Being proactive and putting in code to print the state of things as you go is better.

    Better still would be to use a debugger.

    Chances are you've painted yourself into a corner from which there is no escape.
    Code:
    ABC
    HID
    GFE
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    2
    Thanks for the guide. Try this now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-24-2012, 12:31 AM
  2. Find no of times a no is repeated in a array.
    By Anitrex in forum C Programming
    Replies: 4
    Last Post: 03-18-2012, 06:07 AM
  3. using std::find linking fail in linux
    By mellaicp in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2009, 07:38 PM
  4. fail to count digit of an integer (fail at 9)
    By azsquall in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 09:42 AM
  5. Grade program, pass/fail
    By jadedreality in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2007, 03:58 AM

Tags for this Thread