Hacker Challenge Solution: Christmas Tree
Let's look at one of the solutions for a Christmas tree pattern.
Printing the Christmas tree
Your code should print the following Christmas tree:
Playground for your implementation
Write your code here.
#include <iostream>using namespace std;void printASymbolKTimes(char s, int k){for(int t=1; t<=k; t++)cout << s;}// Write the code for helping functions here// Write the code for the main christmasTree function herevoid christmasTree(int h){cout<<"Height:"<<h<<endl;// Write your code here}int main(){christmasTree(10);return 0;}
We can divide the Christmas tree into three main parts (from the image above):
- The triangles
- The trunk
- The floor
1. The triangles
Note that we have three triangles in the above image. These three triangles are the same with respect to height, but every part has a different range. The top triangle has a range to , the second has a range of to and the third one with to .
We can make a printing function for the triangle with range passed as parameters along with the height, and then call the same function three times with different ranges to print these three triangles’ portions.
Printing the triangle
Let us make the pattern table for the following figure where h
(height) is equal to 10.
The printTriangle() Function
Line # | Spaces | Dashes (-) |
1 | h -1 | 1 * 2 - 1 |
2 | h - 2 | 2 * 2 - 1 |
3 | h - 3 | 3 * 2 - 1 |
... | ... | ... |
ln | h - ln | ln * 2 -1 |
The implementation of the printTriangle()
function will be as follows:
void printTriangle(int si , int ei, int h){for(int ln=si; ln<=ei; ln++){printASymbolKTimes(' ', h-ln);printASymbolKTimes('-', ln*2-1);cout << endl;}}
Suppose we call the printTriangle()
function with si = 3
, ei = 8
, and h = 10
, then this function will display the following triangle:
Instruction: Add the printTriangle()
function in the playground above.
Drawing triangles
To print the above three triangles, we call the printTriangle()
function with the following parameters:
- The first triangle starts printing from the top to the height of
2*h/3
.printTriangle(1, 2*h/3,h);
- The second triangle starts printing from the
1*h/4+1
to the height of3*h/4
.printTriangle(1*h/4+1, 3*h/4,h);
- The third triangle from the
1*h/2+1
to the height ofh
.printTriangle(1*h/2+1, h,h);
Instruction: Add these lines in the christmasTree()
function above and, execute the code. You should see half of the tree already on the console.
Let’s move toward printing the trunk of the Christmas tree.
2. The trunk
To print the trunk, we first need to figure out the width and height of the trunk. Look at the following figure below to understand the width and the height of a trunk:
To print the trunk, we just need to follow these three steps:
- Print the
(2*h-1)*1/3
spaces. - Print the
(2*h-1)*1/3
dashes. - Move to the next line and repeat the above two steps,
2*h/3
times.
The pattern table will look like the following:
The printTrunk() Function
Line # | Spaces | Dashes (-) |
1 | (2*h-1)/3 | (2*h-1)/3 |
2 | (2*h-1)/3 | (2*h-1)/3 |
3 | (2*h-1)/3 | (2*h-1)/3 |
... | ... | ... |
2*h/3 | (2*h-1)/3 | (2*h-1)/3 |
Instruction: Write a printTrunk(int h)
function in the above playground, call it after printing the triangles and execute the code. You should see the entire picture of the tree. Now, we’ll move on to the floor.
3. The floor
As we can see, the last line of the tree is the floor. To print the floor, we just need to print the dashes 2*h-1
times.
The printFloor() Function
Line # | Dashes (-) |
1 | 2*h-1 |
Instruction: Write a printFloor(int h)
function in the above playground, call it after printing the trunk, and execute the playground. You should be able to see the complete required output. Change the passed value to some other heights and see whether the output is correct with respect to every height or not.
Hurrah! You are done with Christmas tree printing.
The complete implementation
Just for reference, the complete implementation will look like the following:
#include <iostream>using namespace std;void printASymbolKTimes(char s, int k){for(int t=1; t<=k; t++)cout << s;}void printTriangle(int si , int ei, int h){for(int ln=si; ln<=ei; ln++){printASymbolKTimes(' ', h-ln);printASymbolKTimes('-', ln*2-1);cout << endl;}}void printTrunk(int h){int base_length = 2*h-1;for(int ln=1; ln<=2*h/3; ln++){printASymbolKTimes(' ', base_length*1/3);printASymbolKTimes('-', base_length*1/3);cout <<endl;}}void printFloor(int h){int base_length = 2*h-1;printASymbolKTimes('-', base_length);}void christmasTree(int h){cout<<"Height:"<<h<<endl;printTriangle(1, 2*h/3,h);printTriangle(1*h/4+1, 3*h/4,h);printTriangle(1*h/2+1, h,h);printTrunk(h);printFloor(h);}int main(){christmasTree(10);return 0;}