Public Group

This topic is 4159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Thank you, works like a charm.

##### Share on other sites
Quote:
 Original post by SarxousThank 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 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 on other sites
Quote:
 Original post by CrypterLoop 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 on other sites
Perhaps I should have given Sarxous psuedocode rather then actual code...

##### Share on other sites
Quote:
 Original post by CrypterPerhaps 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 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 on other sites
Quote:
 Original post by DevFredIf 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).

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632997
• Total Posts
3009781
• ### Who's Online (See full list)

There are no registered users currently online

×