Thread: Alignment

  1. #1
    Unregistered
    Guest

    Question Alignment

    i have a program that calculates and prints Pascal's triangle. the lines line up on the left side of the screen. how do i get the triangle form on the screen?
    1
    1 1
    1 2 1
    1 3 3 1

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Use a counter to determine the number of spaces before printing the item in the triangle. You start with say 5 spaces and then count down and your triangle will be aligned properly.
    pointer = NULL

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    printing the traingle upside down will be slightly easier .

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Doing your math right also will help.
    You cannot center 1 character over the center of 2 characters.
    It is impossible in text mode. This means you have to do:

    1,3,5,7 and so on for your row lengths. Thus:

    1) Find the length of the longest line.
    2) subtract 1 + (2*current line number) and that is the number of spaces you need total. Divide this in half, and that's the number of spaces you need to preceed your "line" with.

    Example:

    Longest line:

    123454321 = 9 characters

    For line 0, we do this:

    line = 0;
    space = (9 - (1+(line)))/2 ;
    thus, space is 4.
    ____1 line 0
    ___121 line 1
    __12321 line 2
    _1234321 line 3
    123454321 line 4

    See how that works out?

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alignment tutorial needed!!
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2008, 04:41 AM
  2. Dynamic struct alignment?
    By matthew180 in forum C Programming
    Replies: 7
    Last Post: 06-15-2007, 08:34 AM
  3. memory boundry alignment with stuctures
    By ed bitwise in forum C Programming
    Replies: 3
    Last Post: 05-08-2006, 11:33 AM
  4. Ok, I'm dumb, byte alignment for the third time
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 06:19 PM
  5. trouble with printf alignment
    By hyaline in forum C Programming
    Replies: 5
    Last Post: 09-22-2001, 01:39 AM