Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,289 people online right now. Registration is fast and FREE... Join Now!




problem creating a star(*) triangle with for loop

 
Reply to this topicStart new topic

problem creating a star(*) triangle with for loop

fgg_86
9 Nov, 2007 - 10:17 AM
Post #1

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 3


My Contributions
I am having trouble with using a for loop to create a triangle. The program displays a triangle of stars(*) based on how many rows the user enters. The rows entered must be between 3 and 20 and the actual triangle needs to have correct space formatting.

Basically what I have done for this program is created a nested for loop that lets the user enter the number of rows, then I use a for loop to start the triangle design like so:

for (int rows = 1; rows <= numRows; rows++)

(numRows is how many rows entered by user)

The problem I am having relates to what kind of for loop I need to nest in the 'rows' for loop. I can't decide if I need to make a for loop for the number of stars in each or make a for loop to show how many spaces display before the stars. If I had to guess I think I might need to make a loop for the number of stars then use formatting code like setw() to get the correct number of spaces.

Any suggestions?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Problem Creating A Star(*) Triangle With For Loop
9 Nov, 2007 - 10:23 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
well post the code you have so far in code tags like this:

code.gif

welcome to </dream.in.code>
and try to search this forum.. you'll find many many threads similar like this one
User is offlineProfile CardPM
+Quote Post

fgg_86
RE: Problem Creating A Star(*) Triangle With For Loop
9 Nov, 2007 - 10:33 AM
Post #3

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 3


My Contributions
Alright it looks like this so far:

CODE
#include <iostream>
using namespace std;

int main()
{
    int numRows;
    int spaces;
    
    cout << "Enter an interger between 3 and 20: ";
    cin >> numRows;
    cout << endl;
    
    for (int rows = 1; rows <= numRows; rows++)

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Problem Creating A Star(*) Triangle With For Loop
9 Nov, 2007 - 02:15 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Ok the big hint is using another loop, so you are basically using a nested for loop where the limit in the condition of the inner loop would the iterator of the outer loop. See where I'm getting at? So the output should be:
user eneters number of rows say.. 5
the triangle will look like
*
**
***
****
*****

a nested for loop looks like this:
CODE
for(;;)
{
     for(;;)
    {
          //something
    }
//something
}


This post has been edited by PennyBoki: 9 Nov, 2007 - 02:16 PM
User is offlineProfile CardPM
+Quote Post

fgg_86
RE: Problem Creating A Star(*) Triangle With For Loop
12 Nov, 2007 - 12:46 PM
Post #5

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 3


My Contributions
Right I know that I need a nested for loop, maybe even a nested loop inside the first nested for loop, my problem is that I can't figure out the syntax behind it.

Like I said I know that I need a nested loop, however I can't see how to type the syntax to create this triangle formatted with setw():

* <-----3 spaces before *
***<-----2 spaces before *
*****<-----1 space before *
*******<-----0 spaces before *

I DO know that the nested for loop is necessary to determine the rows and stars. The outside loop is for rows and the inside loop is for determining the stars in each row. With that in place I could use setw() to make the correct number of spaces in each row.

If I had to guess on how to enter this I would say:

CODE
for (int rows = 1; rows <= numRows; rows++)
    {
        for (int i = 1; i <=


Right there at the end is where I draw a blank

Any thoughts anyone?
User is offlineProfile CardPM
+Quote Post

aaron.henriques
RE: Problem Creating A Star(*) Triangle With For Loop
5 Jan, 2008 - 05:06 AM
Post #6

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
CODE

#include<iostream.h>                                                          
#include<conio.h>                                                            
void main()                                                                  
{       clrscr();                                                            
        int n;                                                                
        cout<<"enter any number of rows";                                    
        cin>>n;                                                              
        for(int i=1;i<=n;i++)                                                
        {       for(int j=1;j<=i;j++)                                        
                   cout<<"*";                                                
                   cout<<"\n";                                                
        }                                                                    
}                                                                            

Try this program it will work...
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Problem Creating A Star(*) Triangle With For Loop
5 Jan, 2008 - 06:52 AM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi aaron.henriques, that is nice of you to help but I suggest you read the forum rules. The member needs to show effort in code so that someone could help.

PS your code is not standard-compliant and there is no need to open threads that are well out of date.

This post has been edited by PennyBoki: 5 Jan, 2008 - 06:54 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 06:03AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month