Results 1 to 15 of 15
  1. #1
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Exclamation Spiral Array Order Number

    Hei master ... please help me, i wanna know how print a spiral order number like this :

    05 04 03 02 01
    06 19 18 17 16
    07 20 25 24 15
    08 21 22 23 14
    09 10 11 12 13

    Please help me ...

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    OK, so you posted a 5x5 example which involved typing the numbers into your post in a particular order. Just have your code do whatever it was that you did: put the numbers one by one into an array and when your finished print out the array contents.

    It might pay to think of each "circuit" (eg from 01 to 16) as a unit which is repeated.

  3. #3
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    hehe,,, i'm a newbie in Java,,, so please ...
    would you mind to share your source code about it ? ;) :rolleyes:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It would look something like this:
    Java Code:
    public class SpiralNumbers {
      // your code would go in here
    }
    Much luck

  5. #5
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Question

    Quote Originally Posted by Fubarable View Post
    It would look something like this:
    Java Code:
    public class SpiralNumbers {
      // your code would go in here
    }
    Much luck
    yeah,,, much look ... where is your code please ? :o

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry, but this isn't a homework mill. You will get better answers if you first show what work you've done and then ask a question regarding your code.

    Best of luck!

  7. #7
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Question

    ok ok ok,,, this my code before ... but it's contain error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at terbilang.spiralOrder.main(spiralOrder.java:34)
    Java Result: 1


    Java Code:
    public class spiralOrder {
        public static void main(String[]args){
            int userNo = 5; // 5 level
            int sqr = userNo*userNo;
            int r = 1, c = userNo;
            int [][] storeArray = new int[userNo][userNo];
            int n = 1;
            int b;
    
            do{
                for(b=r;b<c;b++){
                    storeArray[r][b] = n;
                    n++;
                }
    
                for(b=r+1;b<=c;b++){
                    storeArray[b][c] = n;
                    n++;
                }
    
                n--;
                for(b=c;b>=r;b--){
                    storeArray[c][b] = n;
                    n++;
                }
    
                for(b=c-1;b>=r+1;b--){
                    storeArray[b][r] = n;
                    n++;
                }
    
                r++;
                c--;
            }
            while(n<=sqr);
    
            if(userNo%2!=0){
                int mid = ((userNo/2)+1);
                storeArray[mid][mid] = sqr;
            }
    
            for (int a=1;a<=userNo;a++){
                for (b=1;b<=userNo;b++){
                    System.out.print(storeArray[a][b]);
                }
                System.out.println("");
    
            }
        }
    }
    please help me :confused:
    Last edited by rendra; 11-17-2009 at 05:33 AM.

  8. #8
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    in your second for statement you say
    storeArray[b][c] = n;
    you declared the array as storeArray[5][5] that means that you can acess 0-4 on each
    so when you try to access storeArray[2][5] it will give you an error.

  9. #9
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Unhappy

    Quote Originally Posted by collin389 View Post
    in your second for statement you say
    storeArray[b][c] = n;
    you declared the array as storeArray[5][5] that means that you can acess 0-4 on each
    so when you try to access storeArray[2][5] it will give you an error.
    So,,, what must i do ? :confused:

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't access the index position that is not in the array?

  11. #11
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    that was funny //your code would go here.....i like you fubarable you should have been a comedian....please post on my antivirus thread

  12. #12
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    hehehehe,,, yeah ... i'm confused ...

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read a tutorial about arrays in Java.

  14. #14
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by rendra View Post
    So,,, what must i do ? :confused:
    You now have this:
    Java Code:
    for (int a=1;a<=userNo;a++)
    but you need something like this:
    Java Code:
    for (int a=0;a<userNo;a++)
    Good luck.

  15. #15
    rendra is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    Hiahhh ... i've break it out 2 days ago ... humh ... I CAN ...
    Owh ya ... for Arnold ... thanks ...

    :)

Similar Threads

  1. Comparing two data types in order to store in array
    By gwithey in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 11:27 AM
  2. [SOLVED] Sorting array in descending order?
    By dan0 in forum New To Java
    Replies: 14
    Last Post: 04-16-2009, 01:19 AM
  3. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 12:01 PM
  4. Number Array to Sound
    By Phantasmagorical in forum Advanced Java
    Replies: 8
    Last Post: 03-01-2009, 06:36 PM
  5. How to create a Hypnosis Spiral in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •