Creating a Christmas Tree

Hello, I have an assignment to create a christmas tree in c++ and I have tried everything i can think of and it is driving me crazy.

I am supposed to use 3 nested loops and this is what i have so far :

#include <stdio.h>



int main()
{


int x, y;

for (x=1; x<=7; ++x)
{
for (y=1; y<=x; ++y)
{

printf (" ");
}
for (y=1; y<=x; ++y)
{

printf ("*");

}

printf ("\n");
}

Can anyone give me any advice on how to improve this? Because it looks nothing like a christmas tree lol.
Christmas tree that looks like what, exactly? What shape?

Since you say it needs three nested loops, maybe it's something like this, at a guess?
      *       1
     ***      3
    *****     5
   *******    7
  *********   9
    *****     5
   *******    7
  *********   9
 ***********  11
************* 13
etc...

If so, it's probably that each 'segment' of the tree is an iteration of the outer loop, each line is an iteration of the middle loop, and each character is an iteration of the inner loop. Correct?
Last edited on
it should look like


*
***
*****
*******
*********
***********
*************
***
***

sorry im not sure how to create it in a window like you did...the forum moves the whole tree to the left when i post it and messes it up



the instructer wants us to use 3 loops to create it....i will also be creating the trunk of the tree in another loop but i can prolly figure that out
Last edited on
Your existing program is close. Can you see the pattern with the number of spaces as compared to the number of stars? Your looping conditions are your problem.

Edit: Also you can put [output] before your christmas tree and then [/output] after to make it keep the leading spaces, otherwise the forum removes the leading spaces. Similarly, you can use [code][/code] around your code to syntax highlight it:
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;
}
Last edited on
when i run the program it does the spaces correctly kind of but it is like it does it backwards and i've been trying to figure this out for about 4 hours now lol.

I will continue and let you know if i do figure it out. Thanx for your help.
I see. let me try this again.


           *
          ***
        *****
      *******
    *********
  ***********
*************
          ***
          ***


this is what is should look like.

this is my current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>



int main()
{		

	
	int x, y;
		
		for (x=1; x<=7; ++x)
			{
				for (y=1; y<=x; ++y)
				{
					
					printf (" ");
				}
				for (y=1; y<=x; ++y)
				{
					
					printf ("*");
					
				}
				
			printf ("\n");
			}
		
		
		
	


}


and this is what this code produces for me:


 *
  **
   ***
    ****
     *****
      ******
       *******




For some reason it still isnt getting the whole christmas tree at the beginning but i think you get the idea . thanx again!
I fixed your program's formatting (I noticed you used pre-increment, you're automatically awesome!) and ran it myself:
http://ideone.com/4JC5f7

The problem is the loop condition for your spaces. It is the same as your loop condition for your stars, meaning 1 star = 1 space. You need to use a tweak to make the space loop run more times at the beginning of the outer loop and less times at the end:
http://ideone.com/5XgHaZ
not quite sure what pre increment is but i will accept being awesome lol.

I need to take a break as im getting a massive headache now.
For reference:
1
2
3
4
int x = 3;
int y = 3;
int a = ++x; //pre-increment: x equals 4, a equals 4
int b = y++; //post-increment: y equals 4, b equals 3 (the old value of y) 
It drives me crazy (don't ask why) when people use post-increment in their for loops, but you used pre-increment so you are therefore awesome and deserve to take a break from this for a while.
Last edited on
Topic archived. No new replies allowed.