# Create a christmas tree

topic
////////////////////////////////////////////////////////////
The Christmas tree!

Ask the user to enter a positive integer for the number of stars at the base of the tree. Then construct the tree and print it: What is the base of the largest tree that can fit on the screen?

Please enter an odd number for the base of the tree: 9

*
***
*****
*******
*********
///////////////////////////////////////////////////////////////////////
this is my program is
#include <iostream.h>
#include <stdlib.h>

int main()
{
int nSize = 0;
int nCount = 1;
int size;
int i;

cout << "Enter an odd number for the base of the tree: ";
cin >> nSize;
if (0 == size)
{
cout << "Exiting" << endl;
// Handle errors how you see fit...
exit(0);   // Will exit your program
}
while(0 == (nSize%2))
{
cout << "Enter an odd positive number for the base of the triangle: ";
cin >> nSize;
}

for(i =0; i<nSize;i++)
{
(15-i, 5+i);
for(int j=1; j<=2*i+1; j++)
cout<<"*";              //print the x's
cout<<endl;
}

system("PAUSE");
return 0;
}

can u check this for me, it is not show the solution like :

Please enter an odd number for the base of the tree: 9

*
***
*****
*******
*********
###### Who is Participating?

Commented:
>>>> "for(int j=1; j<(nSize+i+ 1 )/2 ; j++)"

The output of that is:

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

The following should do it

for(int i =1; i<= nSize; i += 2)
{
for(int j=1; j<=(nSize+i)/2 ; j++)
{
if ( j<=(nSize-i)/2 )
cout << " ";
else
cout << "*";
...

Regards, Alex
0

Commented:
for(i =0; i<nSize;i++)
{
(15-i, 5+i);  <<============= what are you doing here?
for(int j=1; j<=2*i+1; j++)
cout<<"*";              //print the x's
cout<<endl;
}
and you cannot do with one nested for-loop. You need two
for(i =0; i<nSize;i++)
{
for(... ; ... ; ... )
//print spaces
for(... ; ... ; ... )
//print stars
cout << endl;
}
0

Commented:
>>>> #include <iostream.h>

use

#include <iostream>
using namespace std;

>>>> #include <stdlib.h>

You don't need it.

>>>>     for(i =0; i<nSize;i++)

change it to

for(i =0; i<nSize;i += 2)

as you need a step of 2 (all lines differ by 2 stars)

>>>>          (15-i, 5+i);

That is nonsense. It does nothing even if it compiles.

>>>>          for(int j=1; j<=2*i+1; j++)

Assume nSize == 9
for i == 1 you need to print 4 spaces and 1 star  (5 chars)
for i == 3 you need to print 3 spaces and 3 stars (6 chars)
for i == 5 you need to print 2 spaces and 5 stars (7 chars)
for i == 7 you need to print 1 space and 7 stars (8 chars)
for i == 9 you need to print 0 spaces and 9 stars (9 chars)

How good is your math? Could you deduce the algorithms from counter i and input nSize ?

No?

Really no?

1. Start value of the j loop is  1
2. End value is  (nSize + i)/2
3. Step is 1
4. You have to print a space as long as j <  (nSize-i)/2
5. You have to print a star else

Note, if you don't understand what I tried to tell you, I will not get angry. But I won't help you if you not at least make an attempt to understand.

Regards, Alex
0

Author Commented:
Is it right??
for(int j=1; j<=2*i+1; j++)
{
for(int j=1; j<(nSize+i)/2 ; j++)
cout<<"*";
cout<<endl;
for(int j=1; j<(nSize-i)/2 ; j++)
cout<<" ";
cout<<endl;

}
0

Commented:
Is it right??

No, the outer loop uses i as counter the inner loop j. Your loop doesn't compile because of that.

I gave you the for statement for the outer loop  (look at my previous post).

You only need one inner loop if you are using my suggestion (steps 1. - 5).

Your first inner for statement is correct.

*But*, you need a block {} after that for statement and and inside of that block you have to decide by an 'if' - 'else' statement whether to print a space character or an asteriks ('*').

Note, you also could use 2 for statements, but the first had to run from 4 to 0 (see 'number of spaces to print' in my previous post) and the second loop from 1 to 9, using a step of 2 (see 'number of stars to print').

Regards
0

Commented:
>>Your first inner for statement is correct.
but cout is incorrect.

Continuing from what alex said...
Assume nSize == 9
for i == 1 you need to print 4 spaces and 1 star  (5 chars)
for i == 3 you need to print 3 spaces and 3 stars (6 chars)
for i == 5 you need to print 2 spaces and 5 stars (7 chars)
for i == 7 you need to print 1 space and 7 stars (8 chars)
for i == 9 you need to print 0 spaces and 9 stars (9 chars)

note that i goes from 1 to nSize in increments of 2. this forms your outer loop.
then you need one inner loop to print some spaces. number of spaces decrements by 1 as i increases by 2.
the other inner loop prints i stars. (or you could use if/else as alex said).

side note: It is very tempting for beginners to use i and j as counter variables. To make code a little more readable try using r(for rows instead of i) and c(for cols instead of j).
0

Author Commented:

for(int j=1; j<=2*i+1; j++)
{
for(int j=1; j<(nSize+i)/2 ; j++)
{
if ( j<(nSize-i)/2 )
{
cout<<"*";
}
else
{
cout<<" ";
}
}

}
0

Commented:
>>>>      for(int j=1; j<=2*i+1; j++)
>>>>      {
>>>>          for(int j=1; j<(nSize+i)/2 ; j++)

Cuong, you can't have a nested loop taking the same counter variable 'j' twice

For nSize == 9 we need counter 'i' starting with 1, then 3, then 5, then 7 and finally 9

Therefore the first for statement is

int i;
for(i =1; i<= nSize; i += 2)       // outer loop using i and step is 2

The inner loop seems to be ok beside of toggling space character and star character in the if-else statement.

Regards

0

Author Commented:
But still it doesnt show the christmas tree as well even i changed the code on the ways that u told me???
0

Commented:
Could you post your full code til now?
0

Author Commented:
/*******************************************************************
Project         54
Author          Cuong Nguyen
Purpose         Write a program to create The Christmas tree!
*******************************************************************/
#include <iostream.h>
#include <stdlib.h>

int main()
{
int nSize = 0;
int nCount = 1;
int size;
int i;

cout << "Enter an odd number for the base of the tree: ";
cin >> nSize;
if (0 == size)
{
cout << "Exiting" << endl;
// Handle errors how you see fit...
exit(0);   // Will exit your program
}

while(0 == (nSize%2))
{
cout << "Enter an odd number for the base of the triangle: ";
cin >> nSize;
}

for(i =1; i<= nSize; i += 2)
{
for(int j=1; j<(nSize+i)/2 ; j++)
{
if ( j<(nSize-i)/2 )
{
cout<<" ";
}
else
{
cout<<"*";
}
}

}

system("PAUSE");
return 0;
}
0

Commented:
Replace

#include <iostream.h>

by

#include <iostream>
using namespace std;

cout << endl;

above second for loop (j-loop).

Regards, Alex

0

Commented:
change "for(int j=1; j<(nSize+i)/2 ; j++)" to "for(int j=1; j<(nSize+i+ 1 )/2 ; j++)"
and add "cout << endl;" like alex said.

iostream.h is not standard so u should use iostream instead but that wont affect the output.
0

Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.