C++ to become christmas tree

This is what i have done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	int i, j;
	
	
	for(j = 1; j <= 5; j++)
	{
	   for(i = 1; i <= 5; i++)
	   {
	      cout << " ";
	   }
	   
	   for(i = 1; i <= j; i++)
	   {
		  cout << "*";
	   }
	    
	   cout << endl;
	}
}


no problems getting either
1
2
3
4
5
6
7
8
9
10
11
12
13
*
**
***
****
*****

OR

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


but what i want is

 
the shape of a christmas tree


anyone knows what i need to add inorder to get it?

thanks
Last edited on
Use spaces as padding. Since a space is always the same length (as is a *), it's easy to calculate the amount of spaces required. It gets much easier if you decide to use only uneven amount of *s.
hmm.. meaning ? sorry i am kinda new to this.
You need to increase the ammount of stars each time. So instead of 1 each time, you want to add 2, to the total amount of stars.

Edit:

When you solve this, I'll show you the solution I just wrote.
Last edited on
for the shape of a Christmas tree, you'd need an isoceles triangle, and a rectangle... looking sumwhat like this:

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


So try getting this...
Last edited on
when i am able to solve it i have no reason to see yours isn't it? zzz
Better than just giving you the answer... You'll learn nothing from it!

At least have a try at it and post your code...
Just determine the pattern. It might be easier to start from the bottom.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	int i, j;
	int x = 5;
        int y = 1;

	for(j = 1; j <= 5; j++)
	{
	   for(i = 1; i <= x; i++)
	   {
	      cout << " ";
	   }
           x--;

	   for(i = 1; i <= y; i++)
	   {
		  cout << "*";
	   }
	   y += 2;

	   cout << endl;
	}
}


X and Y control the loops.
Blackouti386, thanks.

i have found the answer already

@Lynx876 obviously i have already tried before coming here desperately for answer.... is not like i did nothing to it. zzz

Anyway thanks all.
Since you have the answer now, here's my solution. Same as BlackOut's, but a dynamic stump creation, whereas it will make the stump based on the size of the tree:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
int _tmain(int argc, _TCHAR* argv[])
{
	int stars = 1;

	//size is all you need to change, to automatically
	//change the size of the stump

	//size of tree in height
	int size = 10;

	int stumpHeight = size / 3;
	int stumpWidth = size / 2;

	for( int total = size; total > 0; --total )
	{
		//control the amount of spaces
		for( int i = ( total - 1 ); i > 0; --i )
			std::cout << " ";

		//control the amount of stars
		for( int j = 0; j < stars ; ++j )
			std::cout << "*";

		//next row needs 2 extra stars
		stars += 2;

		std::cout << '\n';
	}


	//create the stump
	for( int i = 0; i < stumpHeight; ++i )
	{
		//spaces to the center of the tree
		//so that the stump is centered
		for( int j = 0; j < ( size - ( stumpWidth - ( stumpWidth / 2 ) ) ); ++j )
			std::cout << " ";

		for( int k = 0; k < stumpWidth; ++k )
			std::cout << "*";

		std::cout << '\n';
	}

	return 0;
}
Instead of editing out code from an older post, I thought I'd make a new one.

I just updated this one, now it has a percentage of decorations and a plant pot. And you could easily add some 'cin' to get the user to specify chars.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
int _tmain(int argc, _TCHAR* argv[])
{
	//for a random number
	srand( unsigned( time( NULL ) ) );


	/*///////////////////*/
	/*     Controls:     */
	/*///////////////////*/
	
	//size of tree in height
	int size = 20;

	//percentage of decorations
	int percentage = 25;

	/*   Visual   */

	//tree top
	char treeTop = '^';

	//tree stump
	char treeStump = '|';

	char pot = ( char )219;

	//character for decoration
	char decoration = 'o';

	/*///////////////////*/
	

	//first star
	int stars = 1;

	int stumpHeight = size / 3;
	int stumpWidth = size / 3;

	int potHeight = stumpHeight / 2;
	int potWidth = stumpWidth + 2;

	int randomNumber = 0;

	for( int total = size; total > 0; --total )
	{
		//control the amount of spaces
		for( int i = ( total - 1 ); i > 0; --i )
			std::cout << " ";

		//control the amount of stars
		for( int j = 0; j < stars ; ++j )
		{
			randomNumber = rand() % 100 + 1;

			//decorations wont go on the outline of the tree
			if( ( j > 0 ) && ( ( stars - j ) > 1 ) )
			{
				if( randomNumber < percentage )
					std::cout << decoration;
				else
					std::cout << treeTop;
			}
			else
				std::cout << treeTop;
		}

		//next row needs 2 extra stars
		stars += 2;

		std::cout << '\n';
	}


	//create the stump
	for( int i = 0; i < stumpHeight; ++i )
	{
		//spaces to the center of the tree
		//so that the stump is centered
		for( int j = 0; j < ( size - ( stumpWidth - ( stumpWidth / 2 ) ) ); ++j )
			std::cout << " ";

		for( int k = 0; k < stumpWidth; ++k )
			std::cout << treeStump;

		std::cout << '\n';
	}

	//create the plant pot
	for( int i = 0; i < potHeight; ++i )
	{
		//spaces to the center of the tree
		//so that the stump is centered
		for( int j = 0; j < ( size - ( potWidth - ( potWidth / 2 ) ) ); ++j )
			std::cout << " ";

		for( int k = 0; k < potWidth; ++k )
			std::cout << pot;

		std::cout << '\n';
	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.