# spiral array

Page 1 of 1

## 5 Replies - 4307 Views - Last Post: 11 January 2009 - 09:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=80339&amp;s=5ce95b52e3047b60820922884e2a786d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 priya_lorha

Reputation: 6
• Posts: 81
• Joined: 02-August 08

# spiral array

Posted 11 January 2009 - 08:09 AM

```public class Spiral {
public static void main(String[] args) {
int  i=1, n = 1,s = 4, x=0, y=0;
int m [][]= new int [4][4];

while(i>=1)
{for(int j=0; j<s; j++)
m[x][y++] = i++;
x++;
if(i>=1){for(int j=0; j<s-1; j++)
m[x++][y] = i++;
x--;y--;
}
if(i>=1){for(int j=0; j<s-1; j++)
m[x][y--] = i++;
x--;y++;
}
if(i>=1){for(int j=0; j<s-2; j++)
m[x--][y] = i++;
y++;x++;
s=s-2;
}
}

//print
for(i=0; i<n; i++){
for(int j=0; j<n; j++)
System.out.print(m[i][j] + "  ");System.out.println("\n");

}

}

}
```

Is This A Good Question/Topic? 0

## Replies To: spiral array

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: spiral array

Posted 11 January 2009 - 08:24 AM

Although I didn't spend much time going through the code and what could happen, I'm guessing that some index either reaches below 0 or above 3 causing some ArrayOutOfBoundsException

### #3 bastones

Reputation: 5
• Posts: 35
• Joined: 03-January 09

## Re: spiral array

Posted 11 January 2009 - 08:30 AM

I'm told that you get an ArrayOutOfBoundsException error when the array reaches an index such as arrayVar[5] which may not exist. Remember arrays in programming languages start at index 0.

### #4 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: spiral array

Posted 11 January 2009 - 09:51 AM

Quote

while(i>=1)
{for(int j=0; j<s; j++)
m[x][y++] = i++;
x++;
if(i>=1){for(int j=0; j<s-1; j++)
m[x++][y] = i++;
x--;y--;
}
}

Well the array out of bounds is because in the first for you do y++ each time through, including the final time, which will bring the index to 4, and thus when you try to access it next will be out of bounds

**Meant to say y not x

This post has been edited by BigAnt: 11 January 2009 - 09:54 AM

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: spiral array

Posted 11 January 2009 - 08:38 PM

Never though of using meningfull name for your variables ?
trying to figure out what i, j, n, s, x, y are are a pain in the heck

Can figure out for x and y but for the others....

This post has been edited by pbl: 11 January 2009 - 08:39 PM

### #6 grimpirate

• Pirate King

Reputation: 149
• Posts: 717
• Joined: 03-August 06

## Re: spiral array

Posted 11 January 2009 - 09:38 PM

Don't know if this will help you as it's in another language, namely PHP. However, algorithmically this produces a spiral array. Assuming we both have the same definition for a spiral array.
```<pre>
<?php
\$rowlowlim = 0;
\$rowupplim = 15;
\$collowlim = 0;
\$colupplim = 15;
\$h = 0;
do{
for(\$j = \$collowlim; \$j <= \$colupplim; \$j++){
\$spiral[\$rowlowlim][\$j] = '' . dechex(\$h++);
if(\$h > 15){
\$h = 0;
}
}
\$rowlowlim++;
for(\$i = \$rowlowlim; \$i <= \$rowupplim; \$i++){
\$spiral[\$i][\$colupplim] = '' . dechex(\$h++);
if(\$h > 15){
\$h = 0;
}
}
\$colupplim--;
for(\$j = \$colupplim; \$j >= \$collowlim ; \$j--){
\$spiral[\$rowupplim][\$j] = '' . dechex(\$h++);
if(\$h > 15){
\$h = 0;
}
}
\$rowupplim--;
for(\$i = \$rowupplim; \$i >= \$rowlowlim; \$i--){
\$spiral[\$i][\$collowlim] = '' . dechex(\$h++);
if(\$h > 15){
\$h = 0;
}
}
\$collowlim++;
}while(\$rowlowlim < 8);
for(\$i = 0; \$i < sizeof(\$spiral); \$i++){
for(\$j = 0; \$j < sizeof(\$spiral); \$j++){
echo \$spiral[\$i][\$j] . ' ';
}
echo "\n";
}
?>
</pre>
```
It produces the following output:
```0 1 2 3 4 5 6 7 8 9 a b c d e f
b c d e f 0 1 2 3 4 5 6 7 8 9 0
a f 0 1 2 3 4 5 6 7 8 9 a b a 1
9 e b c d e f 0 1 2 3 4 5 c b 2
8 d a f 0 1 2 3 4 5 6 7 6 d c 3
7 c 9 e b c d e f 0 1 8 7 e d 4
6 b 8 d a f 0 1 2 3 2 9 8 f e 5
5 a 7 c 9 e b c d 4 3 a 9 0 f 6
4 9 6 b 8 d a f e 5 4 b a 1 0 7
3 8 5 a 7 c 9 8 7 6 5 c b 2 1 8
2 7 4 9 6 b a 9 8 7 6 d c 3 2 9
1 6 3 8 5 4 3 2 1 0 f e d 4 3 a
0 5 2 7 6 5 4 3 2 1 0 f e 5 4 b
f 4 1 0 f e d c b a 9 8 7 6 5 c
e 3 2 1 0 f e d c b a 9 8 7 6 d
d c b a 9 8 7 6 5 4 3 2 1 0 f e
```