Thread: Help.

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

    Help.

    I'm trying to create a triangle:
    *
    **
    ***
    ****
    *****
    But I can't make it.

    Any help? Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What have you tried so far - you should know how it works by now: You post the code, we tell you how to move forward.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Use a for loop for to print each line. Inside the for loop add loop that runs to the current position in the outer loop printing a star each iteration. That would be one way of doing it.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by matsp View Post
    What have you tried so far - you should know how it works by now: You post the code, we tell you how to move forward.

    --
    Mats
    I know how I could create a square, I've no idea how to create a triangle:
    Code:
     int i, j;
     for(i = 0; i < 5; i++) {
           for(j = 0; j < 5; j++) {
                 printf("*");
                 }
           printf("\n");

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    How many stars do you need to print on each line? Think about that for a second.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Did you read my post?

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
     int i, j;
     for(i = 0; i < 5; i++) {
           for(j = 0; j < 5; j++) {
                 printf("*");
                 }
           printf("\n");
    Think a bit, how the value of i going from 0 to 4 could help you determine how the maximum value of j, could go from 1 to 5 and you have your answer.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by mike_g View Post
    Did you read my post?
    Yeah, I've read it and didn't actually understand ^^
    MacGyver, I'm trying to think, but failing - How to increment one star at a time? :\
    I could write 5 If's but that would be a waste - I'm trying to be efficient

    Code:
    int i,j;
    for(i = 0; i < 6; i++)     {
    for(j = 0; j < i; j++)
    printf("*");
    printf("\n");
    }
    Yay, thanks guys
    Last edited by eXeCuTeR; 02-18-2008 at 02:58 PM.

  9. #9
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    A triangle is simply a set of 3 line segments that join up together, at the core definition anyway. One can be represented in other ways as well such as using a series of figures. Let's try a filled right triangle with the right angle at the bottom left corner, the easiest kind to make. Take a few coins or something and design something that looks like this. Hint: form the right angle first then do the hypotenuse. Look for any patterns then replicate this in a program. It should be very easy as it just involves variable manipulation and 2 characters being printed, one seen, the other a line feed.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed