# Array Reversal

This topic is 4112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How would you take an array of ints and write them backwards: From: 5 7 9 2 0 | 1 | 2 | 3 To: 2 9 7 5 0 | 1 | 2 | 3 Thanks

##### Share on other sites
Most languages have this in their standard library. For instance, C++ has std::reverse.

##### Share on other sites
Just iterate till the middle of the array and switch the elements with their corresponding elements of the end.

Example:

first step: switch 5 and 2
second step: switch 7 and 9

and so on...

##### Share on other sites

Quote:
 How would you take an array of ints and write them backwards:

Why do extra O(n) step, when we have an array.

##### Share on other sites
Quote:
 Why do extra O(n) step, when we have an array.

My array may or not be full and with what I need to do with it it will be MUCH easier if I have them in reverse order.

My confusion comes in because the array might, and probably often will not be full.

##### Share on other sites
Quote:
Original post by Hammonjj
Quote:
 Why do extra O(n) step, when we have an array.

My array may or not be full and with what I need to do with it it will be MUCH easier if I have them in reverse order.

My confusion comes in because the array might, and probably often will not be full.

Indeed, why an extra step:
int ARRAY_SIZE = 1000;int n= 77; // actual number of elements in arrayX array[ARRAY_SIZE];for ( int i = 0; i < n; i++ ) {  // forward traversal  array...}for ( int i = n-1; i >= 0; i-- ) {  // reverse traversal  array...}

##### Share on other sites
And if you really, really need to reverse it... you couldn't think to do this yourself:

for (int i = 0; i < your_array_size / 2; ++i){    int temp = your_array;    your_array = your_array[your_array_size - i - 1];    your_array[your_array_size - i - 1] = temp;}

Of course, std::reverse will do this in a generic way for you. I mean, it's already there. Use it!

// note: I've never used std::reverse, but my guess would be this:std::reverse(your_array, your_array + your_array_size);

Hope that helps!

##### Share on other sites
/** * Program that takes an array of ints and writes it backwards. */#include <algorithm>#include <iostream>#include <iterator>using namespace std;int array[] = { 5, 7, 9, 2 };int main(){  reverse_copy(array,	       array + sizeof(array) / sizeof(int),               ostream_iterator<int>(cout, " "));  cout << "\n";}

##### Share on other sites
This is a fun little exercise, heh, here's my function:

void areversal(int *ara, int n){        int i;        for(i=0; i<n/2; i++)        {                ara+=ara[n-1-i];                ara[n-1-i]=ara-ara[n-1-i];                ara-=ara[n-1-i];        }}

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009410
• ### Who's Online (See full list)

There are no registered users currently online

×