Print Array in reverse order

This is a discussion on Print Array in reverse order within the C++ Programming forums, part of the General Programming Boards category; Hey Guys I am trying to read into 10 integers then print them out in reverse order using a simple ...

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

    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
    }
    I'm just trying to be a better person - My Name Is Earl

  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,475
    I think i considered ARRAY_SIZE to be the starting value of 10. Maybe that is my error.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,475
    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
    I'm just trying to be a better person - My Name Is Earl

  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, 06:31 PM
  3. reverse order - arrays
    By Lillian in forum C Programming
    Replies: 12
    Last Post: 11-03-2002, 06: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

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