473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

draw Tree pattern based on inputed height

41
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.\nPlease enter a number between 3 and 15. Thank you. ";
exit(0);
}

else if (height > 15)
{
cout << "This height can't be calculated.\nPlease 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
Feb 2 '07 #1
2 4705
jeffb
4
So, you're looking for final output something like:

Expand|Select|Wrap|Line Numbers
  1.   /\
  2.  /  \
  3. /____\
  4.   |_|
  5.  
(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....
Feb 2 '07 #2
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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void F_DrawWhiteSpace (int);            // This function called from all other functions
  4. void F_DrawLeftSideOfTree (int);        // This function called from "main"
  5. void F_DrawRightSideOfTree (int, int);    // This function called from "main"
  6. void F_DrawTrunkOfTree (int, int, int);    // This function called from "main"
  7. void F_DrawSideOfTrunk (void);            // This function called from "F_DrawTrunkOfTree"
  8. void F_DrawBaseOfTree (int);            // This function called from "F_DrawRightSideOfTree" and "F_DrawTrunkOfTree"
  9.  
  10. void main ()
  11. {
  12.     int tree_height = 0;
  13.     int current_level = 0;    // This variable indicates current level in pyramid
  14.     int trunk_length = 2;    // This variable indicates the length of trunk
  15.     int trunk_width = 1;    // This variable indicates the width of trunk. IMPORTANT !=> trunk_width += trunk_width <=!
  16.  
  17.     printf ("Please enter a height for the pyramid: ");
  18.     scanf ("%d", &tree_height);
  19.  
  20.     // !=> Your condition <=!
  21.  
  22.     for (current_level = tree_height; current_level > 0; current_level--)
  23.     {
  24.         F_DrawLeftSideOfTree (current_level);
  25.         F_DrawRightSideOfTree ((tree_height - current_level) * 2, current_level);
  26.     }
  27.     F_DrawTrunkOfTree (tree_height, trunk_length, trunk_width);
  28. }
  29.  
  30. void F_DrawLeftSideOfTree (int current_level) // This function draws the left side of pyramid
  31. {
  32.         F_DrawWhiteSpace (current_level);
  33.         printf ("/");
  34. }
  35.  
  36. void F_DrawRightSideOfTree (int distance_to_right_side, int f_base_of_pyramid) // This function draws the right side of pyramid
  37. {
  38.     if (f_base_of_pyramid != 1) // This condition checks if has been reached the base of pyramid. !=> "f_" indicates "flag" <=!
  39.     {
  40.         F_DrawWhiteSpace (distance_to_right_side);
  41.         printf ("\\\n");
  42.     }
  43.     else
  44.     {
  45.         F_DrawBaseOfTree (distance_to_right_side);
  46.         printf ("\\\n");
  47.     }
  48.  
  49. }
  50.  
  51. void F_DrawTrunkOfTree (int tree_height, int trunk_length, int trunk_width) // This function draws the trunk of tree by using another three functions
  52. {
  53.         for (; trunk_length > 0; trunk_length--)
  54.         {
  55.             if (trunk_length != 1) // This condition checks if has been reached the base of turn
  56.             {
  57.                 F_DrawWhiteSpace (tree_height - trunk_width );
  58.                 F_DrawSideOfTrunk (); // Right side of trunk
  59.                 F_DrawWhiteSpace (trunk_width + trunk_width );
  60.                 F_DrawSideOfTrunk (); // Left side of trunk
  61.                 printf ("\n");
  62.             }
  63.             else
  64.             {
  65.                 F_DrawWhiteSpace (tree_height - trunk_width );
  66.                 F_DrawSideOfTrunk (); // Right side of trunk
  67.                 F_DrawBaseOfTree (trunk_width + trunk_width);
  68.                 F_DrawSideOfTrunk (); // Left side of trunk
  69.             }
  70.         }
  71.         printf ("\n");
  72. }
  73.  
  74. void F_DrawSideOfTrunk (void) // This function draws right and left side of trunk
  75. {
  76.     printf ("|");
  77. }
  78.  
  79. void F_DrawWhiteSpace (int tree_height) // This function create the "white" spaces in pyramid and trunk
  80. {
  81.     for (; tree_height > 0; tree_height--)
  82.     {
  83.         printf (" ");
  84.     }
  85. }
  86.  
  87. void F_DrawBaseOfTree (int base_length) // This function draws the base of pyramid and trunk
  88. {
  89.     for (; base_length > 0; base_length--)
  90.     {
  91.         printf ("_");
  92.     }
  93. }
Feb 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Florida Draw | last post by:
------=_NextPart_000_0012_1D5428A2.65C196F8 Content-Type: text/plain Content-Transfer-Encoding: 8bit Sign-up for the FREE Holiday Giveaway Draw You must register to be eligible for the draw....
0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
1
by: Foodbank | last post by:
Hi, I'm currently teaching myself C data structures in preparation for a job transition and I've just passed the point in which radix sorting was covered in my book. I've recently finished a...
3
by: Arnold the Aardvark | last post by:
I am creating a custom draw tree view based on CTreeCtrl. When the control is re-sized the background is blanked completely, resulting in a horrible flicker. I've seen various suggestions...
1
by: zxo102 | last post by:
Hi everyone, I have tried two days to figure out how to draw the image in wx.BufferedDC on the page created by AddPage of wx.Notebook but still got no clue. The attached example works fine. If...
1
by: smilecry | last post by:
I am trying to create a tree table (javascript code was adopted from online source) but the rowspan in td tag does not work well when I toggle the rows. Here is the sample code, notice the row "4"...
0
by: benfly08 | last post by:
Hi, guys. I have a program to draw bar/pie chart based on the data i hard coded in it. However, my image comes with "BLACK" background color. I don't know how to fix this. The code snippet is...
1
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.