So the exam question is this:
Write a method using the method header below.
public void Reverse( double [] values, int start, int finish){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}Assume that 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)
I answered it like this:
public void Reverse( double [] values, int start, int finish){
do {
Swap(values, int start, int finish);
}
start++;
finish--;
}
while (start < finish)
I think my answer is not correct but I cannot think of anything else. Swap() method already does everything. Anyone can correct me? Thanks
Swapswaps just two elements, not the whole sequence. In your example you should runSwap(data,2,5)andSwap(data,3,4). I don't want to give you the solution, but just an idea: if you divide sequence length by two and runSwapfor every item in this array using correct indexes...