How do I make a Christmas tree with stars using nested for in C++?
In this article, I would like to share with you all one of my answer to a question that asked in Quora. Question is “How do I make a Christmas tree with stars using nested for in C++?” So here is the question and it’s answer
How do I make a Christmas tree with stars using nested “for” in C++?
My answer does not contains any specific code parts, but I wanted give the algorithm of the problem. Of course it is more important, because I beleive every programmer needs to solve their problems by themselves. To solve the problem, you need to understand it as much as you can, so that you will be capable of giving the optimal code for the problem. While doing this, algorithms can be helpful to you, so try to read other developers codes, and try to understand their logic in it.
Here is my answer below:
This is not good for asking whole answer in here, you need to search this through the internet and think more than you searched.
Let me describe you what should you algorithm look like.
There will be 2 main “for” loops, one for head and one for bottom of the tree.
Starting from the head, you need two more for loops for handling empty spaces and stars(I assume you want your tree with the star operators).
Let’s try to write it.
My starting variables for controlling my loops
int s; int c = 15; int x;
Now I’ll start writing my first main for loop;
for(s=1; s <= c; s++)
// This first for loop will control the empty spaces
for(x = c; x >=s; x--)
std::cout << " ";
//This second one will control the star operators
for(x = 1; x <=s; x++)
std::cout << "*";
// Now second for loop for the bottom of the tree
for(int b = 1; b<9;b++)
std::cout << " ";//This line will break through the body of the tree
for(int d = 1; d < 9; d++)
std::cout << "*";