RSS Forums RSS

for and while HELP

Please support our C++ advertiser: Programming Forums
Reply
Posts: 11
Reputation: mnmustafa is an unknown quantity at this point 
Solved Threads: 0
mnmustafa mnmustafa is offline Offline
Newbie Poster

for and while HELP

  #1  
Jan 7th, 2009
hiz, well i have a question regarding to for and while loop
i want to create such a triangle with "*"
*
* *
* * *
* * * * (its not right angle triangle, its equilateral triangle)

and 2nd thing, what if i want the USER to put the line numbers (upto 30)

Thnx
MnMustafa
Last edited by mnmustafa : Jan 7th, 2009 at 12:49 am.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 11
Reputation: mnmustafa is an unknown quantity at this point 
Solved Threads: 0
mnmustafa mnmustafa is offline Offline
Newbie Poster

Re: for and while HELP

  #2  
Jan 7th, 2009
||||||*
||||*||*
||*||*||*
*||*||*||*

like this but without "|"
Reply With Quote  
Posts: 5,126
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 633
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: for and while HELP

  #3  
Jan 7th, 2009
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Posts: 11
Reputation: mnmustafa is an unknown quantity at this point 
Solved Threads: 0
mnmustafa mnmustafa is offline Offline
Newbie Poster

Re: for and while HELP

  #4  
Jan 7th, 2009
well thnx for those articles, but this isnt my homework or something, i m trying to do this since 2 days, i am new, so ofcourse i need some help. well here is the program i wrote which has 7 errors
# include <iostream>
using namespace std;

int printStars (int lines,int stars,int space);

int main ()
{
	printStars(lines, stars, space);
	return 0;
}

int printStars (int lines,int stars,int space)
{
	int lines, stars, space;

	for (lines=1; lines<=4; lines++)
	{
		for (space= 4; space >=0; space--)
		{
			cout<<" ";
			while (space=0)
			{
			for (stars=1; stars<=lines; stars++)
			}
			
			{
				cout<<"* ";
			}
			
		}
	cout<<endl;
	}
			

}

thnx
Last edited by mnmustafa : Jan 7th, 2009 at 1:06 am.
Reply With Quote  
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: for and while HELP

  #5  
Jan 7th, 2009
The first thing you need to do is get the number of lines to print, using cin. First, create a variable to hold the number of lines, then read it in from the user.

An example would be
  1. int count;
  2. cout << "What is the count?";
  3. cin >> count;
Last edited by c++noobie : Jan 7th, 2009 at 1:09 am.
Reply With Quote  
Posts: 11
Reputation: mnmustafa is an unknown quantity at this point 
Solved Threads: 0
mnmustafa mnmustafa is offline Offline
Newbie Poster

Re: for and while HELP

  #6  
Jan 7th, 2009
well sir, actually the CIN is 2nd part of my question!! i just want to write a program if i want to create 4 lined equilateral triangle..
Reply With Quote  
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: for and while HELP

  #7  
Jan 7th, 2009
Use for loops to print the stars, the spaces, etc. The standard for loop structure is one of the following two

for( int i = 0; i < numberOfTimes; i++ )
for( int i = 1; i <= numberOfTimes; i++ )

for loop number of lines
{
for loop spaces
{
print space
}
for loop stars
{
print star, print space
}
print newline
}
}

Edit:
BTW, the number of spaces to print is equal to the TotalNumberOfLines - CurrentLineNumber
The number of stars to print is equal to the CurrentLineNumber
Last edited by c++noobie : Jan 7th, 2009 at 2:00 am.
Reply With Quote  
Posts: 524
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 78
Murtan Murtan is offline Offline
Posting Pro

Re: for and while HELP

  #8  
Jan 7th, 2009
Ok so lets 'draw' one by hand and then figure out how to get the computer to do what we did.

(I'm going to use '.' instead of ' ' so you can see them.)

Ok so in my head, I start with the base of the triange as that determines how big it will be:

Then I just add the lines above it:

...*
..*.*
.*.*.*
*.*.*.*

Is that what you wanted?

So, for a 4 row triangle, the first row must be indented 3 spaces, the next 2 spaces, the next 1 space and the last line isn't indented at all.

The first line has 1 star, the second 2 stars seperated by a space, the third 3 stars with spaces and the last 4 stars with spaces.

Do you see a pattern here that you might be able to exploit?
Reply With Quote  
Posts: 11
Reputation: mnmustafa is an unknown quantity at this point 
Solved Threads: 0
mnmustafa mnmustafa is offline Offline
Newbie Poster

Re: for and while HELP

  #9  
Jan 7th, 2009
thnx people
Reply With Quote  
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: for and while HELP

  #10  
Jan 7th, 2009
while (space=0)

Is it something you really mean? Shouldn't it be ==?

http://cppkid.wordpress.com
Last edited by kbshibukumar : Jan 7th, 2009 at 9:15 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Views: 284 | Replies: 9 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:46 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC