spiral array

Showing error : Array out of bound

Page 1 of 1

5 Replies - 4198 Views - Last Post: 11 January 2009 - 09:38 PM Rate Topic: -----

#1 priya_lorha  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • 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  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 bastones  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is online

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • 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 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1