# Bounds problem...

• 10-21-2010, 05:27 PM
licka
[solved]Bounds problem...
So I'm being told that I have a bounds problem and I know why, but I can't figure out how to set appropriate bounds. I'm trying to do a steady state temperature distribution, but I'm unsure on how to actually create bounds which work. I know I need one for the center points, one for the boarder points, and one for the corners. Anyone think they could help me with this?

Code:

```int length = 20; int width = 20; double hotplate[] [] = new double[length] [width];     ((Each of the hotplate arrays have been filled...)) for (int row = 1; row < length; row++){         for (int column = 1; column < width; column++){                 int left = row + 1;                 int right = row - 1;                 int up = column + 1;                 int down = column - 1;                 hotplate[row] [column] = ((left + right + up + down)/4);                 hotplate[row] [column] = ((hotplate[left] [column] + hotplate[right] [column] +hotplate[row] [up]+hotplate[row] [down])/4);                 System.out.print("|"+hotplate[row] [column]+"|");                 }         System.out.println(""); }```
• 10-21-2010, 06:01 PM
curmudgeon
You might need to tell us a bit more; in fact, a whole lot more -- what problem are you trying to solve, what is wrong with your current code, etc....
• 10-21-2010, 06:11 PM
licka
I'm taking a 2D array (20x20) with all the values set to 0 except for the top and bottom line which have 100's in each of the holders. I'm trying to take an array (say array[2] [2]) and changing it to the average of the array directly above it, below it, and on either side of it. I'm doing this until no cell in the array changes more than 0.1 degree. So what I need help with if figuring out how to create bounds for the outter edges, corners, and interior calculations... My actual error is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
• 10-21-2010, 07:17 PM
pbrockway2
Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20

Code:

```for (int row = 1; row < length; row++){         for (int column = 1; column < width; column++){                 ...                 hotplate[row] [column] = ...```

Array indices in Java are zero based: ie if I have an array of length 7, the array indices run from zero to six.

See Arrays in Oracle's Tutorial.
• 10-21-2010, 07:34 PM
licka
Quote:

Originally Posted by pbrockway2
Array indices in Java are zero based: ie if I have an array of length 7, the array indices run from zero to six.

See Arrays in Oracle's Tutorial.

I've actually read that quite a bit :D I'm glad to know I was at least looking in the right place.

I'm aware that the values for a 20x20 array would be 0-19. I have row = 1 and column = 1 because I don't want it to try reading the 0 values.
Code:

```0  100  100  100  100  100  0 0    0    0    0    0    0  0 0    0    0    0    0    0  0 0    0    0    0    0    0  0 0    0    0    0    0    0  0 0  100  100  100  100  100  0```
So rather than reading 0, it reads the 1. It was an attempt to avoid part of my border problem. If I had it at 0 my left and up would have problems.
• 10-21-2010, 08:11 PM
pbrockway2
OK - Sorry, I wasn't reading closely enough. You take care of one end by starting at index 1, but there is still a problem at the other end where you use indices left and up which are one greater than the index values set by the loop.

Code:

```0  100  100  100  100  100  0 0    *    *    *    *    *  0 0    *    *    *    *    *  0 0    *    *    *    *    *  0 0    *    *    *    *    *  0 0    *    *    *    *    *  0 0  100  100  100  100  100  0```
In the case of a 7x7 plate only the cells marked * can have their new value set as an average of their neighbours. Ie they form a 5x5 collection. In the general case wouldn't this correspond to something like the folllowing?:

Code:

```for (int row = 1; row < length-1; row++){         for (int column = 1; column < width-1; column++){```

(Also you might like to confirm that the new value is the average of the 4 neighbours - or describe what the rule is.)
• 10-21-2010, 09:38 PM
licka
Thank you for that! I tried setting a while loop for row being less than 19 and column being less than 19, then I tried to simply place a fixed number where length and width were and that wasn't working either. (Though I don't know the latter didn't work :confused: ) But I didn't even think of having an equation being equal to row and column. That's something I'll have to remember. Thank you so much for showing me that you can use equations on a greater than or less than problem! :)