• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find the heights between the distances of a walking trail.

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I counted wrongly. You have eleven is.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can provide me with code fix it would be appreciated.
Im still confused....
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually if you have a start and end, shouldn't the <= be changed to <
 
reply
    Bookmark Topic Watch Topic
  • New Topic