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
Swap accepts oneIndex and otherIndex, not start and finish.
What you missed is the loop between start and finish, in which you have to call the swap method with each iterated number:
probably theres a way to remove the uneeded iteration where I checkked if the indexes are the same, but this is the basic concept.