Thread: triangle of *

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    triangle of *


    hello,
    how can i get an o/p that's basically if i/p is 4
    then o/p should be
    *
    **
    ***
    ****
    using for loop
    ican get only one * per line
    so how can i get o/p in reqd format
    please suggest!

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    deja vu. this homework assignment shows up quite often.

    Think counter.... think spitting out the number of asterisks that is in your counter.... think incrementing the counter each loop iteration.

    That is all. I hope nobody gives you any code.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    tri of*

    hey iam not asking for code: afer here iam kind of cofused. how can i have multiple * is what iam struck at
    # include <iostream.h>
    main()
    {
    int n ,i;
    cout<<"enter pos no";
    cin>>n;
    for ( i=1;i<=n;i++)
    cout<<"*";
    }
    return 0;
    }

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ok, so I imagine what you are seeing is n stars. This is because you are drawing one star per iteration. But what you want to do is drop down a line each iteration right? you know how to do that I assume. plus... each iteration needs to draw "i" stars. another loop inside maybe? understand where I'm going?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Thumbs up tri of*

    thank you for the tip.i got the o/p
    Code:
    # include <iostream.h>
    main()
    {
    	int n ,i,j;
    	cout<<"enter pos no";
    	cin>>n;
    	for ( i=1;i<=n;i++)
    	{
    		for ( j=1;j<=i;j++)
    			{
    				cout<<"*";
    			}
    		cout<<endl;
    	}
    
    	return 0;
    }
    Code tags added by Hammer
    Last edited by GMK; 11-07-2002 at 02:13 PM.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    glad to hear it. I wasn't attacking you're post by the way. I just wanted to head anyone else off who might post the code.

    also, use code tags and your code will keep its indenting.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Query reg return 0

    hello,
    i waswondering why do we need to add the return 0 at the end of each prg. why warning is generated if this is not used and what is difference if we use void in the main function
    void main (void) and void main()

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ANSI standard main returns an int. What does this mean to the system? very little. void works in many compilers but some people and some compilers stick to the ANSI standard religiously.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if figuring out that type of puzzle provided a sense of accomplishment, you can further challenge yourself by trying to display the triangle as an isosoles(sp?) triangle (2D pyramid), and once you have that figured out try to draw a diamond shape. It is easier/prettier if you use only an odd number of * per row for these shapes.

  10. #10
    Or prelude's fractal triangle

    I would still like to see that code again, i can't find it anymore.. Links anyone?

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I would still like to see that code again, i can't find it anymore.. Links anyone?
    It was probably something like this:
    Code:
    #include <iostream>
    
    int main()
    {
      int i, j;
      
      for ( i = 0; i < 16; i++ ) {
        for ( j = 0; j <= i; j++ )
          std::cout.put ( ~i & j ? ' ' : '1' );
    
        std::cout.put  ( '\n' );
      }
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    >It was probably something like this:
    Actually, it was exactly like this...
    Code:
    #include <iostream>
    
    const int N = 16;
    
    int main ()
    {
      for ( int n = 0; n < N; ++n ) {
        for ( int k = 0; k <= n; ++k )
          std::cout.put ( ( ~n & k ) ? ' ' : '*' );
    
        std::cout.put ( '\n' );
      }
    
      std::cin.get();
    }
    (I liked it, too. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    tri of*

    hello,
    i would like to try that right angle triangle and also prelude's fractal triangle
    how should the o/p look like ?
    please suggest
    thanx

  14. #14
    This question didn't get answered last time:
    >> What exactly does "( ~n & k )" test for?

    Thanks!
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Since this is Prelude's code, she can feel free to yell at me...not really...but let's take a shot.
    Code:
    std::cout.put ( ( ~n & k ) ? ' ' : '*' );
    '~' inverts the bits of a given value, in this case, 'n'. ('~' is typically used in a destructor.)

    (You can see where this is going already, can't you?)

    The bit values of 'n' are inverted then 'AND'ed against the bits of 'k'. Both must be '1' for the AND expression to evaluate the bit to '1', otherwise the bit evaluates to '0'.

    Ex.:

    n = 0001
    ~n = 1110

    k =0101, therefore,

    ~n & k = 0100

    The rest is semi-self-explanatory (I think that's a new word ). If the value of (~n & k) is '0' (false), we print an asterisk. If any of the bits evaluate to '1', the value is "true" - for those extremely new to the language, anything other than '0' is "true" - and we print a space character.

    Play with it on paper to really convince yourself. Note that when the inner loop has completed, we go to the next line.

    (Danged if I know how that young lady figured this out, though. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 05:55 AM