Thread: Help - Walk in a bidimensional array

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Holy Moley!

    It's not a different function for every direction - it's one loop, with one turn, when you can't go straight in the direction you are traveling now, and loop.

  2. #17
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Just one function? When I have to go to the right, the ROW is a constant and the COL is a variable. When I have to go down, the ROW is a variable and the COL is a constant.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by juanjuanjuan View Post
    Just one function? When I have to go to the right, the ROW is a constant and the COL is a variable. When I have to go down, the ROW is a variable and the COL is a constant.
    Just because it is not changing *right that instant* does not magically make a variable a constant.

  4. #19
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Quote Originally Posted by tabstop View Post
    Just because it is not changing *right that instant* does not magically make a variable a constant.
    You're right. What I mean is that, in the function Right, the integer row is a constant (the value is the same for all the function) and the integer col is a variable (because it changes it's value)

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by juanjuanjuan View Post
    You're right. What I mean is that, in the function Right, the integer row is a constant (the value is the same for all the function) and the integer col is a variable (because it changes it's value)
    As you have the function written, they're both variables, although admittedly you don't change any of them (the only thing that appears to change is j). Adak's point is that the error here is that you have written a "Right" function in the first place. Don't do that.

  6. #21
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I wrote that because the first thing to do is to go to the right...

    I'm not understanding what do I have to change.. sorry

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should have something like
    Code:
    current_direction = right;
    if (already_seen(next_square_in_current_direction)) {
        current_direction = next_direction
    } else {
        take_a_step
    }
    You simply need a variable that holds your current direction, not a separate function for each direction.

  8. #23
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Yes, I understand that, but I can't write something that works. This is what I've got, but it has a million problems, I suppose:

    Code:
        j=0;
        current_step = Right;
        for (i=1; i<rows; i++)
        {
            if (mat[i+1][j]==-1)
            {
                do{
                    current_step = Down;
                }while(mat[i][j+1]!=-1);
                do{
                    current_step = Left;
                }while(mat[i][j-1]!=-1);
                do{
                    current_step = Up;
                }while(mat[i-1][j]!=-1);
                
            }
                
            j++
        }

  9. #24
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I'm going to sleep. Today I can't think anymore.

    Thanks everyone

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can control the movement through the array, with just one loop, but in noting how simple it was in pseudo code, I probably misled you as to the actual code.

    You're on the right track, but study this:
    Code:
    #include <stdio.h>
    #include <time.h>
    
     
    #define ROW 5
    #define COL 6
    #define UP 1
    #define RIGHT 2
    #define DOWN 3
    #define LEFT 4
     
    void printIt(int A[ROW][COL]);
       
    int main() {
       int A[ROW][COL]= {0};
       int r=0,c=0;
       int dir = 0,count=ROW * COL;
    
       printf("UP is 1, RIGHT is 2, DOWN is 3, and LEFT is 4\n\n");
       A[r][c]=1;
       for(dir=2;count>=0;count--) {
          if(dir==1) {
             if(--r>=0 && !A[r][c]) {
                A[r][c]=1; //VISITED while going UP;
             }else {
                ++r;
                printf("\ndirection=%d\n\n",dir);
                dir=2;
                printIt(A);
             }
          }
          if(dir==2) {
             if(r<ROW && ++c<COL && !A[r][c]) {
                A[r][c]=2; //VISITED while going RIGHT;
             }else {
                --c;
                printf("\ndirection=%d\n\n",dir);
                dir=3;
                printIt(A);
             }
          }
          
          if(dir==3) {
             if(++r<ROW && !A[r][c]) {
                A[r][c]=3; //VISITED while going DOWN;
             }else {
                --r;
                printf("\ndirection=%d\n\n",dir);
                dir=4;
                printIt(A);
             }
          }
          
          if(dir==4) {
             if(--c>=0 && !A[r][c]) {
                A[r][c]=4; //VISITED while going LEFT;
             }else {
                ++c;
                printf("\ndirection=%d\n\n",dir);
                dir=1;
                printIt(A);
             }
          }
       }
       printf("Last Values:\n\n");
       printIt(A);
       printf("\n\n");   
    
       return 0;
    }
    void printIt(int A[ROW][COL]) {
       int r,c;   
       for(r=0;r<ROW;r++) {
          printf("|");
          for(c=0;c<COL;c++) {   
             printf("%2d |",A[r][c]);
          }
          printf("\n-------------------------\n");
       }
       _sleep(5);  //adjust this depending on your compiler
       printf("\n\n");
    }
    If you don't have _sleep(), sleep(), or Sleep(), use a getchar() instead. That way you can see the values change as the traversal in a spiral, is going on.

  11. #26
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Adak View Post
    If you don't have _sleep(), sleep(), or Sleep(), use a getchar() instead. That way you can see the values change as the traversal in a spiral, is going on.
    A traversal in a spiral is a pirouette! I know that this isn't common nomenclature but I think it should be introduced -- somebody has to be the first.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Hodor View Post
    A traversal in a spiral is a pirouette! I know that this isn't common nomenclature but I think it should be introduced -- somebody has to be the first.
    OK, Mr. Dancer! Anything else you'd like to tell us? <ROFL>

  13. #28
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Adak View Post
    OK, Mr. Dancer! Anything else you'd like to tell us? <ROFL>
    In fact, yes: HODOR.

  14. #29
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Sorry, Adak, I didn't understand. I know you are saying when the program has to enter in each loop, but I don't know why you wrote the number of the direction in a bidimensional array...

  15. #30
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    A traversal in a spiral is a pirouette!
    O_o

    This seems like the difference between and aileron role and a barrel role.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Bidimensional array
    By juanjuanjuan in forum C Programming
    Replies: 17
    Last Post: 02-14-2014, 08:43 PM
  2. problem with pointers and bidimensional arrays
    By Gustaff in forum C Programming
    Replies: 4
    Last Post: 02-07-2013, 12:42 AM
  3. Program that generates a "random walk" across 10*10 array
    By danieldcc in forum C Programming
    Replies: 37
    Last Post: 07-23-2011, 01:19 AM
  4. 4 questions about bidimensional table
    By Dan. in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2011, 11:18 AM
  5. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM