Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sheepopo39

macrumors 6502
Original poster
Sep 18, 2008
251
0
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
 
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 ) ;


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.

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.
 
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?
 
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?

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.
 
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?

Why do you think i == 0? Explaining your answer may help you understand why it does not!
 
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;
}
 
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
 
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?
 
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?

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.
 
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?

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.
 
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++ ;
		[COLOR="Blue"]vec[ i-1 ][/COLOR] = i ;
		cout << " | " << [COLOR="Blue"]vec.at( i - 1 )[/COLOR] ;
	}
	return 0;
}

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.