Watch this video first! <- Click here or on the thumbnail below!
๐โ๏ธ Ho ho ho! Merry Christmas!
Today's exercise is very simple but fun.
All you have to do is to print a Christmas tree to the console.
...with a star on top!
...and a trunk!
But there is a catch... hehehe.
// Example with height of 6
*
#
###
#####
#######
|
The rules are simple:
- The tree should be printed in 3 parts: star, tree, trunk.
- The height should be dynamic. At least 4!
- The star should be printed in the middle of the first line.
- The tree layers should be centered/odd numbers of
#
characters. - The trunk should be printed in the middle of the last line.
- The last layer of the tree should have a margin of 1 space on each side.
The height should be given as an argument to the print_christmas_tree()
function.
Min value: 4
Only even numbers are allowed, e.g.: 4, 6, 8, 10, 12, etc.
// Example with height of 8
*
#
###
#####
#######
#########
###########
|
You can complete the task in 3 different difficulty levels:
Just call the print_christmas_tree()
function with a fixed height as an argument.
e.g.:
print_christmas_tree(6);
You can also use the #define
directive to define a constant.
Then, you may use a combination of for-loops
and printf()
to print the tree.
Call the print_christmas_tree()
function with a dynamic height as an argument from the Command Line.
e.g.:
# UNIX
./main 6
# Windows
main.exe 6
You can use the atoi()
function to convert the string to an integer or go beyond and use the strtol()
function to convert the string to a long integer and do some error checking.
strtol
: https://devdocs.io/c/string/byte/strtol
Everything from the Medium level, but also:
void print_star(size_t height)
and
void print_trunk(size_t height)
may NOT use a for-loop
!
You are only allowed to use 2 for-loops
in total!
Hint: You can use the printf()
function to print multiple characters at once.
printf()
: https://devdocs.io/c/io/fprintf
Check out this if you have no idea how to do it:
https://stackoverflow.com/... <- solution here!!!
Watch the video first where I explain the task in detail.
And remember: Don't Panic!
Click on the src
folder above. Then, click on the main.c
file. This will open the template code in your browser. You can then copy/paste the code into your IDE or download the file.
./src/main.c
The solution code is in the solution
folder.
./solution/main.c
Watch the video on YouTube <- Click here or on the thumbnail below!
Coming soon!
Watch the video on YouTube <- Click here or on the thumbnail below!