Console Window Christmas Tree
As a festive programming challenge, programmers are tasked with creating a Christmas Tree out of asterisks, '*', which is the ASCII character code '\xCE'. The purpose of this challenge is to use for loops and setw( ) from the <iomanip> library to create The Desired Output. As such, you cannot simply output a string of spaces and asterisks.The solution requires an understanding of for loops and an understanding of what is necessary mathematically. Don't let that scare you, it's not difficult math: Addition, subtraction and possibly multiplication. There are many possible ways to achieve the goal. This is a problem which requires some complex thought so don't get discouraged if it doesn't come to you right away.
To start, let's take a look at The Desired Output.
The Desired Output |
* *** ***** ******* ********* |
Tackling the Tree
First, we notice that the count of characters, number of asterisks, that are output each line starts with one character and increases by two every line after. Also, starting with the number one and adding two each time ensures that the number will always be the same, either an even or odd value, that it was originally.Why is it important that the number of characters being output is an odd count? It is necessary so that a single character acting as the center of the tree. If we had an even number of characters there wouldn't be one center character and the tree would not be as centered.
Knowing that we will want to store the number of characters we are currently sending to the output we can declare an unsigned int variable, which we'll name x. We'll also want to declare a variable to hold the current line, which will hold our current line, unsigned int y.
The number of lines, the final and desired height of the tree, which will not change throughout the program we can declare as an const unsigned int variable, HEIGHT.
How do we know where to go from there? Well, that's tricky but we know that we're going to need a basic program setup so let's start with what we have so far:
The Start of the Tree |
main.cpp |
//===================================INCLUDES================================== #include <iostream> //Included for text output, cout. #include <iomanip> //Included for setw(), fill(), output manipulation. //Assuming namespace ::std. using namespace std; //==================================MAIN ENTRY================================= int main( void ) { //The desired height of the tree. const unsigned int HEIGHT = 5; //The current width of the tree. unsigned int x; //The current height of the tree. unsigned int y; //The for loop solution //... //End the program, returning zero. return ( 0 ); } |
Using what we know, that the x variable depends on the y, we can assume the loop dealing with height will be the first loop which will then have a second loop that outputs the correct number of characters.
The count of characters from left to right, will be depends on the height. We know that the height, loop dealing with the y will be the outer-most for loop and that the inner-loop, the x loop, will have a condition which is directly affected by the current line number.
Part of a Possible Solution |
main.cpp, for loop part 1 |
//Set current line, y, to 1 since we'll need it // to calculate number of asterisks to output, x. for ( y = 1; y < HEIGHT; y++ ) { //Set width before the asterisk(s) to HEIGHT minus the current line, y. cout << setw( HEIGHT - y ); //Consider what conditional would be appropriate. for ( x = 1; x < conditional ; x++ ) { cout << '*'; } cout << endl; } |
Try a few things, again -- there are many possible solutions. The following is the complete code of one solution.
One Possible Complete Solution:
A Completed Christmas Tree |
main.cpp |
//===================================INCLUDES================================== #include <iostream> //Included for text output, cout. #include <iomanip> //Included for setw(), fill(), output manipulation. //Assuming namespace ::std. using namespace std; //==================================MAIN ENTRY================================= int main( void ) { //The desired height of the tree, plus one. const unsigned int HEIGHT = 6; //The current width of the tree. unsigned int x; //The current height of the tree. unsigned int y; //The for loop solution. //Loop through each line, up to a count of HEIGHT - 1 times. for ( y = 1; y < HEIGHT; y++ ) { //Set width before the asterisk(s) to HEIGHT minus the current line, y. cout << setw( HEIGHT - y ); //Loop until the current asterisk, x, is less than twice the line, y. for ( x = 1; x < y * 2; x++ ) { //Output the asterisk. cout << '*'; } //End the output to the current line. cout << endl; } //End the program, returning zero. return ( 0 ); } |
No comments:
Post a Comment