# Thread: Help - Walk in a bidimensional array

1. ## Help - Walk in a bidimensional array

Hello everyone!
What I have to do is to print a bidimensional array of N*N like this:

What I tried to do was:
- a function called Right that goes to the right
- a function called Left that goes to the left
- a function called Up that goes up
- a function called Down that goes down

Like this:

As you can see, in the first bidimensional array, N=6. I have to go 3 times right, 3 times left, 2 times up and 2 times down. So, I have to go N/2 times to the left and to the right, and (N/2 - 1) times to up and down.
In the second bidimensional array, N=7. I have to go 3 times to left, right, up and down. So, I have to go N/2 times to left, right, up and down, and, in the end, I have to add the number that is in [N/2][N/2]

Here's the code. It works fine for this columns and rows:

but I don't know how to do a for that says the program how much times to do those 4 steps.
I know how many times they have to be done:
- If N/2==0, the program has to do (N/2) times to left and right, and (N/2 -1) times up and down
- If N/2==1, the program has to do (N/2) times to left, right, up and down, and I have to add the number in the element [N/2][N/2]

Code:
```#include <stdio.h>
#include <stdlib.h>

void CompleteBidimArray(int rows, int cols, int mat[rows][cols])
{
int i, j;

for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("\n[%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
}

void PrintOriginalBidimArray(int rows, int cols, int mat[rows][cols])
{
int i, j;

for (i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("%4d", mat[i][j]);
}
printf("\n");
}
}

void Right (int rows, int cols, int InitialRow, int mat[rows][cols])
{
int InitialCol=InitialRow;
int j;
printf("\n\nRIGHT\n");
for(j=InitialCol; j<(cols-InitialCol); j++)
{
printf("%4d", mat[InitialRow][j]);
}
}

void Down(int rows, int cols, int InitialCol, int mat[rows][cols])
{
int InitialRow=cols-InitialCol;
int i;
printf("\n\nDOWN\n");
for(i=InitialRow; i<(rows-InitialRow); i++)
{
printf("%4d", mat[i][InitialCol]);
}
}

void Left (int rows, int cols, int FinalCol, int mat[rows][cols])
{
int FinalRow=FinalCol;
int j;
printf("\n\nLEFT\n");
for(j=FinalCol; j>(cols-FinalCol-2); j--)
{
printf("%4d", mat[FinalRow][j]);
}
}

void Up (int rows, int cols, int FinalRow, int mat[rows][cols])
{
int FinalCol=cols-FinalRow-1;
int i;
printf("\n\nUP\n");
for(i=FinalRow-1; i>(rows-FinalRow-1); i--)
{
printf("%4d", mat[i][FinalCol]);
}
}

int main()
{
int rows, cols;
printf("\nRows: ");
scanf("%d", &rows);
printf("\nColumns: ");
scanf("%d", &cols);

int mat[rows][cols];

CompleteBidimArray(rows, cols, mat);
PrintOriginalBidimArray(rows, cols, mat);
Right(rows, cols, 0, mat);
Down(rows, cols, cols-1, mat);
Left(rows, cols, cols-1, mat);
Up(rows, cols, cols-1, mat);

if(rows%2==1)
{
printf("\n\n %d", mat[rows/2][cols/2]);
}

return 0;
}```

Thankssssssss!

Juan

2. Follow the "right hand rule":

1. Start by going right, as far as you can. Put a unique "have been there" value, into each square of the array, you have visited.

2. When you can no longer move into a new square in the current direction, loop back to #1.

1. Start by going right, as far as you can. Put a unique "have been there" value, into each square of the array, you have visited.

2. When you can no longer move into a new square in the current direction, loop back to #1.
I like this but I wouldn't use a "have been there" value as I don't think it's necessary (although it will work and isn't "bad"). You can easily calculate the path length for 1 entire "rotation/spiral" (re-calculate each "rotation".) The total path length is known as well, so you know when to stop pirouetting.

4. I like this but I wouldn't use a "have been there" value as I don't think it's necessary.
O_o

