# Triangle

• 10-07-2004
BB18
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
• 10-07-2004
sand_man
/[ code ] needs to be [ / code ]
• 10-07-2004
BB18
Yeah Sorry!
Ehehe, minor HTML mistake there.
• 10-07-2004
xErath
First error, you're calling system without including stdlib.h

Second send the area2 var as parameter not the area

Code:

```for(area2=height; area2>=0; area2--) {     PrintLine2(area2);//FIX: send area2, not area }```
You could also simplify your code.. The task done is extremely simple.

Code:

```#include<stdio.h> #include<conio.h> int main(){         int i,j, height = 7; /*.do whatever input you want.......*/         for(i=1;i<height;i++){                 for(j=0;j<i;j++)                         putchar('*');                 putchar('\n');         }         for(;i>0;i--){                 for(j=0;j<i;j++)                         putchar('*');                 putchar('\n');         }         getch(); }```
• 10-08-2004
BB18
Thank You
Thanks! I understand perfectly.

Now, I have to create a triangle that displays like this, in this case, when the height is 11:

*
***
*****
*******
*********
***********
*********
*******
*****
***
*

The difference is apparent. The user must input a positive odd integer value. The amount of stars printed on each line increases by 2 as we go from line 1 up to the middle part, where 11 (for instance) or the input value of stars is printed. Going downhill, the number of stars begins to decrease by 2, until only 1 star is printed (like in the beginning). I've tried to make some changes on the original triangle program code, but so far I haven't suceeded.

My code from last time, which I fixed, looks like:
Code:

```/*Project 53 Herbert Ortiz Due: October 11, 2004 This programs prompts the user to input a positive odd integer value:  And based on this information, print a triangle as the output*/ #include <stdio.h> #include <conio.h> #include <assert.h> void PrintTop(int linenumber);  //this function will print the top triangle portion void PrintBottom(int linenumber2); //this one will print the bottom portion main() {  int linenumber, linenumber2;  //local variables declared for use in the loops  int height; //input variable is declared here  printf("Enter the height of the triangle: "); //prompt  scanf("%d", &height); //height is stored in memory for evaluation in the loop  assert(height>0);  //error handler  for(linenumber=0; linenumber<height; linenumber++) //the for loop dealing with top triangle portion  {   PrintTop(linenumber);  //function call for top portion of triangle  }  for(linenumber2=height; linenumber2>=0; linenumber2--) //for loop dealing with bottom triangle portion  {   PrintBottom(linenumber2);  //function call for bottom portion  }  system("PAUSE"); } void PrintTop(int linenumber) //function header and definition {  int i;  //variable i is used for the for loop here  for(i=0; i<linenumber; i=i+2) //for loop that will print first output  {   printf("*");  }  printf("\n"); } void PrintBottom(int linenumber2)  //function header and definition {  int j; //j is used for the following for loop  for(j=linenumber2; j>0; j=j-2)  //this loop prints the bottom triangle piece  {   printf("*");  }  printf("\n"); }```
• 10-08-2004
xErath
First you should asset this at the beginnig:
Code:

` if(heigth/2==0?) heigth++;`
Although it's not necessary.
Then keep your PrintTop() and PrintBottom() functions as they were before (PrintLine()...). Those work well.
You only have to change the primary loop
Code:

```//you have  for(linenumber=0; linenumber<height; linenumber++) //But it should be: start with 1 which is odd, then add 2 at each iteration  for(linenumber=1; linenumber<height; linenumber+=2) //and the second for(linenumber2=height; linenumber2>=0; linenumber2-=2) //but if height can be even its better to for(linenumber2=linenumber; linenumber2>=0; linenumber2-=2)```
Extra: your Print*** functions do EXACTLY the same thing, so you only need ONE. And you still haven't included stdlib.h
• 10-08-2004
BB18
Awesome
I have made the necessary touchups you pointed out. I'll try to see what my output is. Thank you!
• 10-09-2004
Salem
• 10-11-2004
BB18
The So-Called Christmas Tree
The last triangle looks different from the previous ones in that it's actually standing like a normal triangle should!

