0

Hi there, I'm new to this whole site and I was hoping to get some help from anyone with this question please. The question was given to us on a worksheet (and no it is not for an exam) however I have been unable to get into contact with my tutor and this question is really annoying me.

Q) Write a method using the method header below. This method will reverse the elements in an array between a lower index position and an upper index position.

So given the following array declaration

double [] data = {8.5, 12.0, 23.2, 18.0, 15.5, 5.0, 10.5};

following a call to the method Reverse{data, 2, 5); the contents of data would be

{8.5, 12.0, 5.0, 15.5, 18.0, 23.2, 10.5}

You have already written a method called swap that swaps two elements in an array, the elements identified by the two index values passed as parameters.

Swap(array, oneIndex, otherIndex)
public void Reverse( double [] vales, int start, int finish){

That is the question in full, exactly what it says in the same order. I was hoping that I might be able to receive some help on this question as I have having a massively hard time understanding it.
Thanks,
Danb

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jbisono
1

You need reverse the range given, this can be done by swapping pairs of numbers until the data is reversed. Using your example, you'd swap 2 and 5, 3 and 4 and then you would be done. The following snippet is what you need in your method.

while (start < finish) {
    Swap(values, start++, finish--);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.