Thread: Reverse contents of array

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    Reverse contents of array

    I found this algorithm to reverse the contents of an array-
    Code:
    int len=strlen(word);
        for (int i=0;i<len/2;i++)
        {
            word[i]^=word[len-i-1];
            word[len-i-1]^=word[i];
            word[i]^=word[len-i-1];
        }
    But I really don't understand how it works. It looks as though the loop only traverses half of the array. If anyone could lend some insight I would appreciate it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What does the code in the loop body do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    It is supposed to reverse the contents of an array, that is take 4689 and turn it into 9864, using XOR operations.

    I found the algorithm here
    http://stackoverflow.com/questions/1...129028#1129028

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Make word an array of doubles, or pointers, or structs, and see how far it gets you.
    Question 3.3b
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Yes, but I am asking you specifically about the loop body, i.e.,
    Code:
    word[i]^=word[len-i-1];
    word[len-i-1]^=word[i];
    word[i]^=word[len-i-1];
    That said, now that I took a good look myself, I think you should examine this example instead:
    Code:
    int len = strlen(word);
    for (int i = 0; i < len / 2; i++)
    {
        char temp = word[i];
        word[i] = word[len - i - 1];
        word[len - i - 1] = temp;
    }
    The same question applies: what does the loop body do? Once you answer this correctly, you are on your way to answering your own question of why "the loop only traverses half of the array".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    I figured it out; were swapping the first and last elements, then second and second to last element, etc. which means that we only need to go half way through the array to reverse the elements.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Yes, that is correct
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  2. protecting array contents
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 01-22-2010, 10:23 AM
  3. comparing array contents
    By ReLiEnThAwK in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2006, 07:52 AM
  4. Adding array contents
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:26 AM
  5. contents of array
    By Sway in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 06:26 PM

Tags for this Thread