# Thread: Could this idea work?

1. ## Could this idea work?

This thing here is a "rectangular progressive spiral". Each point (0, 1, 2, 3, 4, 5...) has x-y coordinates. The goal of my program is to allow the user to type the number of a point (0, 1, 2, 3, 4, 5...) and the program should print its x-y coordinates according to the progression of the spiral...

I have come up with this code, but it doesn't seem to work. If someone can take a look at it and give me some advise, I would truley appreciate it

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

struct COORDINATES
{
int x;
int y;
};

typedef struct COORDINATES coordinates;

int main()
{
int i = 0, n = 0, a, N;
coordinates spiral[10000];

printf("Enter N= ");
scanf("%d", &N);

spiral[0].x = 0;
spiral[0].y = 0;

while (1)
{

for (a = 0; a < n + 1; a++)
{
spiral[1 + i].x = spiral[i].x + 1;
spiral[1 + i].y = spiral[i].y;
i++;
if (i > N)
break;
}

a = 0;

do {
spiral[1 + i].x = spiral[i].x;
spiral[1 + i].y = spiral[i].y + 1;
i++; a++;
if (i > N)
break;
} while (a < n);

if (n > 0)

{
for (a = 0; a < n; a++)
{
spiral[1 + i].x = spiral[i].x;
spiral[1 + i].y = spiral[i].y + 1;
i++;
if (i > N)
break;
}

}

for (a = 0; a < n + 1; a++)
{
spiral[1 + i].x = spiral[i].x - 1;
spiral[1 + i].y = spiral[i].y;
i++;
if (i > N)
break;
}

for (a = 0; a < n + 1; a++)
{
spiral[1 + i].x = spiral[i].x - 1;
spiral[1 + i].y = spiral[i].y;
i++;
if (i > N)
break;
}

for (a = 0; a < n + 1; a++)
{
spiral[1 + i].x = spiral[i].x;
spiral[1 + i].y = spiral[i].y - 1;
i++;
if (i > N)
break;
}

for (a = 0; a < n + 1; a++)
{
spiral[1 + i].x = spiral[i].x;
spiral[1 + i].y = spiral[i].y - 1;
i++;
if (i > N)
break;
}

for (a = 0; a < n + 1; a++)
{
spiral[1 + i].x = spiral[i].x + 1;
spiral[1 + i].y = spiral[i].y;
i++;
if (i > N)
break;
}

n++;

}

printf("The coordinates of N= %d are: \n\n", N);
printf("x = %d\n\n", spiral[N].x);
printf("y = %d\n\n", spiral[N].y);

system("pause");

return 0;

}```
Have fun

2. Is this spiral finite as shown, or is it theoretically infinite? If the former, then maybe a lookup table would be the easiest option.

3. Well you've managed to ctrl-c/ctrl-v your way into a hole, without studying the problem.

Careful observation would note the following.
- a cycle of right, up, left, down
- right and up are of length N, and left and down are of length N+1. The pattern repeats again at N+2.

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

typedef struct coord {
int x, y;
} coord;

coord createSide(coord start, coord direction, int steps, int *num) {
for ( int i = 0 ; i < steps ; i++ ) {
start.x += direction.x;
start.y += direction.y;
printf("%d coord=%d %d\n", *num, start.x, start.y);
(*num)++;
}
return start;
}

int main() {
coord directions[] = {
{ 1, 0 },   // right
{ 0, 1 },   // up
{ -1, 0 },  // left
{ 0, -1 },  // down
};
coord start = { 0, 0 };
int num = 1;
for ( int side = 1 ; side <= 9 ; side += 2 ) {
start = createSide(start,directions[0],side,&num);
start = createSide(start,directions[1],side,&num);
start = createSide(start,directions[2],side+1,&num);
start = createSide(start,directions[3],side+1,&num);
}
}```

4. @Gabriel.K, just a thought: If you intend to solve the URI Online exercise this way, it will fail... too slow!

5. The trick to this problem is finding how far above a perfect square your number is...

If the square is from an odd number, you know to start from the line going down/right; you know how far up you should go before turning left...

If the square is from an even number, you know to start from the line going up/left; down... and then right...

If I tell you any more, I will rob you of your learning experience!