Thread: Print Array in reverse order

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Print Array in reverse order

    Hey Guys

    I am trying to read into 10 integers then print them out
    in reverse order using a simple one-dimensional array.
    The below code compiles and does work but I am getting a garbage value before the output.

    So say I entered 1 - 10 my ouput is: 125214 10 9 8 7 6 5 4 3 2 1

    I am wondering why the program is generating this value
    as I am not overstepping the bounds of the array. Any help appreciaited.

    Code:
    #include <iostream>
    
    // main - begins program execution /////////////////////////////////////////////
    //
    int main(int argc, char *argv[])
    {
       const int ARRAY_SIZE = 10;
    
       int data[ ARRAY_SIZE ];
    
       std::cout << "Enter ten integers: ";
    
       for ( int i = 0; i < ARRAY_SIZE; i++ )
       {
          std::cin >> data[ i ];
       }
    
       std::cout << "\nIn reverse order they are:\n\n";
    
       for ( int i = ARRAY_SIZE; i >= 0; i-- )
       {
          std::cout << data[ i ] << " ";
       }
    
       std::cin.get(); // freeze console output window
       std::cin.ignore();
    
       return 0; // return value from main to OS
    }
    Double Helix STL

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Where does this for-loop start
    Code:
       for ( int i = ARRAY_SIZE; i >= 0; i-- )
    and how does this relate to the output of:
    125214 10 9 8 7 6 5 4 3 2 1

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I think i considered ARRAY_SIZE to be the starting value of 10. Maybe that is my error.
    Double Helix STL

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    as I am not overstepping the bounds of the array.
    But you are!

    I think your mistake is just that your forgot that in the typical forward iteration:
    Code:
       for ( int i = 0; i < ARRAY_SIZE; i++ )
       {
          std::cout << data[ i ] << " ";
       }
    The array index i is never used to index data since the loop exits the moment i = ARRAY_SIZE. However, in your reverse iteration, i indexes data on the first iteration, thus overstepping the bounds 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

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Managed to solve it. Thanks for your help guys

    Code:
    for ( int i = ARRAY_SIZE - 1; i >= 0; i-- )
    I forgot the -1 after ARRAY_SIZE thus preventing the array over-runing
    Double Helix STL

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    Managed to solve it. Thanks for your help guys

    Code:
    for ( int i = ARRAY_SIZE - 1; i >= 0; i-- )
    I forgot the -1 after ARRAY_SIZE thus preventing the array over-runing
    Yes, a simple mistake that I think almost ALL of us has done at one point or another.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Another way to fix the problem is to use
    Code:
    for (int i=ARRAY_SIZE; i > 0;) {
      i--;
      // stuff
    }
    moving the decrement out of the for loop and into the start of the loop body. This would be necessary if i was an unsigned type, since "i >= 0" would always be true. The same method also works with reverse pointer/iterator loops in ensuring that an invalid pointer is never assigned, which would generate undefined behavior (it's not enough to simply avoid dereferencing it). Of course in the latter case one can just use a reverse iterator, but whoever implements it needs to be aware of this issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings in reverse order
    By cakestler in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 04:32 PM
  2. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  3. reverse order - arrays
    By Lillian in forum C Programming
    Replies: 12
    Last Post: 11-03-2002, 07:38 PM
  4. how do I reverse order of my array
    By jgonzales in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 03: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