If I understand the problem, one could use the example you gave in the other thread to "clamp" `size-2' over center each pass while using most of the code in the original post for the "edges".

Soma

I don't know what is a "have been there" value. Do you have any example to see how to use it?

I know how many times I have to do each step, but I don't know how to put that into a for.

Thanks!

6. Originally Posted by juanjuanjuan

I don't know what is a "have been there" value. Do you have any example to see how to use it?

I know how many times I have to do each step, but I don't know how to put that into a for.

Thanks!
NO! God NO! Don't start counting steps!

It's simple, in one loop, you have two if statements. The loop ends when there are no vacant squares left. It's EZ!

Yes, I made a maze solver program using it. but I'm not going to post it. THIS IS EASY, and elegant in it's utter simplicity (which makes it a bit hard to see at first). Let the loop do the work for you (almost).

A "has been there" value could be any value: -1, -99, +1, +1234. Set all the array squares to 0, and then as you visit a square, you give it a different value, which is always the same - that's your "been there" value.

7. Sorry, I didn't understand everything. You say that, in a for, I have to write 2 if.
If (continue with the steps)
If (stop because I finish the "square")

Are you saying that? I'm trying to write a program that says that but I can't

8. Originally Posted by Hodor
I like this but I wouldn't use a "have been there" value as I don't think it's necessary (although it will work and isn't "bad"). You can easily calculate the path length for 1 entire "rotation/spiral" (re-calculate each "rotation".) The total path length is known as well, so you know when to stop pirouetting.
Nothing wrong with that approach, but it's more code. This algorithm minimizes BEAUTIFULLY, if you let it. WAY EZ.

9. Originally Posted by juanjuanjuan
Sorry, I didn't understand everything. You say that, in a for, I have to write 2 if.
If (continue with the steps)
If (stop because I finish the "square")

Are you saying that? I'm trying to write a program that says that but I can't
You can use either a for or a while or a do while loop. They all can be made to work in place of the other. I like the for loop for this one.

You turn right on square one, and enter the loop.
Code:
```start the loop: (stop the loop when you have no more empty squares) {
(either the end of the array has been reached, or the square in front of you has a "been visited" value in it)
If (you can't walk in that direction any more, && you have an empty square on your right)
You turn right
}```
Give it some work, Juan!

Isn't that a thing of simple "noir" genre beauty?

WAY EZ.
EZ PZ?

(For those who don't get it, "easy-peasy" is common phrase in certain places.)

11. Originally Posted by Matticus
EZ PZ?

(For those who don't get it, "easy-peasy" is common phrase in certain places.)
DING! DING! DING! Winner winner, chicken dinner!

12. E-Zed, P-Zed? I don't get it (just kidding!)

DING! DING! DING! Winner winner, chicken dinner!
Yes, thank you - and hurry please. I'm hungry

14. Thanks!

I put a "have been visited" value (I chose -1 but it's not a good value.. I would love to put a 'hbv' (have been visited) but I can't)

I don't know why it doesn't work like I want. I wrote the loops like you told me (but I used 1 while and 1 if instead of 2 if)

Code:
```#include <stdio.h>
#include <stdlib.h>

void CompleteBidimArray(int rows, int cols, int mat[rows][cols])
{
int i, j;

for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("\n[%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
}

void PrintOriginalBidimArray(int rows, int cols, int mat[rows][cols])
{
int i, j;

for (i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("%4d", mat[i][j]);
}
printf("\n");
}
}

void Right (int rows, int cols, int InitialRow, int mat[rows][cols])
{
int InitialCol=InitialRow;
int j;
printf("\n\nRIGHT\n");
for(j=InitialCol; j<(cols-InitialCol); j++)
{
printf("%4d", mat[InitialRow][j]);
mat[InitialRow][j]=-1;
}
}

void Down(int rows, int cols, int InitialCol, int mat[rows][cols])
{
int InitialRow=cols-InitialCol;
int i;
printf("\n\nDOWN\n");
for(i=InitialRow; i<(rows-InitialRow); i++)
{
printf("%4d", mat[i][InitialCol]);
mat[i][InitialCol]=-1;
}
}

void Left (int rows, int cols, int FinalCol, int mat[rows][cols])
{
int FinalRow=FinalCol;
int j;
printf("\n\nLEFT\n");
for(j=FinalCol; j>(cols-FinalCol-2); j--)
{
printf("%4d", mat[FinalRow][j]);
mat[FinalRow][j]=-1;
}
}

void Up (int rows, int cols, int FinalRow, int mat[rows][cols])
{
int FinalCol=cols-FinalRow-1;
int i;
printf("\n\nUP\n");
for(i=FinalRow-1; i>(rows-FinalRow-1); i--)
{
printf("%4d", mat[i][FinalCol]);
mat[i][FinalCol]=-1;
}
}

void PrintModifiedBidimArray(int rows, int cols, int mat[rows][cols])
{
int i, j;
printf("\n");
for (i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("%4d", mat[i][j]);
}
printf("\n");
}
}

int main()
{
int rows, cols, i, j;
printf("\nRows and Columns: ");
scanf("%d", &rows);
cols=rows;

j=cols;
int a=0;
int mat[rows][cols];

CompleteBidimArray(rows, cols, mat);
PrintOriginalBidimArray(rows, cols, mat);

Right(rows, cols, a, mat); //first step
for (i=1; i<rows; i++)
{
while((mat[i-1][j])!=-1)
{
Down(rows, cols, cols-1, mat);
Left(rows, cols, cols-1, mat);
Up(rows, cols, cols-1, mat);
}
a++;
if((mat[i-1][j])==-1)
{
Right(rows, cols, a, mat);
}

}

if(rows%2==1)
{
printf("\n\n %d", mat[rows/2][cols/2]);
}

PrintModifiedBidimArray(rows, cols, mat);

return 0;
}```
Thanks!

15. Originally Posted by Matticus
Yes, thank you - and hurry please. I'm hungry
Hang on! You're not going to share? I wouldn't mind a chicken dinner myself Especially one of Adak's fine dining meals!