0

Write a reverse function that takes an integer array and its length as arguments. Your function should reverse the contents of the array, leaving the reversed values in the original array, and return nothing.

 #include<iostream>

    using namespace std;

    void printArray(int a[], const int n)
    {
        for(int i=0;i<n;i++)
        {
            cout<<a[i];
            i!=n-1 ? cout<<", " : cout<<"";
        }
    }

    void reverse(int a[], const int n)
    {
        int reverse[n];
        for(int i=0;i<n;i++)
        {
            reverse[n-1-i]=a[i];
        }
        a = reverse;
    }

    int main()
    {
        int *a,n;
        cin>>n;
        a = new int[n];
        for(int i=0;i<n;i++)
            a[i]=0;
        a[0]=1;
        reverse(a,n);
        printArray(a,n);
        delete [] a;
        a = NULL;
        return 0;
    }

After calling reverse function the array from main is not modifying, please advice! :(

5
  • 3
    You have to pass the array by reference, not by value. Commented Sep 26, 2013 at 16:27
  • @Jamal but also avoid the memory leak and potentially referencing a dead object Commented Sep 26, 2013 at 16:28
  • @vlad: Oh yeah, new is used here as well. I missed that. Commented Sep 26, 2013 at 16:28
  • 1
    Worded very much like a homework assignment to me! Commented Sep 26, 2013 at 16:32
  • 1
    @Skizz: I suppose that also explains why using namespace std is present. Commented Sep 26, 2013 at 16:34

4 Answers 4

5

You can't assign one array to another. Instead copy from reverse to back into a:

std::copy(reverse, reverse + n, a);

Or possibly

memcpy(a, reverse, n * sizeof(int));
Sign up to request clarification or add additional context in comments.

Comments

4

You are not copying the data from reverse back to a - you are instead pointing it (a) to a memory location that will no longer exist (be valid) after your function returns. You need to copy the values from reverse back to a. And I would recommend not using the same name for a function and a variable.

Try

void reverse(int a[], const int n)
{
    int reverse[n];
    for(int i=0;i<n;i++)
    {
        reverse[n-1-i]=a[i];
    }
    for(int i=0;i<n;i++)
    {
        a[i]=reverse[i];
    }
}

As was pointed out in comments, the above shows one way of getting the reversed data into array a. It is not the only way - memcpy is considered a more efficient function to use. Even more efficient would be to do in place reversal - this would require a loop of just n/2 iterations while the above loops for 2n and is thus about 4x less efficient.

I recommend that you study all the answers provided - they highlight different aspects of memory handling, code efficiency etc.; something to learn from all of them.

4 Comments

Or with a single for-loop: for (int i = 0; i < n / 2; ++i) { std::swap(a[i], a[n - i - 1]); }
From the way the question is worded, it sounds like they're looking for an in-place reversal of the elements, but that could just be me. Still, in-place is surely preferable to allocate and copy?
I agree that in-place reversal would be preferable. I was trying to "change as little code as possible" since OP seems to be trying to understand "what is wrong with a = reverse;?" rather than "do my homework for me so I get an A". @ZacHowland's method is obviously more efficient.
@Floris: Understood ... I was just mentioning it for completeness. It would be beneficial for the OP to understand that his solution effectively requires iterating through the array 2 * N times ... while the swap method only requires iterating through the array N / 2 times.
1

Pointers! They're really useful.

void reverse (int *a, const size_t n)
{
  int *b = a + n - 1;
  while (b > a)
  {
    const int swap_value = *a;
    *a = *b;
    *b = swap_value;
    ++a;
    --b;
  }
}

1 Comment

Bonus points if you modify it to only require a single increment per iteration instead of an increment and decrement ;)
0

Aha, you know you should pass int a[], a pointer to a, to reverse(), but you still encounter the same problem. You can't modify the pointer stored in a, unless you pass &a to reverse().

2 Comments

And even if he did - that memory location will not be valid after the function returns.
Yeah, it's stack memory

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.