Thread: How do you reverse an array?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    1

    How do you reverse an array?

    How do you reverse an array?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    search forum for reverse string assignments
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Assuming that the array a is a general type of array for which you know the length n, then exchange

    a[0] with a[n-1]
    a[1] with a[n-2]
    etc. If the truncated value of (n-1)/2 is N, then the last exchange is a[N] with a[n-1-N] (which just exchanges a[N] with itself if n is odd). You have to define a single temp variable to use in making each exchange.

    Edit: You can also use reverse() from the Standard Library. Just pass it pointers to the beginning and one past the end of the array.

    Edit: Ignore the previous edit. That's C++ only.
    Last edited by robatino; 03-29-2007 at 06:11 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by robatino View Post
    Edit: You can also use reverse() from the Standard Library. Just pass it pointers to the beginning and one past the end of the array.
    There is no reverse function in the standard C library.


    Quzah
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > There is no reverse function in the standard C library.
    Sorry, forgot which forum I was in.

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    There is no reverse function in the standard C library.
    What about strrev()?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Noir View Post
    What about strrev()?
    What about it? Non-standard. Furthermore, the OP doesn't mention anything about a string.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    It's not standard? I didn't know that. Thanks.

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I tried to do this without knowing the type of the array, but I'm not sure if it's the best way. Can you look at my code and tell me if I'm doing anything wrong?
    Code:
    void reverse( void *array, int nelem, int size ) {
      char *carray = array;
      int i = 0;
    
      if ( size == 1 ) {
        char temp;
    
        for ( ; i < --nelem; i++ ) {
          temp = carray[i];
          carray[i] = carray[nelem];
          carray[nelem] = temp;
        }
      } else {
    #define elem(i) ( carray + ( i * size ) )
        void *temp = malloc( size );
    
        for ( ; i < --nelem; i++ ) {
          memcpy( temp, elem( i ), size );
          memcpy( elem( i ), elem( nelem ), size );
          memcpy( elem( nelem ), temp, size );
        }
    
        free( temp );
    #undef elem
      }
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You shouldn't have to treat size == 1 as a separate case (except possibly for efficiency). Also, the malloc() cries out to be checked for NULL. It should be possible to avoid using it, perhaps by using a char for temp and copying size times? I'm not familiar with memcpy() or with endianness issues so I'm not sure whether it will work.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by robatino View Post
    You shouldn't have to treat size == 1 as a separate case (except possibly for efficiency). Also, the malloc() cries out to be checked for NULL. It should be possible to avoid using it, perhaps by using a char for temp and copying size times? I'm not familiar with memcpy() or with endianness issues so I'm not sure whether it will work.
    endian issues are not relevant here - bytes are copied in the same order independednt on the interpratation...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The special case is exactly for efficiency. I like the byte copying thing, I'll try that out.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would probably use alloca, rather than malloc, for something like this. Then you definitely don't need the special case.
    Or you can just copy a byte at a time, even though that might be a little slower than a memcpy.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    alloca() isn't standard either . . . .

    It can be done with a simple loop. It depends on what the OP wants.
    Code:
    void reverse_array(int *array, int number) {
        int x, t;
    
        for(x = 0; x < number; x ++, number --) {
            t = array[x];
            array[x] = array[number];
            array[number] = t;
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I was continuing with the "without knowing the type of the array" idea.
    If the type is known, I'd swap items as per dwks' code, just like any other sane person.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. Reverse Print Vector Array help
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 06:47 PM
  3. how do I reverse order of my array
    By jgonzales in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 03:48 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  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