christmas tree

October 6, 2009 at 18:54:34
Specs: Windows 7
This is the code for my christmas tree.
How do i output asterisks to make the stump of the tree? Currently if you input ten asterisks it will display ten rows of asterisks, How do i make the code so that the stump is exaclty half of the input of the tree? Like if they input ten, i want the stump to be five rows.

*
**
***
****
*****
******
**
**
**
[code]
{
int height=0;
int i=0;
int j=0;
{
cout << "Please enter tree height." << endl;
cout <<""<<endl;
cin >> height;
} while (height < 1);
for(j=0; j<height; j++)
{ for(i=0; i<=j; i++)

{ cout << "*";
} cout << endl;
}
[/code]


See More: christmas tree

Report •


#1
October 6, 2009 at 19:42:16
the stump is exaclty half of the input of the tree?
stump = height / 2;


Report •

#2
October 6, 2009 at 20:34:01
Is it just me or does that while loop never finish?

I would use a for loop over height with an if (){}else{} in it and another for loop in the if statement.


Batch Variable how to


Report •

#3
October 6, 2009 at 20:36:22
Yea thats what i had already but im confused

code
------------------
{
int height=0;
int i=0;
int j=0;
int s=0; //stump
{
cout << "Please enter tree height." << endl;
cout <<""<<endl;
cin >> height;
} while (height < 1);
for(j=0; j<height; j++)
{ for(i=0; i<=j; i++)
for(s=0; s == height / 2; s++)

{ cout << "*";
} cout << endl;
}
---------------------------------------
/code


Report •

Related Solutions

#4
October 6, 2009 at 21:46:51
First of all that while loop is a "do-while" loop with out the "do". Adding the "do" before the while block(before the "{") will ensure that it does as intended, only pass when "height" is greater than zero.

I presume that you want trunk height to be total height / 2 and trunk width to be the widest branch width /2. You have to decide which way you want to round on odd numbers because you can't output half a character to the console.


Yea thats what i had already but im confused

I could give you the answer, but then you may not learn as much.

Think of it this way: I want to loop over the value of "height", if the value looping over height is less than half of height then the branch width is the current value.

If the current loop value is over half way the width is half the size of the widest branch.


Remember you can test against a sum of values with if, while or for. For example:

int number = 6;
int height = 10;
if (number > height / 2)
{
    cout << number << " is greater than half of "
        << height << endl;
}


Batch Variable how to


Report •


Ask Question