|
-
October 7th, 2005, 04:35 PM
#1
Nested Loops help
Ok, sorry for the post, tried searching around online, this seems to be a general program students have to do when the nested loops chapter comes around, couldnt find much information.
Basically I need to write a program that asks the user to enter a number, then the output is a diamond made up a number of rows the user put. Making a nested loops to output this is easy:
*
**
***
****
*****
however I can't figure out how to get the opposite side of the triangle, ie:
(replace - with a blank space, wouldnt show up right if I just did spaces)
----*
---**
--***
-****
*****
This is the only part that has me stumpted, once I get the nested loop right to have the spaces first, then print the asteriks I shouldnt have any problem getting the bottom half to print up. Any help would be greatly apprechiated. The code I am using for the first triangle example is:
for ( int row = 1; row <= 5; ++row){
for ( int col = 1; col <= row; ++col )
cout << "*";
cout << "\n";
}
I'm sure it's something very simple and I'm just making things worse and worse.
-
October 7th, 2005, 04:52 PM
#2
Re: Nested Loops help
Yes, it's quite simple. Just think about it.
Code:
#define MAX 5
for ( int i = 1; i <= MAX; ++i)
{
for ( int j = 1; j <= i; ++j )
cout << "*";
cout << "\n";
}
In the first case you put on each row i asterisk. Than means there are MAX - i spaces left on each row (even though you don't dsiplay them).
So what you have to do is for each row to display first MAX - i spaces and then i asterisks. That means you need 2 fors inside the first for's block.
-
October 7th, 2005, 09:40 PM
#3
Re: Nested Loops help
Try This
Code:
#include <iostream>
#define MAX 5
int main()
{
for ( int i = 1; i <= MAX; ++i)
{
for(int k=0;k<MAX-i;k++)
{
std::cout<<" ";
}
for ( int j = 1; j <= i; ++j )
std::cout << "**";
std::cout << "\n";
}
return 0;
}
Appreciate others by rating good posts
"Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett
-
October 8th, 2005, 05:40 AM
#4
Re: Nested Loops help
wow, it was that simple, hah. I was just screwing around with examples in the book and came up with something alittle more complicated. For the most part it works, I just need to get one last thing right. The number the user has to input can only be an odd number, so the very middle line of the diamond has to be the biggest line, mine always comes up like this: (I will figure this out!).
---------*----------
--------**---------
-------***---------
------****--------
------****---------
-------***---------
--------**----------
---------*----------
say the number entered was 9, it would round down to 8. I used the variable 'size' for the user input, and in the for loops I ended up doing size/2 for each half of the diamond (top and bottom). I just need to figure out how to have that middle line added and it's all set. Again, any help would be apprechiated. Also, if possible all I'm looking for is a little push in the right direction, little hints on where my problem is or what I'm doing wrong. No need to actually put down the solution/code Thanks everyone.
And thank you Cilu and Sunnypalsingh for the replies, both were very helpful. Especially Cilu, the problem was so simple, but I just wasnt thinking. I might just re-read the chapter.
-
October 8th, 2005, 01:45 PM
#5
Re: Nested Loops help
Well, if you're using size/2 for both of your loops, and you need one additional line in the center, then that means that one of your loops needs to run one extra time. So have one of your loops run size/2 times, and the other run (size+1)/2 times. Remainders are always dropped when doing integer division in C++, so:
- If size is even, then (size+1)/2 = size/2.
- If size is odd, then (size+1)/2 = size/2 + 1.
This seems to be exactly what you want, since your program is only having trouble when size is odd. Also, for future reference, if you need to display some text in a fixed-width font that preserves multiple consecutive spaces, as in your illustrations, you can wrap the text in [code]...[/code] tags. Like this:
Code:
*
**
***
****
****
***
**
*
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|