# Spiral Array Order Number

Printable View

• 11-17-2009, 04:50 AM
rendra
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 ...
• 11-17-2009, 05:09 AM
pbrockway2
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.
• 11-17-2009, 05:15 AM
rendra
hehe,,, i'm a newbie in Java,,, so please ...
would you mind to share your source code about it ? ;) :rolleyes:
• 11-17-2009, 05:17 AM
Fubarable
It would look something like this:
Code:

```public class SpiralNumbers {   // your code would go in here }```
Much luck
• 11-17-2009, 05:19 AM
rendra
Quote:

Originally Posted by Fubarable
It would look something like this:
Code:

```public class SpiralNumbers {   // your code would go in here }```
Much luck

yeah,,, much look ... where is your code please ? :o
• 11-17-2009, 05:23 AM
Fubarable
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!
• 11-17-2009, 05:31 AM
rendra
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

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:
• 11-17-2009, 06:44 AM
collin389
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.
• 11-17-2009, 09:17 AM
rendra
Quote:

Originally Posted by collin389
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:
• 11-17-2009, 09:41 AM
r035198x
Don't access the index position that is not in the array?
• 11-17-2009, 03:16 PM
aaroncarpet
that was funny //your code would go here.....i like you fubarable you should have been a comedian....please post on my antivirus thread
• 11-18-2009, 02:16 AM
rendra
hehehehe,,, yeah ... i'm confused ...
• 11-18-2009, 07:57 AM
r035198x
Read a tutorial about arrays in Java.
• 11-18-2009, 07:20 PM
Arnold
Quote:

Originally Posted by rendra
So,,, what must i do ? :confused:

You now have this:
Code:

`for (int a=1;a<=userNo;a++)`
but you need something like this:
Code:

`for (int a=0;a<userNo;a++)`
Good luck.
• 11-20-2009, 04:20 AM
rendra
Hiahhh ... i've break it out 2 days ago ... humh ... I CAN ...
Owh ya ... for Arnold ... thanks ...

:)