File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Find the heights between the distances of a walking trail. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Find the heights between the distances of a walking trail." Watch "Find the heights between the distances of a walking trail." New topic
Author

Find the heights between the distances of a walking trail.

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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.


When The Going Gets Tougher,The Tougher gets Going
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
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

Joined: Jul 01, 2008
Posts: 507

@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
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
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
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Actually, I counted wrongly. You have eleven is.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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

Joined: Jul 01, 2008
Posts: 507

If you can provide me with code fix it would be appreciated.
Im still confused....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Actually if you have a start and end, shouldn't the <= be changed to <
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find the heights between the distances of a walking trail.
 
Similar Threads
DataTable inside a DataTable
summing each row in a 2-dimensional array
method with an unknown number of parameters
Row Selection problems with JTable
Two dimensional array total and sort