Thread: Need help drawing triangles (C)

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Need help drawing triangles (C)

    I need to allow the user to input length n and draw a triangle as shown below based on the length n.

    n = 7 (odd number)
    *
    ***
    *****
    *******
    (^should be centered^)

    n= 4 (even number)
    *
    **
    ***
    ****

    Can anybody help me at least get started?

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You can get the users input using scanf. Then I would use a for loop to print out the triangles. Give it a try and if you still need help post the code you have created so far.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    How would I distinguish between odd and even numbers?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    look at the % operator. % allows division and returns the remainder.

    that is 11%2 = 1


    ie


    if( ( iNum % 2) != 0 )//is odd
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >How would I distinguish between odd and even numbers?
    That's not nearly as interesting as how you would get an even number well centered:
    Code:
      * /* Uh oh */
     **
    ****
    Odd works much better:
    Code:
       *
      ***
     *****
    *******
    Using an ASCII display you can't get an even numbered pyramid to center properly. In this case I would make sure that the number is always odd by either requiring an odd number to be input, or making it odd by adding or subtracting one if it's even.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    oh the even doesnt need ot be centered. im just having trouble printing the triangle.

  7. #7
    root
    Join Date
    Sep 2003
    Posts
    232
    >(^should be centered^)
    >oh the even doesnt need ot be centered.
    Be more specific.

    >im just having trouble printing the triangle.
    Think three loops:
    Code:
    for each row
      for each space
        print a space
      loop
      for each star
        print a star
      loop
      print a newline
    loop
    Naturally you'll need a variable with the number of spaces to print that you decrement and a variable with the number of starts to print that gets incremented. This should get you started on experimenting, have fun.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing HBITMAP into CWnd, Acquired from "screenshot"
    By DeusAduro in forum Windows Programming
    Replies: 6
    Last Post: 07-02-2009, 03:41 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  3. Drawing triangles in C#
    By John_L in forum C# Programming
    Replies: 2
    Last Post: 03-15-2008, 08:48 AM
  4. How to do double buffing with win32 drawing?
    By Josh Kasten in forum Windows Programming
    Replies: 2
    Last Post: 03-27-2004, 12:02 AM
  5. Drawing triangles
    By TonyLFL in forum C++ Programming
    Replies: 7
    Last Post: 06-02-2002, 09:51 AM