How do you reverse an array?

This is a discussion on How do you reverse an array? within the C Programming forums, part of the General Programming Boards category; 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    search forum for reverse string assignments
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,295
    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,046
    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,295
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  2. Reverse Print Vector Array help
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21