0

Possible Duplicate:
Looping in a spiral

I'm creating a program to populate a 3 by 3 matrix. I want to result in something looking like this

5 4 3
6 1 2
7 8 9

As you have probably noticed it is a spiral. Now the algorithm I'm using is this: I have a 2-d array where the values represent the coordinates of the number. First I assign that every number coordinate in this array will have a value of 10. Then starting at 9 I decrease my x coordinate and assign the value of the coordinate to currentnum - 1 until it reaches the end or its value is not 10; Then I do the same thing except I increase the value of Y; Then decrease the value of x; Then of Y;

The reason I assign 10 to every number is so like it acts as a road for my program. Since current num will never exceed 9. If the value of a square is 10 it is like a green light. If it is not 10 meaning a value has been assigned to that square it breaks out of it.

Here is my code, please note it is written in Java

public class spiral {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int spiral [] [] = new int[3][3];
        for(int i = 0; i <= 2; i++){
            for(int j = 0; j <= 2; j++){
                spiral[i][j] = 10;
            }
        }
        //0 is x value, 1 is y value
        spiral[0][0] = 9;
        int x = 1;
        int y = 1;
        int counter = 1;
        int currentnum = 9;
        int gridsquare  = 3;
        for(int i = 0; i <= 8; i++){

            if(counter == 5){
                counter = 1;
            }
            if(counter == 1){
                System.out.println(x + " " + y);
                for(int j = 0;j <= 1;j++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        x += 1;
                    }
                    else{
                        y += 1;
                        break;
                    }
                }
            }
            if(counter == 2){
                for(int k = 0; k <= 0; k++){
                    System.out.print(x + " " + y);
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        y += 1;
                    }
                    else{
                        x -= 1;
                        break;
                    }
                }
            }
            if(counter == 3){
                for(int z = 0; z <= 0; z++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        x -= 1;
                    }
                    else{
                        y -= 1;
                        break;
                    }
                }
            }
            if(counter == 4){
                for(int b = 0; b <= 0; b++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        y -= 1;
                    }
                    else{
                        x += 1;
                        break;
                    }
                }
            }
            counter++;
        }
        System.out.print(currentnum);
    }
}

I'm getting this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at spiral.main(spiral.java:44)

Since I'm new to Java would someone please suggest a posible fix for this. Also if you see any problems with my algorithm please do inform me.

Community
  • 1
  • 1
Tom
  • 515
  • 3
  • 7
  • 21

2 Answers2

1

You do not need to pre-fill with 10: zero works just as well.

I think the best approach to solving the spiral is to think of how you do it manually: start in a corner, and go horizontally until you hit non-zero or an edge of the array. Then you turn right. Stop when the current number goes past N*N.

Now let's look at what each part of the algorithm means:

  • Starting in the corner means setting x=0 and y=0.
  • Going in a straight line means x=x+dx, y=y+dy, where either dx or dy is zero, and dy or dx is 1 or -1.
  • Turning right means assigning dx to dy and -dy to dx.

Here is how it looks in the code:

int current = 1;
// Start in the corner
int x = 0, y = 0, dx = 1, dy = 0;
while (current <= N*N) {
    // Go in a straight line
    spiral[x][y] = current++;
    int nx = x + dx, ny = y + dy;
    // When you hit the edge...
    if (nx < 0 || nx == N || ny < 0 || ny == N || spiral[nx][ny] != 0) {
        // ...turn right
        int t = dy;
        dy = dx;
        dx = -t;
    }
    x += dx;
    y += dy;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You've incremented x or y to 3 which is past the end of one of your arrays.

Step through your program with the debugger or add System.out.println statements before each if (counter) to find out where you're doing this.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161