CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2014
    Posts
    7

    A custom pyramid-drawing program

    Write your question here.
    Okay, so I am trying to figure out how to create a program that will draw a triangle using *'s with a base the has a user-inputted number of *'s like so:

    *
    ***
    *****
    It needs to take a user inputted number and draw a pyramid like the above pyramid with the number of *'s in the base matching the user inputted number (i.e., user enters 10, so the triangle has 10 *'s in the base). I figured it would be best to first create a loop to draw out the correct number of *'s before trying to create another loop to draw out the correct number of spaces, to properly align the *'s into a triangle shape. Any pointers, or help, or explanation would be so helpful. Thanks.

    int width = 0;
    int height = 0;
    int i = 0;
    int leafWidth = 0;
    int i2 = 1;
    int i3 = 1;
    int i4 = 1;

    cout << "Enter trunk height: " << endl;
    cin >> height;
    cout << "Enter trunk width: " << endl;
    cin >> width;
    cout << "Enter leaves width: " << endl;
    cin >> leafWidth;
    cout << endl;
    i2=1;
    i3=1;
    i4=1;
    while (i2<=leafWidth){
    while (i3 <=i4){
    cout << "*";
    i3=i3+1;
    i4=i4+2;
    }
    cout << endl;

    i2 = i2+2;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: A custom pyramid-drawing program

    When posting code please format your code first and use code tags to make the code readable. Go Advanced, select the code and click '#'

    To draw the required pyramid as you state in your post #1, you require the user to enter the number of '*'s in the base line. So why are you asking the user to input the trunk height, trunk width and leaves width?

    To draw the pyramid, what is the relationship between the row number and the number of stars in that row? What is the relationship between the row number and the number of spaces to display before the '*' to format properly the pyramid?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: A custom pyramid-drawing program

    Like kaud said, you need to devise the mathematical formulas that calculate the number of stars and blanks for any given row number.

    When you have done that, it will be easy to deploy a few loops to construct the actual program.

    I believe that any more additional help at this time would be very detrimental to your learning, because this exercise is very basic, only a tad harder than writing "hello world" or similar.

    Good luck!
    Nobody cares how it works as long as it works

  4. #4
    Join Date
    Jul 2014
    Posts
    7

    Re: A custom pyramid-drawing program

    Okay, so I now can create a nice looking triangle for odd numbered user inputs, but how would I format it so that it doesn't round down to an odd number if the user inputs an even number? It seems that any mathematical formulas I can think of to go from one star in the peak to two stars in the second row, to four stars in the third row just throw all of my other code into chaos, even with an if/else statement to depending on if the user input is even or odd. BTW the other stuff is because the final product is a whole tree. Here is what I have so far:

    Code:
    #include<iostream>
    using namespace std;
    int main(){
    	int trunk=0;
    	int trunkWidth=0;
    	int leafWidth=0;
    	int width2=0;
    	int h,i,j,k=0;
    
    	cout << "Enter trunk height: ";
    	cin >> trunk;
    	cout << "Enter trunk width: ";
    	cin >> trunkWidth;
    	cout << "Enter leaves width: ";
    	cin >> leafWidth;
    
    	j=1;
    	width2=leafWidth/2;
    		for(h=1;h<=leafWidth;h=h+2){
    			for (i=width2;i>=0;i=i-1){
    				cout << " ";
    			}
    		 for (j=1;j<=h; j=j+1){
    				cout << "*";
    			}}
    			width2=width2-1;
    			cout << endl;
    		}
    
    	return 0;
    }
    I was cruising along just fine until this project smacked me upside the head. Feeling pretty discouraged at my inability to problem solve this.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: A custom pyramid-drawing program

    To display a symmetrical tree with 1 star at the top the way you are drawing it requires an odd number in the last line. Often the user is prompted for the height of the tree rather than the number in the base. If the input needs to be the number in the base then either error check the input to be an odd number or if even entered add 1 to make it odd and display a message.

    Your code can be simplified somewhat
    Code:
    #include<iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    size_t leafWidth = 0;
    
    	cout << "Enter leaves width: ";
    	cin >> leafWidth;
    
    	for (size_t h = 1, width2 = leafWidth / 2; h <= leafWidth; h += 2, --width2)
    		cout << string (width2, ' ') << string(h, '*') << endl;
    
    	return 0;
    }
    See http://www.cplusplus.com/reference/s...string/string/ for info about string.
    Last edited by 2kaud; July 8th, 2014 at 02:13 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jul 2014
    Posts
    7

    Re: A custom pyramid-drawing program

    Unfortunately, it needs to be able to print an even numbed base, too, even if it looks like this:


    *
    **
    ****
    ******
    ********

    I am stumped.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A custom pyramid-drawing program

    Quote Originally Posted by anundal View Post
    Unfortunately, it needs to be able to print an even numbed base, too, even if it looks like this:


    *
    **
    ****
    ******
    ********

    I am stumped.
    About what? I'm guessing you're stumped because you're thinking about code, not the algorithm. Figure out in English, without even thinking about code, what exactly the logic is for drawing the pyramid the way you want. You can't write code if you don't understand the problem and the solution first.

  8. #8
    Join Date
    Jul 2014
    Posts
    7

    Re: A custom pyramid-drawing program

    Good advice. I got it to work, but the process of getting there was not pretty. Thanks for the help. Hopefully next time I can be more systematic and less trial and error. Programming is sure making me realize how un-analytical my thought process tends to be.Thanks again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured