Thread: small code issue - please tell me what I'm doing wrong.

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    11

    small code issue - please tell me what I'm doing wrong.

    Hi everyone,

    I'm taking a course online, one of my projects is to build a half pyramid to user specified dimensions.

    I've only been using C 4 days, watched all the teaching vids on the course, read an ebook and watched some youtube tutorials, so please be gentle!.

    my code:
    removed by user

    }[/CODE]

    my problem is, my pyramid is always one layer short, if I ask for a 5 layer pyramid it gives me 4?

    the pyramid has to start with a double block on the top i.e

    ##
    ###
    ####
    #####

    and be aligned to the right, which i've managed and got working after some fiddling and brain re-wiring.

    can anyone see why i'm a layer short please?
    Last edited by MonkeyTennis; 08-14-2015 at 12:32 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum.

    You might want to edit out your personal information at the top of your code.

    The number of rows printed depends on the user input and the length. Length starts at two, and you're printing one less row than you expect. Perhaps this indicates you should increase the size of the user input by a certain amount for purposes of your calculations.

    Additionally, you should use more meaningful variable names. You should strive to make your code self-documenting. For instance:

    Code:
    int l =1, w = 1; //l = length of pyramid, w = width of pyramid
    Instead of "l" and "w", why not just name them "length" and "width"? Then you don't need the comment to know what their purpose is, and the code is easier to read.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Welcome to the forum.

    You might want to edit out your personal information at the top of your code.

    The number of rows printed depends on the user input and the length. Length starts at two, and you're printing one less row than you expect. Perhaps this indicates you should increase the size of the user input by a certain amount for purposes of your calculations.

    Additionally, you should use more meaningful variable names. You should strive to make your code self-documenting. For instance:

    Code:
    int l =1, w = 1; //l = length of pyramid, w = width of pyramid
    Instead of "l" and "w", why not just name them "length" and "width"? Then you don't need the comment to know what their purpose is, and the code is easier to read.


    ahh great thanks, my brain needed that jolt, figured it out now and it's working. Also thanks for the styling tips, you are correct - I'm creating extra work for no reason.
    thanks again

    - removing code for the Academic Honesty of others

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome for the help, but please don't edit out your original post. We help publicly so that anyone can benefit from the knowledge shared. By removing your original post, you invalidate the contribution of those who volunteered their time to help, since nobody else can benefit from their input. Additionally, it ruins the continuity of the thread since any follow-up posts lose context.

    Here is a quote of the original post, with the personal information removed:

    Quote Originally Posted by MonkeyTennis View Post
    Hi everyone,

    I'm taking a course online, one of my projects is to build a half pyramid to user specified dimensions.

    I've only been using C 4 days, watched all the teaching vids on the course, read an ebook and watched some youtube tutorials, so please be gentle!.

    my code:
    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <math.h>
    
    int main(void)
    
    {
    
        int h = 0; //height integer preset
    
        do
        {
            printf("Please enter a height between 1 and 23: ");
            h = GetInt();
        }
        
        while (h <0 || h >23);  /**
                                checks if value given is between the desired integers, 
                                if not user will be prompted for input again
                                */
              if (h == 0 )               
              {                 
              printf("");
              }
              
              else
              
              printf("\n");
              
              
        int l =1, w = 1; //l = length of pyramid, w = width of pyramid
        
        /** 
        The following code nest runs though checks and prints the correct
        number of spaces and "#", also placing the next line command.
        The loop will also make sure a block is deleted on each line to
        allow the pyramid to be the correct dimensions
        */
                       
        for (l = 2; l <= h; l++)  // provides the code for the length
        {
        
            for (w = 1; w <= h; w++) //provides the code for the length
                
            {
                if (w >= h +1 -l) //handles the removal of the troublesome block
                
                {
                printf ("#");
                }
                
                else
                
                {
                printf (" ");
                }
                
            }
                   
        printf("\n");
        }
               
    }
    my problem is, my pyramid is always one layer short, if I ask for a 5 layer pyramid it gives me 4?

    the pyramid has to start with a double block on the top i.e

    ##
    ###
    ####
    #####

    and be aligned to the right, which i've managed and got working after some fiddling and brain re-wiring.

    can anyone see why i'm a layer short please?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lottery game Issue in Code...whats wrong?
    By thebridge in forum C Programming
    Replies: 6
    Last Post: 10-07-2010, 08:16 PM
  2. Need help with a small issue...
    By yoyo2004 in forum C Programming
    Replies: 5
    Last Post: 09-01-2007, 03:56 AM
  3. Small Logic Issue
    By Flakster in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2006, 02:40 PM
  4. A small issue.
    By Overwhelm in forum C++ Programming
    Replies: 35
    Last Post: 01-18-2005, 05:17 PM
  5. Small issue with system()
    By Metal Man in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2003, 01:33 PM