0

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

3
  • 3
    No, Swap swaps just two elements, not the whole sequence. In your example you should run Swap(data,2,5) and Swap(data,3,4). I don't want to give you the solution, but just an idea: if you divide sequence length by two and run Swap for every item in this array using correct indexes... Commented Jun 2, 2012 at 14:27
  • @Marco, when I read question again I actually get the idea, it actually swaps the specified range rather than 2 values. So I assume a do while would fix it? PS: I updated my answer now. Commented Jun 2, 2012 at 14:33
  • Your answer looks correct. Why not add some command line outputs, to see an array before and after. to check its working. Commented Jun 2, 2012 at 14:41

3 Answers 3

2

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:

int iterationsNum = (finish - start) / 2 ;
for(int i=0;i<=iterationsNum;i++)
{
    if(start+i != finish-i)
    {
      swap(values, start+ i, finish-i);
    }
}

probably theres a way to remove the uneeded iteration where I checkked if the indexes are the same, but this is the basic concept.

Sign up to request clarification or add additional context in comments.

1 Comment

can you check my current one?
0

If you will work a little bit on paranthesis within your function and remove ints from function call it will work.

public static void Swap(double[] values, int firstIndex, int secondIndex)
{
    double temp = values[start];
    values[start] = values[finish];
    values[finish] = temp;
}

public static void Reverse( double [] values, int start, int finish)
{
    do
    {
        Swap(values, start, finish);
        start++;
        finish--;
    }
    while (start < finish);
}

static void Main(string[] args)
{
    double[] data = { 8.5, 12.0, 23.2, 18.0, 15.5, 5.0, 10.5 };
    Reverse(data, 2, 5);
    foreach (double number in data)
        Console.Write(number.ToString() + ", ");
    Console.ReadKey();
}

Gives:

8.5, 12, 5, 15.5, 18, 23.2, 10.5,

Comments

0

Heres two more possible answers:

    static void Reverse(int[] values, int start, int finish)
    {
        int end = finish;
        for (int i = start; i < end ; i++)
        {
            Swap(values, i, end);
            end--;
        }
    }

    static void Reverse2(int[] values, int start, int finish)
    {
        int upper = finish;
        int lower = start;

        while (upper > lower)
        {
            Swap(values, lower, upper);
            lower++;
            upper--;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.