The dreaded For loop.

Started by
8 comments, last by rip-off 16 years, 10 months ago
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?
Advertisement
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;}
Thank you, works like a charm.
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.

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.
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.
Perhaps I should have given Sarxous psuedocode rather then actual code...
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.
If you're not allowed to use nested loops, try this:

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

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

This topic is closed to new replies.

Advertisement