While Loops with Vector Arrays in C++

Discussion in 'Mac Programming' started by sheepopo39, Dec 10, 2009.

  1. sheepopo39 macrumors 6502

    Joined:
    Sep 18, 2008
    #1
    I was working through one of the examples in my book on C++ with dealing with while loops. I understand the concept of the while loops, however I don't get the example, I also understand vector arrays, which were taught earlier.

    The code is

    Code:
    #include <vector>
    #include <iostream>
    using namespace std ;
    
    
    // A C++ application demonstrating the while loop
    
    
    int main()
    {
    	vector <int> vec( 10 ) ;
    	int i = 0 ;
    	
    	while ( i < vec.size() ) 
    	{
    		i++ ;
    		vec[ i-1 ] = i ;
    		cout << " | " << vec.at( i - 1 ) ;
    	}
    	return 0;
    }
    The output is " | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10" as it shows in the book. However I don't understand the one part where you get the array of vec[i-1] (which would be equal to zero. Then it outputs the element at "i - 1" in the array, but that would equal -1, and not 1. I simply do not understand how that output was created. Even though it turns out right. Shouldn't it be in the negatives instead?

    Edit: I tried going through it step by step, and I just get stuck, the value of i goes into the negatives, so shouldn't the output be in positives and not negatives. I just have no idea what to do. It makes absolutely no sense to me.

    Thanks.

    John
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Your analysis is wrong.

    Code:
    	vector <int> vec( 10 ) ;
    
    This creates a vector of ints, containing 10 elements.

    vec.size() is 10 at this point. It remains 10 for the entire loop and the full extent of main().

    http://en.wikipedia.org/wiki/Vector_(C++)

    http://www.cppreference.com/wiki/stl/vector/vector_constructors

    As a diagnostic aid, I recommend displaying the value of vec.size() at this point, before the loop begins.


    Code:
    	int i = 0 ;
    	
    	while ( i < vec.size() ) 
    	{
    		i++ ;
    
    At this point, in the first iteration of the loop, i is 1.

    As a diagnostic aid, I recommend displaying the value of i before and after the i++. Or learn how to use the debugger to step through your code one statement at a time.

    Code:
    		vec[ i-1 ] = i ;
    
    vec[ 0 ] = 1;

    Code:
    		cout << " | " << vec.at( i - 1 ) ;
    
    cout << " | " << vec.at( 0 );


    During the second iteration:
    Code:
    		i++ ;
    
    The previous value of i was 1. i is now 2.

    Code:
    		vec[ i-1 ] = i ;
    
    vec[ 1 ] = 2;

    Code:
    		cout << " | " << vec.at( i - 1 ) ;
    
    cout << " | " << vec.at( 1 ) ;


    The only operation to change the value of i is the i++. Therefore, the value of i only increases, it never decreases. Given this, and the loop's conditional, please explain how you believe the value of i goes negative.
     
  3. sheepopo39 thread starter macrumors 6502

    Joined:
    Sep 18, 2008
    #3
    THanks, it did help me a lot, however, I still don't get this part.

    Code:
    cout << " | " << vec.at( i - 1 ) ;
    At this point i = 0, then your subtracting it by 1, so shouldn't it be -1?
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Well, no. Read the statements that are executed just before and see what they do. Or use the XCode debugger, which will show you exactly what happens when you step through the code.
     
  5. Thom_Edwards macrumors regular

    Joined:
    Apr 11, 2003
    #5
    Why do you think i == 0? Explaining your answer may help you understand why it does not!
     
  6. Gyro1 macrumors newbie

    Joined:
    Dec 11, 2009
    #6
    Simplified

    It would have been better to write the loop like this:

    Code:
    int main()
    {
    	vector <int> vec( 10 ) ;
    	int i = 0 ;
    	
            // this trades two subtractions for one addition
    	while ( i < vec.size() ) 
    	{
    		vec[ i ] = i + 1 ;
    		cout << " | " << vec.at( i ) ;
                    i++;
    	}
    	return 0;
    }
    
     
  7. sheepopo39 thread starter macrumors 6502

    Joined:
    Sep 18, 2008
    #7
    Ohhh, ok I get it now, I was thinking when you did this

    Code:
    vec[ i-1 ] = i ;
    		cout << " | " << vec.at( i - 1 ) ;
    That it decreased the value of the variable, okay, great, but it doesn't. Although I do have a question on the placement of break statements.

    Thanks a lot everyone

    John
     
  8. sheepopo39 thread starter macrumors 6502

    Joined:
    Sep 18, 2008
    #8
    Also, I'd like to ask, my book says that the position of "break" and "continue" statements are important and that they must appear after the incrementer to avoid an infinite loop, but why exactly does this happen? However, I don't get how it would create an infinite loop for using break before the incrementer, because wouldn't it just break out of the loop?
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Your description is too vague. You need to post specific code and ask a specific question about that code.

    You might also want to quote the actual passage that discusses the position of break and continue. I'm not aware of any unusual rules regarding their placement.

    Also, please post the title & author of your book.
     
  10. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #10
    Exactly. So I can't think of a way to use "break" in such a way as to cause an infinite loop.

    If you use "continue" before the increment however, your loop would keep continuing with the current iteration indefinitely.
     
  11. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #11
    I would like to point out that while this is all valid code, if you're ever using large amounts of data (100s), this will become a bottleneck very quickly. Different compilers are allowed to implement the STL their own ways, and on OS X with gcc 4.0, my experience has been that this method of accessing vector data causes an additional loop at each call in order to retrieve the data.

    Since you're clearly still learning the basics, I won't confuse you with example code. Suffice it to say that at some point, before you start heavily using vectors as a replacement for arrays, you need to learn about iterators. Replacing a few key loops like this with iterators reduced the time taken for a simple task with unusually large amounts of data (1000s) from ten minutes to ten seconds.
     

Share This Page