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

  1. #31
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    ThebigH - I don't wanna do a bigger array cause the question asks for a 10*10

    iMalc - i reversed as per your advise and works well....gonna go read about "short circuit evauation" now

  2. #32
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    ok. i get it......I didn't think that through....thought because it's OR then the order won't matter, but you're right.

    OK. I think I'm done now thks.

  3. #33
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by danieldcc View Post
    iMalc - i reversed as per your advise and works well....gonna go read about "short circuit evauation" now
    Posts #3, 11, 27. I only told you three times!


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

  4. #34
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    Quote Originally Posted by quzah View Post
    posts #3, 11, 27. I only told you three times!


    Quzah.
    tks quzah

  5. #35
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by danieldcc View Post
    oh man, Tater, I think you're too advance for me.

    iMalc - i think i want to do it this way
    1. You could 'or' a bounds test in with each 'anded' test for a dot in that spot.

    can you give me a little more clue on that
    Ok, you're 33 messages into this thing and no closer to a solution... Will you at least try my suggestion from post #23...

  6. #36
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    No closer to a solution? I got my solution already I believe.

    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];  //set array[0][0] to first element 
    while (k < 26) {
      direction = rand() % 4;
      
    if (board[i][j] == '.')
    board[i][j] = letters[k++];  
    if ((j == ROWS - 1 || board[i][j + 1] != '.') && (i == COLS -1 || board[i + 1][j] != '.') && (i == 0 || board[i - 1][j] != '.') 
        && (j == 0 ||board[i][j - 1] != '.' ))
    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;
    
    }

  7. #37
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by danieldcc View Post
    oh man, Tater, I think you're too advance for me.
    Modulus is actually a very simple operation. a % b == the remainder of a / b. So the subscript is never greater than b. When b is an array size, that's a good thing.

  8. #38
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    Ok, you're 33 messages into this thing and no closer to a solution... Will you at least try my suggestion from post #23...
    He has a solution, and it's taken somewhat reasonable shape now.
    If you're refering to the modulus thing you suggested, that is not useful because he never said he wanted to wrap from one side to the other.
    The idea is to react to hitting a border and not just mimic a borderless grid.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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