# Thread: Help - Walk in a bidimensional array

1. 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. 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. Originally Posted by juanjuanjuan
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. Originally Posted by tabstop
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. Originally Posted by juanjuanjuan
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. 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. 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. 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. I'm going to sleep. Today I can't think anymore.

Thanks everyone

10. 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. Originally Posted by Adak
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. Originally Posted by Hodor
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. Originally Posted by Adak
OK, Mr. Dancer! Anything else you'd like to tell us? <ROFL>
In fact, yes: HODOR.

14. 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. A traversal in a spiral is a pirouette!
O_o

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

Soma

Popular pages Recent additions