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.
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.
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.
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.
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
You should have something like
You simply need a variable that holds your current direction, not a separate function for each direction.Code:current_direction = right; if (already_seen(next_square_in_current_direction)) { current_direction = next_direction } else { take_a_step }
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++ }
I'm going to sleep. Today I can't think anymore.
Thanks everyone
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:
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.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"); }
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...
O_oA traversal in a spiral is a pirouette!
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