[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create a christmas tree

Posted on 2005-04-08
14
Medium Priority
?
441 Views
Last Modified: 2010-04-01
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

 

       *
     ***
   *****
 *******
*********
0
Comment
Question by:cuong5985
  • 6
  • 5
  • 3
14 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 13739110
     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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13741001
>>>> #include <iostream.h>

use

#include <iostream>
using namespace std;

instead. iostream.h is old header and not C++ Standard

>>>> #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?

Ok! I'll help you.

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 Comment

by:cuong5985
ID: 13745500
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13746708
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 13748142
>>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 Comment

by:cuong5985
ID: 13755192
How about this??


      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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13759545
>>>>      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 Comment

by:cuong5985
ID: 13768507
But still it doesnt show the christmas tree as well even i changed the code on the ways that u told me???
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13769779
Could you post your full code til now?
0
 

Author Comment

by:cuong5985
ID: 13769797
/*******************************************************************
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13769910
Replace

#include <iostream.h>

by

#include <iostream>
using namespace std;

That is C++ standard header.

Add

     cout << endl;

above second for loop (j-loop).

Regards, Alex


0
 
LVL 9

Expert Comment

by:jhshukla
ID: 13771648
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 13772825
>>>> "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
 

Author Comment

by:cuong5985
ID: 13779174
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question