Link to home
Start Free TrialLog in
Avatar of Ewe kean tan
Ewe kean tan

asked on

draw a Christmas tree by using a nested loop?

i need to draw a Christmas tree by using a nested loop . Anyone can help me .Thank in advanced
Avatar of mankowitz
mankowitz
Flag of United States of America image

Do you need to draw a tree? What is the source data you will be using?
Avatar of Ewe kean tan
Ewe kean tan

ASKER

i  using visual studio 2012 n quincy 2005 ...quincy 2005 is old version C++ coding....
btw i need to submit my assignment by next weeks ......
must using nested loops ...
christmas tress ...
Are you trying to graphically draw christmas trees? I'm not sure why nested loops are needed, but I suppose you could have one loop that draws branches and another loop that draws trees. I'm afraid you may need to give us more information.
question need to use function ...i am new to this C+++ ...sorry ..hope u guy will help me :)
thank you  <3
i would suggest to create an array in your program which represents the number of asterisks for each line:

int cmtreedef [] = { 1, 3, 5, 7, 2, ...... };  // define the whole tree from top to bottom
int nlines = (int)(sizeof(cmtreedef)/sizeof(cmtreedef[0]));

Open in new window


with that you can start a loop counting n from 0 to nlines-1.

in the loop
     - get the number of asterisks from cmtreedef[n]
     - calculate the offset in characters to the first asterisk
     - output 'offset' space characters (for example by creating a std::string with 'offset' spaces and output the string.)
    - output the asterisks for that line (you also could create a string  or use a nested loop).

Sara
The OP said that he needed nested loops. Consider the following:

#include <iostream>
#define STAR_WIDTH 7
#define TREE_WIDTH 10
#define TRUNK_WIDTH 3
#define TRUNK_HEIGHT 3
#define DISPLAY_WIDTH 25

using namespace std;

void triangle(int base) {
	for (int i = (base%2)||2; i < base; i+=2) {
		for (int j=0; j<(DISPLAY_WIDTH-i)/2; j++) {
			cout << " ";
		}
		for (int j=0; j<i; j++) {
			cout << "*";
		}
		cout << "\n";
	}
}

void rectangle(int height, int width) {
	for (int i=0; i<height; i++) {
		for (int j=0; j<width/2; j++) {
			cout << " ";
		}
		for (int j=0; j<width; j++) {
			cout << "*";
		}
		cout << "\n";
	}
}

int main() {
	triangle(STAR_WIDTH);
	triangle(TREE_WIDTH);
	rectangle(TRUNK_WIDTH, TRUNK_HEIGHT);
	return 0;
}

Open in new window

Arguments in line 37 need to be reversed.
See http://ideone.com/s9qEhl
 The rectangle function needs to have an offset. Here is the output:
            *
           ***
          *****
            *
           ***
          *****
         *******
        *********
 ***
 ***
 ***

Open in new window

for (int i = (base%2)||2; i < base; i+=2)

the for statement is badly readable and wrong (as shown by phoffric).

Sara
>> the for statement is badly readable
Agree!

