Thread: Program that generates a "random walk" across 10*10 array

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    Wink Program that generates a "random walk" across 10*10 array

    the program randomly walks through from element to element always going up, down, left, right by one element. the elements visited by the program will be labeled with the letters a through z in the order visited. If no place for a letter to go then the program terminates.

    My problem is that when it reached end of row, it goes to next row and column 0 and populates. I don't want it to go out of bounds or go to next row if at end of row but don't know how to fix it. Otherwise the code works fine.

    I would like some help in fixing that and after fixed, please provide some ideas to simplify the code if possible. tks
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void) {
    
    int i, j, k, direction;
    char board[10][10];
    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 < 10; i++)
      for (j = 0; j < 10; j++)
        board[i][j] = '.';
    
    i = 0;
    j = 0;    
    k = 1;
    board[i][j] = letters[0];
    while (k < 26) {
      direction = rand() % 4;
        switch (direction) {
          case 0: if (board[i][j + 1] == '.'){
                  board[i][j +1] = letters[k];  //move right 
                  k++; j++;}      
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;     
          case 1: if (board[i + 1][j] == '.') {
                  board[i + 1][j] = letters[k];  //move down
                  k++; i++; }
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;
          case 2: if (board[i - 1][j] == '.'){
                  board[i - 1][j] = letters[k];  //move up
                  k++; i--; }
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;      
          case 3: if (board[i][j - 1] == '.') {
                  board[i][j - 1] = letters[k];  //move left
                  k++; j--;}
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;
    }
    }
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++)
        printf ("%4c", board[i][j]);
      printf ("\n");
    }
    
    return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't take a step to the right if you're on the right side of the board. So, before you take a step to the right, you need to check whether you are, in fact, on the right side of the board. If you are, you need to choose a different direction. You'll also need to adjust your "help I'm blocked in" test to take this into account as well.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Check to make sure you don't move when you shouldn't.
    Code:
    if( dir == 0 && row == 0 )
        invalid...
    else
        ok...
    if( dir == 1 && col == COLS -1 )
        invalid...
    else
        ok...
    if( dir == 2 && row == ROWS -1 )
        invalid...
    else
        ok...
    if( dir == 3 && col == 0 )
        invalid...
    else
        ok...
    Assuming 0 is north, and 3 is west.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    it's still not working....I just don't understand how let's say array[0][9] becomes array[[1][0] or array[4][0] becomes array[3][9]?????????

    i modified to look like this with no success
    Code:
    #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;
    board[i][j] = letters[0];
    while (k < 26) {
      direction = rand() % 4;
        switch (direction) {
          case 0: if (board[i][j + 1] == '.' && i != ROWS - 1){
                  board[i][j +1] = letters[k];  //move right 
                  k++; j++;}      
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;     
          case 1: if (board[i + 1][j] == '.' && j != COLS -1) {
                  board[i + 1][j] = letters[k];  //move down
                  k++; i++; }
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;
          case 2: if (board[i - 1][j] == '.' && j != 0){
                  board[i - 1][j] = letters[k];  //move up
                  k++; i--; }
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;      
          case 3: if (board[i][j - 1] == '.' && i != 0) {
                  board[i][j - 1] = letters[k];  //move left
                  k++; j--;}
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;
    }
    }
    for (i = 0; i < ROWS; i++) {
      for (j = 0; j < COLS; j++)
        printf ("%4c", board[i][j]);
      printf ("\n");
    }
    
    return 0;
    
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The same way 9 becomes 10: carry. An array is stored all in one chunk, so the end of one row is followed immediately by the beginning of the next.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    oh yeah, i remember that it's stored in memory consecutively....forgot about that.

    any suggestion to fix? Also any suggestions for improving the code?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could improve your indentation, e.g., indent in the scope of the function and if statements
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    ok. got it...THANK YOU VERY MUCH FOR THE HELP

    Code:
    #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;
    board[i][j] = letters[0];
    while (k < 26) {
      direction = rand() % 4;
        switch (direction) {
          case 0: if (board[i][j + 1] == '.' && j != ROWS - 1){
                  board[i][j +1] = letters[k];  //move right 
                  k++; j++;}      
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;     
          case 1: if (board[i + 1][j] == '.' && i != COLS -1) {
                  board[i + 1][j] = letters[k];  //move down
                  k++; i++; }
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;
          case 2: if (board[i - 1][j] == '.' && i != 0){
                  board[i - 1][j] = letters[k];  //move up
                  k++; i--; }
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;      
          case 3: if (board[i][j - 1] == '.' && j != 0) {
                  board[i][j - 1] = letters[k];  //move left
                  k++; j--;}
                  if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
                  k = 27;
                  break;
    }
    }
    for (i = 0; i < ROWS; i++) {
      for (j = 0; j < COLS; j++)
        printf ("%4c", board[i][j]);
      printf ("\n");
    }
    
    return 0;
    
    }
    CAN I GET SOME HELP WITH IMPROVING THE CODE? ANY SUGGESTIONS?

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    in improving code i mean making it shorter and more to the point if possible

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by danieldcc
    in improving code i mean making it shorter and more to the point if possible
    That may have been what you meant, but ultimately "shorter" and "more to the point" are goals related to readability rather than functionality, and good formatting of code is another such related goal. Another related goal is avoiding the use of magic numbers: you already used ROWS and COLS, but what about 26?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    case 0: if (board[i][j + 1] == '.' && j != ROWS - 1){
    You should be doing this before doing that. Also != still lets > get through. Believe it or not, there is actually a reason I used less-than.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    oh yeah, one last question.....when i wanna break out of the switch loop, i used k = 27; .... is there a different or better way to break out of that while loop? Also, if you run this code, it works 9 out of 10 times but sometimes nothing happens when i run it, must be something wrong somewhere

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by danieldcc View Post
    oh yeah, one last question.....when i wanna break out of the switch loop, i used k = 27; .... is there a different or better way to break out of that while loop? Also, if you run this code, it works 9 out of 10 times but sometimes nothing happens when i run it, must be something wrong somewhere
    You could always try break.

    There are lots of ways to do this. You could goto out. You could set a flag and add a check for that. You could set k to 27 ... provided you don't need the value of k outside of the loop. It also depends how far you want to exit.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    ok. goto works well and i like it better.....this code hangs 1 in 20 times, any reason why?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your new code.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-03-2007, 01:49 PM
  2. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Replies: 3
    Last Post: 01-14-2002, 05:09 PM