last day (15 days later) » 

04:57
-3
Q: How to inverse an array without forming a new one in C++

Raunaq SinghI wanted to write a function that would inverse an array and would print the inverse permutation of the array without forming a new one. Given an array of size n of integers in range from 1 to n, we need to find the inverse permutation of that array. An inverse permutation is a permutati...

Swap the first and last element, then the second and second-to-last, and so forth.
"without forming a new one." ... "but I have to form a new array in order to do that" You don't actually form a new one? I don't get your question?
1 5 4 3 2 is this what you want?
Your code produces 5 1 2 3 4. How is that an inverse of your array?
@dbush That would be the reverse of the array, but it's not what the current code does.
"I wrote a code that can inverse an array" This code will not reverse an array. Well, it might reverse a very specific array.
04:57
int arr2[size]; is a variable length array, and is not legal by standard C++.
I don't think you can do what you're currently doing without creating a new array, at least temporarily. You can't modify the original array while it holds the indexes you need in the result.
@πάνταῥεῖ Are you sure your edit is correct? You changed "inverse" to "reverse", but it's not at all clear that's really what he wants. He says this code works, except that it uses a new array. It doesn't reverse the array.
I mean inverse. By mistake, I wrote reverse.
What do you mean by inverse? Give us an example of the input and expected output.
@Barmar The correct output would be 5 1 2 3 4. You are the only one who has understood the question. Rest everyone is reversing the array
I didn't really understand, that's just what your code produces. You need to explain what it's supposed to do. It's just printing the array indexes of the numbers from 1 to size.
04:57
So by "inverse" you really mean "shift the array to the right twice"?
What you can do is loop from 1 to size, and call std::find() to get its index. Then print the index + 1.
@Barmar But I want to store the inverted array in an array and not just print out the values. I have to modify the original array.
As I said earlier, I don't think you can do that without a temporary array.
@Barmar Thanks for the edit
@Barmar That is my question. How can I do it without a temporary array?
04:57
I don't think you can. Is there some reason you think it's actually possible? Is it a programming challenge?
@Barmar Yes. Take as input N, the size of array. Take N more inputs and store that in an array.Write a function that inverses the array. Print the values in inverted array
Where does that say it has to be done without a temporary array?
The language of the question made me think that way. I don't think it is possible, because if we modify the values in the array we lose them and can't use them. We have to do it using a temporary array.
After you assign to arr2, copy it back to arr and it will update the original array.
That is not correct. We still have used a temporary array.

last day (15 days later) »