Thread: Triangle

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Question Triangle

    I am trying to generate a triangle using "height" as input. The triangle should look like this:

    *
    **
    ***
    **
    *

    where the height is 3, for 3 asterisks. However, the input can be any integer value. You could have 5 for the height.
    I have two functions to generate two parts of the triangle. One prints the top part (up to where the input number height...in this case, the 3 stars) and then the other function prints the bottom part. I'm having trouble with the bottom portion. Please help me. My source code is:

    Code:
    /*Project 52
    Herbert Ortiz
    Due: October 7. 2004
    This program prompts user to input triangle base, and this info
    will generate the shape of that triangle!*/
    
    #include <stdio.h>
    #include <conio.h>
    
    void PrintLine(int linenum);
    void PrintLine2(int area);
    
    main()
    {
     int area, area2;   //local variables declared
     int height;  //same here
    
     printf("Enter the height of the triangle: "); //
     scanf("%d", &height);
    
     for(area=0; area<=height; area++) //main for loop containing function call
     {
      PrintLine(area);  //function call
     }
    
     for(area2=height; area2>=0; area2--)
     {
      PrintLine2(area);
     }
    
     system("PAUSE");
    }
    
    void PrintLine(int linenum)  //function definition and header
    {
     int i;  //local variable i will determine how the output is displayed
    
     for(i=0; i<linenum; i++)  //output for loop
     {
      printf("*");
     }
    
     printf("\n");
    }
    
    void PrintLine2(int area2)
    {
     int j;
    
     for(j=area2; j>0; j--)
     {
      printf("*");
     }
    
     printf("\n");
    }
    The top portions is displayed fine, but the bottom part is messed up! What should my for loop be in order to fix this problem? Thanks!

    Herbert
    Last edited by BB18; 10-07-2004 at 09:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 05:55 AM