Thread: Triangle

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Question Triangle

    Hey, I'm having issues creating a triangle.

    what i want:

    Output: Random number
    Input: 4
    Output: Triangle

    What I get:
    http://i41.tinypic.com/711d3n.png

    What I want:
    The opposite.

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        system("COLOR 0A");
        int height, numberstars, space; 
     
     
     
        printf("height of triangle: ");
        scanf("%d", &height); 
     
        if ((!height) || (height <= 0))
        {
            printf("Wrong input!\n");
        }
     
        int i, j, k;
        for (i = 1; i <= height; i++)
        {
            space = i;
            numberstars = (height + 1 - i)*2-1;
     
            for (k = 1; k <= space; k++)
                printf(" ");
    
    
            for (j = 1; j <= numberstars; j++)
                printf("*");
                printf("\n");
        }
        return 0;
    }
    Can someone help me?

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Opposite is a bit vague. Do you want to swap the stars and spaces, so you get a "hollow" triangle? Or do you want to flip it, e.g. upside down? Or something else?

    Assuming you want to flip it upside down:
    1. My preferred way would be to adjust the equations for space and numberstars so they work out correctly for an increasing loop counter (possibly just swapping them).
    2. The easy way: make your loop go the opposite way, i.e. decreasing from height to 1.

    Other notes:

    1. system("COLOR 0A") is non-standard and not portable. Just an FYI, since if you are doing this on Windows, and your school is doing this on Linux or something, that command may cause you problems.
    2. You should check the return value of scanf to see if you actually read any input. If it didn't, height will have whatever bogus value it had when main started, and your height may end up a huge positive number.
    3. Your check if (!height) is unnecessary. !height is the same as height == 0. But that is covered by your other check of <= 0. Just use if (height <= 0).
    4. The conventional for loop in C starts at 0 and uses < (not <=) for the loop condition: for (i = 0; i < 10; i++). That iterates 10 times, giving values from 0 to 9. What you did also works, it's just slightly less conventional.
    5. Your indentation on line 32 is misleading. It suggests the printf("\n") is part of the for loop, but it's not. I think you know it's not, but I see lots of noobs make that mistake, so just want to make sure you're clear on that point.
    6. You don't need both j and k. You can reuse the variable. Better yet, I would make a small function that prints a single character n times. See below:


    Code:
    void print_char_n_times(char c, int n)
    {
        // fill in code to print c in a loop n times
    }
    ...
    print_char_n_times(' ', spaces);  // note the use of single quotes for a char literal
    print_char_n_times('*', numberstars);

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So fix your formulas for space and numberstars then. Presumably you want space to go down with i (so i would have to be subtracted from something) and numberstars to go up.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Alright, I got it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int main() {
        system("color a");
        system("cls");
        int height, spaces;  
        int i, j, k;
        printf("Bitte die Hoehe des Dreiecks eingeben: ");
        scanf("%d", &height);
        spaces = height - 1;
    
    
        if (height < 1) {
            printf("Ungueltige Eingabe!\n");
        }
        for (i = 1; i <= height; ++i) {
            for (j = 0; j < spaces; ++j) {
                printf(" ");       
            }
            for (k = 0; k < (2 * i - 1); ++k) {
                printf("*");
            }
            spaces--;
            printf("\n");
        }
        getch();
        return 0;
    }
    Thank you for your help.
    Last edited by Erik Nemeth; 12-14-2013 at 12:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1 to 20 Triangle
    By zenox_ruiz in forum C Programming
    Replies: 2
    Last Post: 02-18-2013, 10:41 AM
  2. triangle help??
    By unix7777 in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2008, 03:46 PM
  3. Triangle
    By howeezy in forum C++ Programming
    Replies: 7
    Last Post: 10-10-2005, 09:36 AM
  4. triangle
    By volk in forum C Programming
    Replies: 2
    Last Post: 01-06-2003, 05:05 PM
  5. triangle of *
    By GMK in forum C++ Programming
    Replies: 16
    Last Post: 11-12-2002, 10:40 PM

Tags for this Thread