Nested loop? Heck, you can do it with just a single loop:
Joined: Mar 29, 2005
Originally posted by mark ralphael: Hi Marc, thanks for the answer. I was unable to resolve this trivial question, can you give me any advice on how i should go about solving the problem. I wouldn't of thought of doing a nested loop ???
I know your question was directed at Marc, but I'll put my two cents in. Mostly so you don't think I'm picking on you by posting my horrible code examples.
Like with most things in life, most of the trick of getting the solution is in posing the question in the right way. For example, what if I had given you the exact same problem, but put it this way:
Write a Java program to print out five lines.
On each of these lines, have it print the same number of characters as the line number. That is, for line one, print one character. For line two, two characters, and so on.
(Oh, and by the way... the character I want to you print should match the line number.)
You'd have probably been able to come up with the solution pretty quickly, wouldn't you? That just screams out "nested loop". You have to have a loop to print each line, and you'll need another loop to print out the correct number of characters.
So I guess it's a matter of recognizing the pattern, and then translating that into the programming idiom. The more you program, the better and quicker you get at doing that.
As Jeff illustrated, it's a matter of interpreting the problem conceptually before you get lost in the code details. Before you start driving, sketch out a map. If you take a step back and look at where you want to go, you'll see...
You want to print out 5 lines. Okay, so that's a loop.
Now, within each of those lines, you want to print out a certain number of characters. Okay, so within each iteration of the first loop, we have another loop.
The number of inner loop iterations depends on the outer loop iteration. In other words, the upper limit of the inner loop's counter (y) is a function of the outer loop's counter (x).
(By the way, that binary approach is pretty clever. )
Alternatively, XP says YAGNI ... you aren't gonna need it.
[ April 01, 2005: Message edited by: Carol Enderlin ]
Joined: Mar 29, 2005
Thank you very much Steve, Marc, and Jeff. Your input has been extremely helpful. I hope to speek to you again in the future.
Joined: Mar 29, 2005
Originally posted by marc weber: (By the way, that binary approach is pretty clever. )
Thanks! I was pretty psyched to come up with it myself.
But it does serve to further flesh out what we're talking about: One you recognize a pattern, the programming is a lot simpler. My alternate solution just comes from seeing a second pattern in the example output:
1 * 1 2 * 11 3 * 111 4 * 1111 5 * 11111
It's more of a mathematical way of looking at it than a procedural way, but once you spot that pattern, and notice that the second number looks suspiciously like the decimal interpretaion of a binary number, the "one loop" version becomes a no-brainer.
- Jeff [ April 02, 2005: Message edited by: Jeff Jetton ]
When coding a loop, remember that the first and sometimes second iterations are often special cases. This is because doing something 0 times or even 1 time doesn't doesn't give you a sense of "iterating." In other words, these are not good models for all the other iterations that will follow.
So when coding a loop, you should mentally put yourself "in" a few steps to get a better feel for the loop's typical behavior.
Specifically, in this example, printing the character "1" a single time might not give you a sense that you actually need to count the number of times you're printing.
Just for grins, you could give yourself a handful of additional excercises to play with. Draw a symmetrical triangle, a diamond, an empty diamond, a zig-zag line. Some times you'll get something working then read it carefully and realize you can remove half of it. That's when you start building your skills through these tiny problems.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi