Triangle

This is a discussion on Triangle within the C Programming forums, part of the General Programming Boards category; I am trying to generate a triangle using "height" as input. The triangle should look like this: * ** *** ...

  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 10:31 PM.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    /[ code ] needs to be [ / code ]

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Yeah Sorry!

    Ehehe, minor HTML mistake there.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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();
    }
    Last edited by xErath; 10-07-2004 at 11:35 PM.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Exclamation 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");
    }
    Please help me. Thanks!

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Smile Awesome

    I have made the necessary touchups you pointed out. I'll try to see what my output is. Thank you!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Just in case: "Odd" Triangle Challenge (for me)
    One thread for each topic next time - OK
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Exclamation 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. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    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;
    }
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    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.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Arrow 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.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Red face 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.

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, 01:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 06:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21