Thread: Creating a half pyramid

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Creating a half pyramid

    Hello all!

    I have a problem I need help with. I am tasked with creating a half pyramid like below. First I prompt user to enter an integer less than 23 to dictate how many asterisks in height the pyramid will be. So let's say they entered 8. The half pyramid would need to look like this (ignore the dashes; I had to use them instead of spaces to format here)

    ---------**
    --------***
    ------****
    -----*****
    ----******
    ---*******
    --********
    -*********

    Here is the code I have so far.

    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    int main(int argc, char *argv[])
    {
    int maxSize;
    int xAxisCounter;
    int yAxisCounter;
    
    do
    {
    printf("Enter an integer less than 23: ");
    maxSize=GetInt();
    }
    while(maxSize > 23);
    
    for (yAxisCounter = 0; yAxisCounter < maxSize; yAxisCounter++)
    {
           for (xAxisCounter = 0; xAxisCounter < maxSize; xAxisCounter++)
       {
            printf(" %i ",xAxisCounter);
       }
    
    printf("\n");
    }
             system("PAUSE");
             return 0;
    }
    This code will draw a square like this if the user entered 8 (but with numbers because I have code set to %i right now)

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

    On top of all this, the whole pyramid has to be on the right side of the console.. right justified.

    I am losing hope I can be a programmer because I just don't get it. I have been at this four days and tried many things and nothing works
    Last edited by rightbrainer; 04-20-2009 at 05:00 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    prompt for size of half-pyramid base:
    for each line
        spaces = base_size - 1 - current line
        stars = 1 + current line
        output spaces
        output stars
        output newline
    Something like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think of it as you have a total width to cover, either with spaces (which will always come first), or stars.

    With each line you print, the number of stars to print, increases. Total width - number of stars == number of spaces to print.

    Do that calculation before each line, you can use a for or a while loop, and just stop when the number of stars equals the number you need.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Sorry I don't follow either reply.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well then programming isn't the thing for you. The only way to make that clearer is to write it for you, which I practically did.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    "Well then programming isn't the thing for you.."

    Ironic; I'd say given your lack of patience, TEACHING isn't for YOU.

    In any case, I got it...

    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    int 
    main(int argc, char *argv[])
    {
       int maxSize;
       int xAxisCounter;
       int yAxisCounter;
       
       do
       {
       printf("Enter an integer less than 23: ");
       maxSize=GetInt();
    }
    while(maxSize >23);
    
       for (yAxisCounter = 2; yAxisCounter < maxSize; yAxisCounter++)
       {
           for (xAxisCounter = 0; xAxisCounter < 80; xAxisCounter++)
           {
                if(xAxisCounter<80-yAxisCounter)
                   printf(" "); 
                else
                   printf("O");
           }
       }
    
       printf("\n\n");
    
    
       system("PAUSE");
       return 0;
    }
    keywords: harvard university cs50, half pyramid, malan, mario
    Last edited by rightbrainer; 04-20-2009 at 11:54 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't tell us you were attending Harvard!

    There's the problem. You're an over-educated idiot.

    We would have lowered our expectations, if only we'd known.

    Since we're a little over-geeked, and our posters tend to be a little over-geeked, as well, it's a great fit. We're the same kind of idiots - very important for good communication.

    We have much less useless baggage and a bit more common sense, than what you must be carrying around.

    You have our *deepest* sympathies.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, we can stop the personal attacks now. *thread closed*

    rightbrainer, next time state what exactly were you unable to understand. Just saying "I don't follow either reply" makes it seem like you made no attempt to understand and merely wanted the answer to be handed to you on a silver platter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Number pyramid
    By Tehy in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 09:01 AM
  3. Half Humanoid, half Chimpanzoid
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-19-2004, 04:23 PM
  4. Half life Problem which I am right and the teacher is wrong
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-06-2002, 04:28 PM
  5. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 03:08 PM