Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6884797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:33:16+00:00 2026-05-27T05:33:16+00:00

Possible Duplicate: Looping in a spiral I’m creating a program to populate a 3

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:33:16+00:00Added an answer on May 27, 2026 at 5:33 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What does this CSS shorthand font syntax mean? I was looking at
Possible Duplicate: What is this weird colon-member syntax in the constructor? I am looking
Possible Duplicate: PHP preg_split string into letter pairs I have an string looking like
Possible Duplicate: Is recursion ever faster than looping? I was first trained to program
Possible Duplicate: Array to named variables How can I convert an array like this
Possible Duplicate: How does foreach work when looping through function results? If I have
Possible Duplicate: What features are people looking forward to in .Net 4.0 - 4.1
Possible Duplicate: JavaScript data formatting/pretty printer I am getting a bit tired of looking
Possible Duplicate: How I can create installer for website. PHP mysql I'm looking to
Possible Duplicate: What are ‘closures’ in .NET? I am currently looking at lambda expression

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.