Thread: Looking for a little help please...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    Looking for a little help please...

    Hello everyone,

    I am currently taking CS50x thru Edx.org and was hoping someone would be kind enough to help explain something to me. Please keep in mind I have had zero experience in programming. My first weeks project was to write a code that would ask the user to input a number between 0 and 23 and then the code would build a half pyramid to the height the user stated starting with to hashes at the top.
    Example: for 8 it would show
    ..............##
    ............###
    ..........####
    ........#####
    ......######
    ....#######
    ..########
    #########
    Except without the .'s. I couldn't get my spaces to stay put.
    I did finally get it to work but getting the hashes to print was totally trial and error. Under //Print hash, I originally though h should equal 2 because I wanted to start with 2 hash marks but instead it was giving me 2 additional lines above my pyramid. So I started playing with that number just to see what would happen and got it to work with h = 0. I was hoping someone would be able to tell me why this is the case. I just need to understand why it works so I can learn from it. I hope I gave enough info. Thanks in advance.

    ------------------------------------------

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    
    int main(void)
    {
        // Declare variable for pyramid height
        int height;
        
        //Prompt user to enter pyramid height and make sure it is within the range
        do
        {
            printf("Please enter a number between 0 and 23: ");
            height = GetInt();
        }
        while (height < 0 || height > 23);
        
        //Print pyramid
        for (int i = 1; i <= height; i++)
        {
        
            //Print space
            for (int s = height - i; s > 0; s--)
            {
                printf(" ");
            }
            
            //Print hash
            for (int h = 0; h <= i; h++)
            {
                printf("#");
            }
            
            //Print new line
            printf("\n");
            
        }
            
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Within your outer "Print pyramid" loop, you are printing the data for one line at a time (and ending with a "\n"). So your "Print hash" loop has to print all the #'s that are going to appear on one line. Now, if you want (say) two hashes on the first line, you have to make sure the inner loop will run twice during the first iteration of the outer loop. The outer loop's control variable i starts at 1 and goes up. So if the inner loop starts from 0 and goes <= i, this will be in the range [0,1] or 0-1 for the first line, meaning you'll print two hashes.

    Basically, it's worth thinking about the loops inductively. Figure out what happens in the first iteration, then make sure the loop variables are going in the right direction (getting larger or smaller) and assume induction will take care of the other loop iterations. Then all you have to do is make sure the loop executes the right number of times; perhaps think through the last loop iteration to make sure it seems correct. Think in terms of hypotheticals: suppose i is 1, then what happens in the inner loops? And so on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    2
    Thanks for the reply dwks. I appreciate the input. I hope this all becomes clearer to me soon. This is only my first week so I cannot get discouraged.

Popular pages Recent additions subscribe to a feed