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