![]() |
|
#1
|
|||
|
|||
Help with nested for loops...please. sorry newbieI am having trouble grasping the concept of nested for loops. Usually, I can find the pattern in things and figure them out, but for some reason, I just can seem to find a pattern in using nested for loops.
Can someone please help me interpret nested for loops? Example: Write a program that uses nested to print a hollow rectangle made out of the '|' and '_' characters. The program should prompt the user for the length and the width of the rectangle. So here is what I started with: CPP / C++ / C Code:
Now, I get all flustered cause I don't know what to do. Should I try to define the limits and then use 'if' statements. Please don't give me the code, I need to be able to do this on my own, and if I don't understand why, then I won't learn. Last edited by dsmith : 14-Oct-2004 at 19:24.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#2
|
|||
|
|||
|
May be you should go ahead with the logic you have in your mind and complete the code and then if you face any problem, there are people to help.
Pls complete the remaining code and post it. We will surely try n' help if we can. As you said, you can't learn unless you do it on your own my friend. Also, pls use code tags to post your code. Good luck. |
|
#3
|
||||
|
||||
|
Yep, I agree with nkhambal. You are on the right track.
There is probably others way to do it, but I would definitely need an if statement (or two). Think of it as a type of grid, the first loop should draw the entire row, with the inner loop drawing each individual character of the row. Try it out and post back if you have problems. Good luck. __________________
The best damn Sports Blog period. |
|
#4
|
|||
|
|||
|
Well, I appreciate the motivation
I am attempting, but I must let my brain uncramp from this mess. I will figure it out, its just a matter of time. |
|
#5
|
|||
|
|||
|
Quote:
BTW - what are code tags? |
|
#6
|
||||
|
||||
|
Using the values c and /c inside square brackets [] to enclose your code.
If you scroll to the very bottom of the "post message" page, or look in the FAQ, there's a link to all the codes you can use when you post messages. They help to highlight key terms to make it easier to read. __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#7
|
|||
|
|||
|
Well, I think that I got it. Does anyone have any suggestions on improving?
CPP / C++ / C Code:
Thanks to all the replied. |
|
#8
|
|||
|
|||
|
How about simplyfying loop to
CPP / C++ / C Code:
Just saved one else if for you :-) Also, you dont really want to use system() call. You can use getchar() instead, if the intension is to hold output window to see the output (I guess you are on windows) |
|
#9
|
|||
|
|||
|
That is exactly what I had at first, but since we were told to use an underscore character the output looks funny on the bottom. You end up with something like this:
CPP / C++ / C Code:
Also the system("PAUSE") (to hold output window), is something that we are supposed to use because of our compiler. For some reason, our teacher uses Dev-C++ version 4. It's free, but I am not sure why she doesn't have us use version 5 because it uses the newer standards....resistant to change like everyone else I guess. Thanks for the post! Have you been programming long? |
|
#10
|
|||
|
|||
|
Hi,
I think we are doing some mistake here..we are not actually printing the required number of "_" & "|". Loop logic is having some problem. If i input hight and width both as 10, i should be getting a square but I am not. I have fixed this. CPP / C++ / C Code:
Parden me, if Above code is not properly indented. I did pasted an indented code but its not printing out the same. |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help using nested for loops | dontcare | C++ Forum | 1 | 03-Oct-2004 12:33 |
| linked lists, newbie needs help | moltarim | C Programming Language | 4 | 06-May-2004 11:32 |
| newbie using Sam's series | Skampy | C++ Forum | 3 | 06-Mar-2004 18:51 |
| C++ books for newbie | mak90thug | C++ Forum | 4 | 04-Feb-2004 14:58 |
| Newbie Question about Borland | andrewcarraway | C++ Forum | 4 | 24-Jan-2004 11:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The