Thread: reverse the order of an array

  1. #1
    Registered User
    Join Date
    Apr 2002

    Question reverse the order of an array

    Hello can anyone give me some advice on how to reverse the order of an array for example say in my array i had the values
    9 8 7 6 5 4 3 2 1... how can i get it to be
    1 2 3 4 5 6 7 8 9.

    i tryed
    for (x=sizeOfArray; x>=0; x--)
    { for (y=0; y<=sizeOfArray;y++)
    { tmp1[y] = tmp2 [x];
    where tmp2 is the array that holds the original values and tmp1 will store the reverse image...for some reason it is not working can any one confrim the code or assist me in correcting it.


  2. #2
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    pretty simple really. There are several ways to do this, one way would be to sort the array, another would be to swap values until it reaches the center. for example:

    temp_size = array_size - 1;
    for (int i = 0; i < array_size; i++)
        if (i != temp_size)
            {temp = a[i];
             a[i] = a[temp_size];
             a[temp_size] = temp;
    This is just a small untested example. You should probably experiment with it a bit, or use it as an example to build off of.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Two lines

    for (int i = sizeof(array)/sizeof(array[0])-1, int x = 0; i > -1; i--, x++)
    		reversearray[x] = array[i];
    However when I compiled this code the compiler complained(warning) about "nonstandard extension used" but from where I can see it is standard (maybe someone can confirm this). Anyway if you dont like this warning just move the first statement ( int i = sizeof(array)/sizeof(array[0])-1, int x = 0 ) before the for-loop and it should compile "correct".

  4. #4
    I don't think you need the 'y' for loop. I think this may work:

    for (x=sizeOfArray-1; x>=0; x--)
       tmp1[y] = tmp2 [x];
    I changed the 'x=' to 'x=sizeOfArray-1. If the size of the array is 10, the last element of it will be 9. I haven't compiled or tested this, but I think it may be o.k.

  5. #5
    Registered User
    Join Date
    Jan 2003
    one line
    ok, you need to include <algorithm>, but still.

    edit: the OP may have wanted
    assuming tmp2 is big enough
    Last edited by grib; 01-23-2003 at 11:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. read into array and spit out in reverse order
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-07-2001, 02:27 PM