Computing.Net > Forums > Programming > christmas tree

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

christmas tree

Reply to Message Icon

Name: cno no
Date: October 6, 2009 at 18:54:34 Pacific
OS: Windows 7
Subcategory: C/C++
Comment:

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]



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 6, 2009 at 19:42:16 Pacific
Reply:

the stump is exaclty half of the input of the tree?

stump = height / 2;


0

Response Number 2
Name: Judago
Date: October 6, 2009 at 20:34:01 Pacific
Reply:

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


0

Response Number 3
Name: cno no
Date: October 6, 2009 at 20:36:22 Pacific
Reply:

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


0

Response Number 4
Name: Judago
Date: October 6, 2009 at 21:46:51 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: christmas tree

print the song The Twelve Days of Christmas www.computing.net/answers/programming/print-the-song-the-twelve-days-of-christmas/20108.html

Algorithm - B Trees www.computing.net/answers/programming/algorithm-b-trees/2190.html

Binary tree www.computing.net/answers/programming/binary-tree/14539.html