# implementing jacobi algorithm to implement laplace equation

sanjay ramaswamy
Greenhorn
Posts: 29
Hi folks ,

I have been trying to implement this algorithm and have been stuck up on with a error i.e Array index out of bounds exception.

The Algorithm traverses a 2D NxN array, making every element the average of its 4 surrounding neighbors (left, right, top, down).

The NxN array has initially all zeros and is surrounded by a margin with all 1’s as shown in the example below. The 1’s never change, and the 0’s increase little by little.

1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1

Jaikiran Pai
Marshal
Posts: 10447
227
"Desi Hotty"

Sean Clark
Rancher
Posts: 377
Hey,

I tried your code above and I didn't get any exceptions happening, it seemed to terminate successfully. Is this the exact code that you are using?

Not so important but there are a couple of things on code conventions you may want to look at as it makes your code more standard and easier to understand:
• All capitals are usually used for private static final variables and camel case for standard variables. e.g.

• Method names should have only letters and numbers in them and should be camel case. e.g.

• Finally all class names should begin with an uppercase with camelcase. e.g.

• Sean

Mike Simmons
Ranch Hand
Posts: 3090
14
If an array in Java has N elements, the index of the first element is 0, and the index of the last element is N-1. You seem to be starting at 1 and going up to, well, various numbers. But I see several loops that go to N, and some others that might go higher, but I can't tell. Anyway, trying to access element N will cause an ArrayIndexOutOfBounds, because there is no element N.

Also, it would be helpful if you look carefully at the stack trace - in particular, at the topmost line of the stack trace. There's a line number there, which is very useful in determining which line caused the exception. If my hints above don't solve your problem, please tell us exactly which line threw the exception. For that matter, the exact full text of the exception may also be helpful as well. But remember that we can't look at a line number and tell which line it is, because we don't have the complete file. You need to look at the exception and figure out what it means.

sanjay ramaswamy
Greenhorn
Posts: 29
Hey Sean,

I am surprised that it terminated sucessfully without giving you an exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at algorithm.Main.main(Main.java:24)
"

if i want to debug the execution then what are the steps that i need to take to do it...where could the problem be ?

sanjay ramaswamy
Greenhorn
Posts: 29
Awesome Mike...you were spot on !!! I am testing the code and will post the update tommorow for all to see . thank you so much

Sean Clark
Rancher
Posts: 377
Hey,

I know why I didn't get an exception, my execution of the program finished by thread 7 so therefore never got to the last thread where the problem is.

Your problem as Mike said is likely to do with your for loops in the create_threads() method.

The first element in an array is 0 and the last is n-1.

Sean

sanjay ramaswamy
Greenhorn
Posts: 29
Hi ,
you guys gave some awesome suggestions , so thank you first . but i have a multithreading problem in it and that is when i am spawning new threads only one thread is going into the monitor and finishing the work (in case of 2 threads ) but when you have more than 4 or 8 threads then 1 or 2 threads among them never do the job . the answer that is generated should be the same for any number of threads that i use but i am getting different answer each time when different number of threads that i use . where am i doing it wrong ???