christmas tree

C

curiousEngine

Hello. I've been able to write a tree in pascal with 21 stars. But in C
++, given the new pre-requisite to adjust for tiers, i think i need
help!

The tree consists of a series of tiers (three in this case) of
increasing size. Write a
program to produce such a display having prompted the user for the
number of tiers.
You could try putting a few baubles on the tree (using o or O).
************************************/

#include<iostream>
using namespace std;

const char star = '*';
const char space = ' ';

void main(){

int noStar(1);
/* int numTiers(0);

cout<<"Enter the number of tiers for the tree: ";
cin>> numTiers;
*/

for(int i(1); i <= 20; i++){
for (int j(1); j = (21 - i); j++)
cout<<space;
for (int c(1); c = (2*i - 1); c++)
cout<<star;
}
cout<<endl;
}
 
J

Jim Langston

curiousEngine said:
Hello. I've been able to write a tree in pascal with 21 stars. But in C
++, given the new pre-requisite to adjust for tiers, i think i need
help!

The tree consists of a series of tiers (three in this case) of
increasing size. Write a
program to produce such a display having prompted the user for the
number of tiers.
You could try putting a few baubles on the tree (using o or O).
************************************/

#include<iostream>
using namespace std;

const char star = '*';
const char space = ' ';

void main(){

int noStar(1);
/* int numTiers(0);

cout<<"Enter the number of tiers for the tree: ";
cin>> numTiers;
*/

for(int i(1); i <= 20; i++){
for (int j(1); j = (21 - i); j++)
cout<<space;
for (int c(1); c = (2*i - 1); c++)
cout<<star;
}
cout<<endl;
}

Obviously homework. I do notice, however, that you accept the input for
numTiers, then never use it. Don't you think you should?
 
A

anon

curiousEngine said:
Hello. I've been able to write a tree in pascal with 21 stars. But in C
++, given the new pre-requisite to adjust for tiers, i think i need
help!

The tree consists of a series of tiers (three in this case) of
increasing size. Write a
program to produce such a display having prompted the user for the
number of tiers.
You could try putting a few baubles on the tree (using o or O).
************************************/

#include<iostream>
using namespace std;

const char star = '*';
const char space = ' ';

void main(){

this should be int instead of void
int noStar(1);
/* int numTiers(0);

cout<<"Enter the number of tiers for the tree: ";
cin>> numTiers;
*/

for(int i(1); i <= 20; i++){
for (int j(1); j = (21 - i); j++)

this for loop is an endless loop
cout<<space;
for (int c(1); c = (2*i - 1); c++)

this one as well
 
C

curiousEngine

the second step about the tier is an option.

but I don't know how to get the tree drawn using a for loop!
 
C

Chris ( Val )

The problem is a tree with stars!!!

Please don't go around spewing out exclamation
marks, because it makes you come across as very
demanding. People give their time here for free
and owe you nothing.

You were pointed to some help that should have
helped you grasp the concept of iteration.

Can you write a simple for loop that prints one
horizontal line of stars at your desired length?

Start small and work from there. Get that right
first and then create an outer loop, etc...
 
C

curiousEngine

Ok. As a newbie to C++ and without major help from tutor, creating the
star tree is a bit difficult; but i'll try my best.

lol for the gif>>
 
C

curiousEngine

Ok. As a newbie to C++ and without major help from tutor, creating the
star tree is a bit difficult; but i'll try my best.

lol for the gif>>

Sorry, I'll be more careful next time given:
Include the smallest, complete and compilable program that exhibits
your
problem. As a rule, posters in comp.lang.c++ will not do homework,
but
will give helpful hints if you have shown some willingness to try a
solution.

Thnx Chris (Val)
 
A

anon

curiousEngine said:
Sorry, I'll be more careful next time given:
Include the smallest, complete and compilable program that exhibits
your
problem. As a rule, posters in comp.lang.c++ will not do homework,
but
will give helpful hints if you have shown some willingness to try a
solution.

Thnx Chris (Val)

I have pointed 3 lines in your program, which are wrong. Why don't you
start by correcting them?
 
J

Jerry Coffin


Not so -- that has only one star, where the original spec clearly calls
for "stars". It's hard to guess what the triple exclamation means, but I
think this is closer:

http://photo.net/photodb/photo.tcl?photo_id=1240855

The question here, of course, is whether "a tree" actually means "one
and only one tree" or "at least one tree". I believe the specification
could be a bit more specific on this point, but if only one tree is
allowed, cropping is easy...
 
J

Jerry Coffin

Hello. I've been able to write a tree in pascal with 21 stars. But in C
++, given the new pre-requisite to adjust for tiers, i think i need
help!

I'd do this in two steps. First take your working Pascal code and get it
working (with no new logic) in C++. Then work on adding the new logic.

Assuming your course has covered writing functions yet (which,
unfortunately, it may not have) I'd consider starting by writing a
function to right-justify a string in a given total width. cout is an
ostream object; you might want to start by looking at what an ostream
already provides for field widths.
 
C

curiousEngine

Ok. as a newbie, here is the code that I wrote BY MYSELF. any
suggestions to improve it plzzzzzz. thnx in advance.

/************************************
REVISION LOG ENTRY
Revised on 03/Oct/07 10:12:25 PM
Question: CHRISTMAS TREE
The tree consists of a series of tiers (three in this case) of
increasing size. Write a
program to produce such a display having prompted the user for the
number of tiers.
You could try putting a few baubles on the tree (using o or O).
************************************/

#include<iostream>
using namespace std;

const char star = '*';
const char space = ' ';
const char bauble = 'O';

int main(){
int starLimit, j(0),k(0),count(0), spaceLimit;
int numTiers(0), treeLines(0);

cout<<"Enter the number of tiers for the tree: ";
cin>> numTiers;
cout<<endl;

for(int t(1); t<=numTiers; t++){
treeLines = t+1;

for(int i(1); i <= treeLines; i++){
starLimit = (2 * i) -1;
j = 0;
k = 0;

spaceLimit = (40 - starLimit)/2;

do{
cout<<space;
k++;

}while(k != spaceLimit);

do{
j++;
count++;
if (j % 2 == 0)
cout<<bauble;
else
cout<<star;
}while(j != starLimit);

cout<<endl;
}
}
cout<<"\t \t | \n";
cout<<"\t \t | \n";
cout<<"\t -------------- \n";
cout<<endl;

return 0;//indicates successful completion of program
}
 
P

Phlip

curiousEngine said:
Ok. as a newbie, here is the code that I wrote BY MYSELF. any
suggestions to improve it plzzzzzz. thnx in advance.

Okay. Where's a _small_ but _important_ stretch of code with duplication in
it? The best way to improve a design is seek and remove duplication,
starting with the simple stuff. This...
if (j % 2 == 0)
cout<<bauble;
else
cout<<star;

Could be this...

 cout << char(j % 2 == 0? bauble: star);

That fixes one element of duplication - the 'cout <<'.

(And esthetically, you have too many baubles; you could randomize them with
rand().)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top