Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Find the heights between the distances of a walking trail.

Deepak Lal
Ranch Hand
Posts: 561
We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the sum of the changes for a walk beginning at the start index and ending at the end index. For example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1 + 5 = 6. The start end end index will both be valid indexes into the array with start <= end.

sumHeights({5, 3, 6, 7, 2}, 2, 4) => 6
sumHeights({5, 3, 6, 7, 2}, 0, 1) => 2
sumHeights({5, 3, 6, 7, 2}, 0, 4) => 11

My code is as below:

Could you let me know why im getting "Exception:java.lang.ArrayIndexOutOfBoundsException: 5 (line number:9)"
whats wrong with my code.Could you provide me with a fix for the same.

Campbell Ritchie
Marshal
Posts: 55735
163
• 1
You want to go through that and see how many ++ operators you are using. Also write down the index you are accessing, starting with 0, and you will doubtless see you are going off the end of the array.

Deepak Lal
Ranch Hand
Posts: 561
@Campbell,im unable to proceed further,Could you help me with the fix in the code that i have provided.i have checked the i++ operator with the previous index as well.As per the Question,The logic has been written,Could you help me fix this up for me.

Campbell Ritchie
Marshal
Posts: 55735
163
Deepak Lal wrote: . . . . ..
You have the variable i nine times in that code snippet. Write it on a piece of paper and write down an array like this: {5, 3, 6, 7, 2}. That is one of your arrays from your earlier post. Now get a different coloured pencil and write the indices against the nine s numbers in that array.
Now get the same pencil and write the values of i as you go through that loop, starting at 3. Now get another colour of pencil and write against each occurrence of i which index in the array you are attempting to use. The value of i and the index are not necessarily the same. When you have done that, it will be obvious what has gone wrong.

Campbell Ritchie
Marshal
Posts: 55735
163
Actually, I counted wrongly. You have eleven is.

Deepak Lal
Ranch Hand
Posts: 561
Campbell,im still not getting it.could you help me to fix it up.Im a bit confused with this problem and struggling to get it right.
if you can provide me with code fix it would be appreciated.

Deepak Lal
Ranch Hand
Posts: 561
If you can provide me with code fix it would be appreciated.
Im still confused....

Rob Spoor
Sheriff
Posts: 21092
85
Get rid of all those "i++" statements inside the loop, and replace them with "i + 1". The latter is only a value - the current value of "i" plus one. The former is a statement that modifies "i" and returns its old value. In your code "i" is increased 2 or 3 times within the loop body, then again in the for-loop increment.

So change it to this:
There is one easier way to do this though:
Math.abs turns a negative value into a positive value, so if height[i+1] > height[i] the subtraction returns a negative value which then gets turned positive again. For any two random values "x" and "y", Math.abs(x - y) == Math.abs(y - x).

Campbell Ritchie
Marshal
Posts: 55735
163
Actually if you have a start and end, shouldn't the <= be changed to <