The user inputs the base (which has to be an odd integer) of the triangle.

For instance:

Base is 9

*
***
*****
*******
*********

I have a rough draft in source code.

Code:

```#include <stdio.h> #include <conio.h> #include <string.h> int GetCountOfStarsInLine(int line); void PrintNSpaces(int line); void PrintStars(int line); main() {  int line, height;  for (line=1; line<=height; line++)  {   PrintNSpaces(GetCountOfStarsInLine(line));   PrintStars(GetCountOfStarsInLine(line));  }  getch(); } void PrintStars(int line) {  int n;  PrintNChars(n, "*"); } void PrintNChars(int n, char *string) {  int i;  for(i=0; i<n; i++)  {   printf(string);  } } int GetCountOfStarsInLine(int line) {  int stars;  stars=2*line-1;  return 2*line-1; }```
Any ideas? How about fixing the last function there? If there are other areas that need to be adjusted, please let me know
• 10-11-2004
itsme86
In the last function, the stars variable is completely useless. You can just rewrite it like so:
Code:

```int GetCountOfStarsInLine(int line) {   return 2*line-1; }```
• 10-11-2004
BB18
Hmm
Oh, that's true...that was just a bunch of b.s., but I wonder...

I hope it's not too much to ask for, but can someone point me to the right direction in how to deal with my PrintNSpaces function? I wonder what things I should have in there.

If it's not too much trouble...anyway, I would appreciate any feedback.
• 10-11-2004
quzah
I don't understand the question. If you already have a function which prints N characters, why is it hard to figure out how to write a function to print N spaces, especailly since you've already got one which prints N stars?

On an aside, why not just do:
Code:

```printNchars( int n, int c ) {     int x;     for( x = 0; x < n; x++ )         putchar( c ); }```
Quzah.
• 10-14-2004
BB18
Actually
The code you all provided was useful in determining the end result. However, the mistake of the triangle's actual apperance was MINE.

This is what it's supposed to look like:

*
***
*****
*******

I wonder...when I type up the spaces and asterisks, does the triangles still appear slanted? Anyhow, this is the code I used:

Code:

```/*Project 54 Herbert Ortiz Due: October 11, 2004 This program is called the Christmas Tree because it takes on the shape of a tree (triangle) and its height depends largely on the base the user inputs*/ #include <stdio.h> main() {  int base, currentrow, spaces, counter;  //my necessary local variables - currentrow could also be the height  printf("Please enter the base: ");  //input prompt  scanf("%d", &base);  //the value for the base is stored for later manipulation  for(currentrow=1; currentrow<base; currentrow++) //my first for loop  {   for(spaces=0; spaces<base-currentrow; spaces++)  //without this, the triangle would be slanted   {   printf(" ");   }   for(counter=0; counter<(2*currentrow-1)-4; counter++) //this prints the stars   {   printf("*");   }   printf("\n");  }  getch(); }```
I can get the desired triangle when my base is 7. How can I get it to work with bases such as 9 and 11, etc...

The base always has to be an odd integer (as I had already clarified, I believe) and the height of the triangle is half of the base and plus one. h=(b/2)+1 for those visual learners.
If that's the only thing I have to implement, then could someone tell me how to? And what I mean by that is, where do I put it? Thanks.
• 10-16-2004
quzah
Quote:

Originally Posted by BB18
However, the mistake of the triangle's actual apperance was MINE.

This is what it's supposed to look like:

*
***
*****
*******

I wonder...when I type up the spaces and asterisks, does the triangles still appear slanted?

You could always try the preview button. ;) 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.)

Quzah.
• 10-16-2004
BB18
Oh That's A Relief
I see, the preview button. So um...how do I fix that minor problem in my code that will generate the output I desire?

that -4 shouldn't be there...that's my guess...but then again, I'm just making a conjecture.