The rectangle function needs to be fixed up. See if you can do it and post your attempt and result.
i change the rectangle(TRUNK_WIDTH, TRUNK_HEIGHT); to rectangle(TRUNK_HEIGHT, TRUNK_WIDTH); ..but there are still same :(
>> but there are still same
 That's because in the program they both have the same value but the calling sequence shows that they had to be reversed.

 Not sure what's in your uploaded doc file. Please post your code within code blocks. Hit the code button to get the code tags.

 You need to move the trunk of the tree to the right by modifying the rectangle function.
#include <iostream.h>
#define STAR_WIDTH 7
#define TREE_WIDTH 10
#define TRUNK_WIDTH 3
#define TRUNK_HEIGHT 3
#define DISPLAY_WIDTH 25

using namespace std;

void triangle(int base) {
      for (int i = (base%2)||2; i < base; i+=2) {
            for (int j=0; j<(DISPLAY_WIDTH-i)/2; j++) {
                  cout << " ";
            }
            for (int j=0; j<i; j++) {
                  cout << "*";
            }
            cout << "\n";
      }
}

void rectangle(int height, int width) {
      for (int i=0; i<height; i++) {
            for (int j=0; j<width/2; j++) {
                  cout << " ";
            }
            for (int j=0; j<width; j++) {
                  cout << "*";
            }
            cout << "\n";
      }
}

int main() {
      triangle(STAR_WIDTH);
      triangle(TREE_WIDTH);
      rectangle(TRUNK_HEIGHT, TRUNK_WIDTH);
      return 0;
}

OUtput:
            *
           ***
          *****
            *
           ***
          *****
         *******
        *********
 ***
 ***
 ***

Press Enter to return to Quincy...
you may try the following Code:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>

const int MARGIN_WIDTH = 4;

void DrawTree(int nlines, int treedef[])
{
    // get maximum stars
    int * pmax = std::max_element(&treedef[0], &treedef[nlines]);
    // calculate tree width
    int treewidth = *pmax + 2*MARGIN_WIDTH;  // have a margin of 4 left and right of the tree 
    // calculate middle
    int middle    = (treewidth +1)/ 2;
    // output all lines
    for (int n = 0; n < nlines; ++n)
    {
        // get number of stars
        int nstars  = treedef[n];
        // calculate offset
        int nspaces = middle - (nstars+1)/2;
        
        // build space string
        std::string spaces(nspaces, ' ');
        // build stars
        std::string stars (nstars,  '*');
        // write output line
        std::cout << spaces << stars << std::endl;
    }
}

int main()
{
    // define the whole tree from top to bottom
    int cmtreedef [] = { 1, 3, 5, 7, 2, 4, 6, 8, 10, 3, 3, 3, };  
    // calculate number of elements
    int nlines = (int)(sizeof(cmtreedef)/sizeof(cmtreedef[0]));
    // draw the tree
    DrawTree(nlines, cmtreedef);

    // wait for User Input
    std::string strbrk;
    std::getline(std::cin, strbrk);

    return 0;
} 

Open in new window

Sorry, was just freehand coding -- trying to give a sense of how to do it. Next time I'll use a compiler.

That ideone is really cool. See http://ideone.com/tUHV09

produces
Success	time: 0 memory: 3468 signal:0
             *
            ***
           *****
            **
           ****
          ******
         ********
           ***
           ***
           ***

Open in new window



#include <iostream>
#define STAR_WIDTH 7
#define TREE_WIDTH 10
#define TRUNK_WIDTH 3
#define TRUNK_HEIGHT 3
#define DISPLAY_WIDTH 25

using namespace std;

void triangle(int base) {
	int initial_width = base%2;
	if (initial_width == 0) initial_width = 2;
      for (int i = initial_width; i <= base; i+=2) {
            for (int j=0; j<(DISPLAY_WIDTH-i)/2; j++) {
                  cout << " ";
            }
            for (int j=0; j<i; j++) {
                  cout << "*";
            }
            cout << "\n";
      }
}

void rectangle(int height, int width) {
      for (int i=0; i<height; i++) {
            for (int j=0; j<(DISPLAY_WIDTH-width)/2; j++) {
                  cout << " ";
            }
            for (int j=0; j<width; j++) {
                  cout << "*";
            }
            cout << "\n";
      }
}

int main() {
      triangle(STAR_WIDTH);
      triangle(TREE_WIDTH);
      rectangle(TRUNK_WIDTH, TRUNK_HEIGHT);
      return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you guy ...but still have one question remaining ...now i solve the question 1 ...Can anyone know about function ????
Now start with the Question 2
the functions would look like

double Summation(double x, double y, double z);
double Average(double x, double y, double z);
double Maximum(double x, double y, double z);

the implementation is simple.

for the main function you will start an outer loop with 10 iterations (count from 0 while less than 10, step +1),

in the loop you ask the user for x, y, z where you have defined variables of type double.

after that you print a menu (1) Summation (2) Average (3) Maximum and ask the user which function to select (you could add (4) Quit and break the outer loop if the user types 4)). depending on the number the user types you call one of the 3 functions and store the result in another double variable. print the result as described in your requirements.

Sara
sorry sara ...
i cant really understand the programming ..
but i still learning on it ..
Thank you sara ..
function average recall back?
for example

double Average(double x, double y, double z)
{
     return (x+y+z)/3.;
}

Open in new window


does that help? can you program the two other functions?

Sara
If you are satisfied with the posts dealing with your question in the OP, then please close this question now.

 Please ask the second question in a separate question, since it has nothing to do with the question in  the OP, nor is it related to the title of this question - "draw a Christmas tree by using a nested loop?"
http://support.experts-exchange.com/customer/en/portal/articles/608596-how-do-i-accept-multiple-comments-as-my-solution-
This link provides you with instructions on how to close this question. Note that after you see the distributed points, you can change them by hitting the edit button.
ok noted