Sign in to follow this  
Sarxous

The dreaded For loop.

Recommended Posts

This book (Learn C++ in 21 Days), which has plagued me over the last few weeks has asked me to write a program using a For loop that displays a 10x10 array of 0s....not using arrays (as they haven't be introduced in the book yet). Help please?

Share this post


Link to post
Share on other sites
Loop inside a loop:

for (int y=0; y<10; y++) {

// print 10 zeros for a column
for (int x=0; x<10; x++)
cout << "0";

// columns done, so print a newline to start new row
cout << endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Sarxous
Thank you, works like a charm.


...

So? The purpose of it was to figure it out yourself. Who cares if it works or not since you didn't write it?

Next time, you better do as much as you can first, state what you have so far and what doesn't work or you don't understand, and ask for hints, not for the solution. This thread has only hurt you, nothing more.

Share this post


Link to post
Share on other sites
I agree with mikeman. The point of a programming exercise is to figure out how to write that piece of code by yourself, not to have someone give you the solution. You will never learn how to do anything if you don’t attempt to solve your own problems.

It is sort of like doing math homework and asking someone for the answer. You get the correct answer, but if you don’t understand the steps that were used to compute that answer then you haven’t learned how to do the problem and you won’t be able to do it on a test.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
Loop inside a loop:

for (int y=0; y<10; y++) {

// print 10 zeros for a column
for (int x=0; x<10; x++)
cout << "0";

// columns done, so print a newline to start new row
cout << endl;
}


Also known as nested loop.

The first loop goes once around then the inside loop all of its loops. Then the outside one does its 2nd loop and then the inside loop does all of its loops again.

Same thing goes on till the condition is met on the outside loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
Perhaps I should have given Sarxous psuedocode rather then actual code...


No that was fine I just rembered back when I had trouble with this I need help because the tutoril only should what it looked like not how it worked.

Share this post


Link to post
Share on other sites
If you're not allowed to use nested loops, try this:


for (int i=0; i<100; ++i) cout << "0" << ((i%10 == 9) ? endl : "");

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
If you're not allowed to use nested loops, try this:


for (int i=0; i<100; ++i) cout << "0" << ((i%10 == 9) ? endl : "");


Nope.

First of all, compiling this on G++ gives "overloaded function with no contextual type information", this is because std::endl is actually some kind of function template and G++ cannot determine the correct version to use in the example code you gave.

Secondly, the ?: operator must have the same type on both sides of the colon. This is not allowed:

std::cout << "blah blah " << (someBoolean ? 17 : "17") << std::endl;


You could use '\n' instead of std::endl. And you could use a proper if statement rather than the more obfuscated way you are doing it (it wouldn't be so bad if you were inserting something into the stream on the else part).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this