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:
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!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"); }
Herbert



LinkBack URL
About LinkBacks



You'll want to wrap code tags around anything which has whitespace that needs to be preserved. (But I'm sure most of us knew what your triangle was supposed to look like. This is a common homework problem.)