Need help!

This is a discussion on Need help! within the C Programming forums, part of the General Programming Boards category; New user to the boards, and I'm also new to c...but anyhow. I recently started to try and teach myself ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Need help!

    New user to the boards, and I'm also new to c...but anyhow.

    I recently started to try and teach myself C out of an old school book one of my friends had, and I have a problem I'm trying to figure out.
    The output that I am trying to display looks like this:

    --------------------------------------
    | * |
    | * * |
    | * * * |
    | * * * * |
    | * * * * * |
    ----------------------------------------

    only I need it centered.

    if anyone can help me out I'd greatly appreciate it!

    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you may notice that the spaces required to 'center' the lines may be inversely proportional to the number of stars you want to print.

    that is, the first line has the most spaces, say n, then you print a star. the second line has n - 1 spaces, then you print 2 stars, etc,. the last line has no spaces, then you print all x stars.

    draw out some examples on paper and you may notice a pattern. heres examples, but try yourself. note i will use .'s in place of spaces.
    x = 2 stars
    .*
    **

    x = 3 stars
    ..*
    .**
    ***

    x = 4 stars
    ...*
    ..**
    .***
    ****
    at times it doesnt look perfectly even, but it cant be. you either have to have spaces between the group of *'s or only use odd number of *'s (as opposed to odd and even here), example:
    .....*
    ...***
    .*****
    *******

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    Solution

    Code:
    #include <stdio.h>
    
    int main (){
      int counter;
      int counter2;
    
      system("clear"); //clear the consol
      for (counter=1; counter < 6; counter++){
           printf("| ");
           for(counter2=1; counter2<= counter; counter2++)
               printf("* ");
           printf("|\n");
      }
    
      return 0;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    @cs05pp2 - read the rules. We're here to help people learn by doing programs for themselves, not hand out complete answers on demand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21