• Advertisement
Sign in to follow this  

Array Reversal

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Reading comprehension!!!

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 this post


Link to post
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 this post


Link to post
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 array
X array[ARRAY_SIZE];

for ( int i = 0; i < n; i++ ) {
// forward traversal
array[i]...
}

for ( int i = n-1; i >= 0; i-- ) {
// reverse traversal
array[i]...
}


Share this post


Link to post
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[i];
your_array[i] = 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 this post


Link to post
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 this post


Link to post
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[i]+=ara[n-1-i];
ara[n-1-i]=ara[i]-ara[n-1-i];
ara[i]-=ara[n-1-i];
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement