" double maximumDistance(double[][] pts) that is given a two-dimensional array pts that is guaranteed to have at least two rows, and each row is guaranteed to have exactly three elements. Each row of this array pts gives the (x, y, z) spatial coordinates of a point in the three-dimensional space. This method should find the two points that have the maximum distance from each other, and return this distance.
To calculate the distance between two points p1 and p2 given as double[], use the method
And here's my code for the question.
My tester has revealed there is an error
53
java.lang.ArrayIndexOutOfBoundsException: 53 at TwoDeeArrayProblems.maximumDistance(TwoDeeArrayProblems.java:98) at TestTwoDeeArrayProblems.testMaximumDistance(TestTwoDeeArrayProblems.java:97)
the line it's referring to is
I'm at a loss at what to do nd i was wondering if i can ask for some assistance. Maybe i inputted something wrong?
'i' goes from 1 to rows-1. The pts array has rows 0 to rows-1. On line 31 you are adding 1 to the row so that would be 2 to rows. The index must be less than 'rows' so you exceed the limit.
This double[][] is very confusing. Can't you create a class MyPoint containing a double[], so that you can transform that double[][] into a one dim array of MyPoints? That would simplify the code.
You can leave out the Math.sqrt from the distance method. You only need to take the sqrt of the max distance you found (why?)
There are three kinds of actuaries: those who can count, and those who can't.
You can do away with all your temp variables, pts[n] returns an array of three elements (x,y,z). So, you'd end up with
Like wise for the code in your loop.
To compare all possible combinations of pts rows you'll need to revise this loop. Note that distance(A,B) is the same as distance(B,A), so don't test the same combination of rows.