0

I have this function:

void reverse(int* nums, unsigned int size)

This function is supposed to reverse the values in the array it is getting.

Now for reversing I thought to create another array with the size of the array passed in. Assigning this new one from the end of the original array to the start.

But I am a kind of new in C++, So I don't know how to create dynamic array in the size of the parameter of the function.

12
  • int* array = new int [size]; Commented Oct 24, 2017 at 22:02
  • Why do you need a second array? Reverse it in place using the original array. Commented Oct 24, 2017 at 22:02
  • 2
    If you need a dynamic array; Just use a std::vector Commented Oct 24, 2017 at 22:02
  • @Raindrop7 well that was rude ,but thank you for your help Commented Oct 24, 2017 at 22:03
  • Sorry you got that comment. That was out of line. We're happy to help you out as you're learning how to program! Commented Oct 24, 2017 at 22:03

4 Answers 4

2

It's actually not necessary to allocate a new array here. See if you can find a way to solve this problem just by rearranging the existing elements in-place.

Given that this seems like it's an exercise with pointers, you can allocate space by using the new[] operator:

int* auxiliaryArray = new int[size];

You'd then free it by writing

delete[] auxiliaryArray;

However, this isn't the preferred way of doing this in C++. The better route is to use std::vector, which does all its own memory management. That would look like this:

std::vector<int> auxSpace(size);

You can then access elements using the square brackets as you could in a real array. To do this, you'll need to #include <vector> at the top of your program.

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

Comments

0

In C++, the recommended way to create an array of variable size would be to use an std::vector

#include <vector>
void reverse(int* nums, unsigned int size)
{
    std::vector<int> V(size);
    ...
}

But that approach isn't the best here for performance because it requires additional memory to be allocated of the size of the array, which could be big. It would be better to start from the outside of the array and swap members one by one that are at mirroring positions (so if the size is 5, swap 0 and 4, then swap 1 and 3 and leave 2 alone). This only requires temporary storage of a single int.

2 Comments

If Asker can use vector, might as well use std::reverse
But if they used a vector, they would need to allocate it and then apply the reversed order to everything in the input array, because the signature is for in-place modification. Best not to use vector at all.
0

You can do it without the need to create another array:

void reverse(int* array, const int size){
    for(int i = 0; i < size / 2; i++){
        int tmp = array[i];
        array[i] = array[size - 1 - i];
        array[size - 1 - i] = tmp;
    }
}


int main(){
    int array[] = {1, 3, 5, 7, 9, 11};
    const int size = sizeof(array) / sizeof(array[0]);
    reverse(array, size);

    for(int i(0); i < size; i++)
        std::cout << array[i] << ", ";

}

As you can see above in the loop you only need to swap the first element (element 0) with the n-1 element and the second one with n-1-1 and son on...

Remember arrays are indexed from 0 through n-1.

If you want to allocate new array which is not practical:

int* reverse2(int* array, const int size){
    int* tmp = new int[size];

    for(int i(size - 1), j(0); j < size; j++, i--)
        tmp[j] = array[i];

        return tmp;
}


int main(){

    int array[] = {1, 3, 5, 7, 9, 11};

    for(int i(0); i < size; i++)
        std::cout << array[i] << ", ";

    std::cout << std::endl;

    int* newArray = reverse2(array, size);
    for(int i(0) ; i < size; i++)
        std::cout << newArray[i] << ", ";

    std::cout << std::endl;

    delete[] newArray;

    return 0;
}

Comments

0

If you want to use a new array you can, but I think is to kill flies with a cannon.

  1. Looks like you are using plain C code and not C++. I say that because of the signature of the function. The signature of the function in a common C++ code could be something like this other:

    void reverse(std::vector& items);

  2. You can reverse the current array without a new array, using the current one. You are passing the pointer to the first item of the array, and the content is not constant so that you can modify it. A better signature for the function could be:

    void reverse(int* const nums, const unsigned int size);

  3. Looks like a pointer problem. Think about the boundaries to iterate the positions of the array. Would you need to iterate the whole array? Maybe only half array? ;)

  4. As bonus track, what about to exchange the values without an auxiliar variable? (this is true into this case that we are using the fundamental type int... remember the binary arithmetic).


array[pos_head] ^= array[pos_tail];
array[pos_tail] ^= array[pos_head];
array[pos_head] ^= array[pos_tail];

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.