draw Tree pattern based on inputed height

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d0ugg
    New Member
    • Jan 2007
    • 41

    #1

    draw Tree pattern based on inputed height

    I'm suppose to do a little program that asks for the height and draw a simple tree.

    Problem

    1 - Promt and read the height
    2 - Test for height in bounds
    3 - draw the base
    4 - draw the trunk
    5 - draw the cone

    Here is my source code:

    #include <iostream>
    using namespace std;

    int main()
    {
    int height;

    cout << "Please enter a height for the pyramid: ";
    cin >> height;

    if (height < 3)
    {
    cout << "This height can't be calculated.\nPl ease enter a number between 3 and 15. Thank you. ";
    exit(0);
    }

    else if (height > 15)
    {
    cout << "This height can't be calculated.\nPl ease enter a number between 3 and 15. Thank you. ";
    exit(0);
    }

    for (int level = 0; level < height; level++)
    {
    for (int spaces = 0; spaces < height -1 - level; spaces++)
    cout << ' ';
    for (int b1 = 0; b1 < 2 - 1; b1++)
    cout << '/';
    cout << endl;
    }
    return 0;
    }


    So I just need to know how to do the other side of the tree with '\'.. I got the spaces already and the left side of the tree, but I cant figure out how to get the right side and the base.

    Thank you very very much,

    Doug
  • jeffb
    New Member
    • Feb 2007
    • 4

    #2
    So, you're looking for final output something like:

    Code:
      /\
     /  \
    /____\
      |_|
    (Sorry - new here, and don't know if there's any way to preserve spacing other than the "code" tag)

    Right? Well, it looks like you already have a method of determining how far to space the left side of the tree... All you need to do is figure out how to calculate the spaces for the "middle" of the tree (between the "/" and "\"), and once you reach the bottom, a way to calculate the "center" so that you can space over for the trunk...

    You're well on your way - I'm sure you can manage it....

    Comment

    • FB2006 TEAM
      New Member
      • Feb 2007
      • 7

      #3
      Well, I not so good in explanations, so I just show you my way to solve this problem (by using C). I’m sure you will understand it! If not, I will try to explain it on my “broken” English.

      Code:
      #include <stdio.h>
      
      void F_DrawWhiteSpace (int);			// This function called from all other functions
      void F_DrawLeftSideOfTree (int);		// This function called from "main"
      void F_DrawRightSideOfTree (int, int);	// This function called from "main"
      void F_DrawTrunkOfTree (int, int, int);	// This function called from "main"
      void F_DrawSideOfTrunk (void);			// This function called from "F_DrawTrunkOfTree"
      void F_DrawBaseOfTree (int);			// This function called from "F_DrawRightSideOfTree" and "F_DrawTrunkOfTree"
      
      void main ()
      {
      	int tree_height = 0;
      	int current_level = 0;	// This variable indicates current level in pyramid
      	int trunk_length = 2;	// This variable indicates the length of trunk
      	int trunk_width = 1;	// This variable indicates the width of trunk. IMPORTANT !=> trunk_width += trunk_width <=!
      
      	printf ("Please enter a height for the pyramid: ");
      	scanf ("%d", &tree_height);
      
      	// !=> Your condition <=!
      
      	for (current_level = tree_height; current_level > 0; current_level--)
      	{
      		F_DrawLeftSideOfTree (current_level);
      		F_DrawRightSideOfTree ((tree_height - current_level) * 2, current_level);
      	}
      	F_DrawTrunkOfTree (tree_height, trunk_length, trunk_width);
      }
      
      void F_DrawLeftSideOfTree (int current_level) // This function draws the left side of pyramid
      {
      		F_DrawWhiteSpace (current_level);
      		printf ("/");
      }
      
      void F_DrawRightSideOfTree (int distance_to_right_side, int f_base_of_pyramid) // This function draws the right side of pyramid
      {
      	if (f_base_of_pyramid != 1) // This condition checks if has been reached the base of pyramid. !=> "f_" indicates "flag" <=!
      	{
      		F_DrawWhiteSpace (distance_to_right_side);
      		printf ("\\\n");
      	}
      	else
      	{
      		F_DrawBaseOfTree (distance_to_right_side);
      		printf ("\\\n");
      	}
      
      }
      
      void F_DrawTrunkOfTree (int tree_height, int trunk_length, int trunk_width) // This function draws the trunk of tree by using another three functions
      {
      		for (; trunk_length > 0; trunk_length--)
      		{
      			if (trunk_length != 1) // This condition checks if has been reached the base of turn
      			{
      				F_DrawWhiteSpace (tree_height - trunk_width );
      				F_DrawSideOfTrunk (); // Right side of trunk
      				F_DrawWhiteSpace (trunk_width + trunk_width );
      				F_DrawSideOfTrunk (); // Left side of trunk
      				printf ("\n");
      			}
      			else
      			{
      				F_DrawWhiteSpace (tree_height - trunk_width );
      				F_DrawSideOfTrunk (); // Right side of trunk
      				F_DrawBaseOfTree (trunk_width + trunk_width);
      				F_DrawSideOfTrunk (); // Left side of trunk
      			}
      		}
      		printf ("\n");
      }
      
      void F_DrawSideOfTrunk (void) // This function draws right and left side of trunk
      {
      	printf ("|");
      }
      
      void F_DrawWhiteSpace (int tree_height) // This function create the "white" spaces in pyramid and trunk
      {
      	for (; tree_height > 0; tree_height--)
      	{
      		printf (" ");
      	}
      }
      
      void F_DrawBaseOfTree (int base_length) // This function draws the base of pyramid and trunk
      {
      	for (; base_length > 0; base_length--)
      	{
      		printf ("_");
      	}
      }

      Comment

      Working...