943,744 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 505
  • C++ RSS
Jan 7th, 2009
0

for and while HELP

Expand Post »
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 1:49 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mnmustafa is offline Offline
11 posts
since Jun 2007
Jan 7th, 2009
0

Re: for and while HELP

||||||*
||||*||*
||*||*||*
*||*||*||*

like this but without "|"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mnmustafa is offline Offline
11 posts
since Jun 2007
Jan 7th, 2009
0

Re: for and while HELP

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 7th, 2009
0

Re: for and while HELP

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
C++ Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int printStars (int lines,int stars,int space);
  5.  
  6. int main ()
  7. {
  8. printStars(lines, stars, space);
  9. return 0;
  10. }
  11.  
  12. int printStars (int lines,int stars,int space)
  13. {
  14. int lines, stars, space;
  15.  
  16. for (lines=1; lines<=4; lines++)
  17. {
  18. for (space= 4; space >=0; space--)
  19. {
  20. cout<<" ";
  21. while (space=0)
  22. {
  23. for (stars=1; stars<=lines; stars++)
  24. }
  25.  
  26. {
  27. cout<<"* ";
  28. }
  29.  
  30. }
  31. cout<<endl;
  32. }
  33.  
  34.  
  35. }

thnx
Last edited by mnmustafa; Jan 7th, 2009 at 2:06 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mnmustafa is offline Offline
11 posts
since Jun 2007
Jan 7th, 2009
0

Re: for and while HELP

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
c++ Syntax (Toggle Plain Text)
  1. int count;
  2. cout << "What is the count?";
  3. cin >> count;
Last edited by c++noobie; Jan 7th, 2009 at 2:09 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Jan 7th, 2009
0

Re: for and while HELP

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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mnmustafa is offline Offline
11 posts
since Jun 2007
Jan 7th, 2009
0

Re: for and while HELP

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 3:00 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Jan 7th, 2009
0

Re: for and while HELP

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:
C++ Syntax (Toggle Plain Text)
  1. *.*.*.*

Then I just add the lines above it:

C++ Syntax (Toggle Plain Text)
  1. ...*
  2. ..*.*
  3. .*.*.*
  4. *.*.*.*

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?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 7th, 2009
0

Re: for and while HELP

thnx people
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mnmustafa is offline Offline
11 posts
since Jun 2007
Jan 7th, 2009
0

Re: for and while HELP

C++ Syntax (Toggle Plain Text)
  1. 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 10:15 am.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: fibonocci sequence question
Next Thread in C++ Forum Timeline: what is going on in this function?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC