Print character x times based on user's input

This is a discussion on Print character x times based on user's input within the C Programming forums, part of the General Programming Boards category; Hi! So first of all, please keep in mind that I have just began with C, I took CS50x from ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    9

    Print character x times based on user's input

    Hi!

    So first of all, please keep in mind that I have just began with C, I took CS50x from Harvard and so far I have a very limited knowledge, so my question may seem very simple.

    What I have to do is to create a "pyramid" which has to look something like this:

    Code:
       #
      ##
     ###
    ####
    The user has to firstly input an integer to declare how many rows the pyramid is going to have.

    What I have so far

    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <math.h>
    
    
    int main(void)
    {
        //create an infinite loop that will check the value 
        while (1)
        {
                //get an integer from the user which will define the height of the pyramid
                printf("Enter the height of the pyramid! The value has to be in range 3 to 24.\n");
                int h = GetInt();
            
                //if a value is in the range, then proceed to create a pyramid
                if (h<=24 && h >= 3)
                {
                    for(int i = h; i > 0; i--)
                    {
                         printf("row\n");               
                    }
                    
                    
                    break;   
                }
                
                //otherwise continue loop and ask for an another value
                else
                {
                    printf("You have entered the invalid value, please try again.\n");
                }
        }
        return 0;
    
    
    }
    What I am struggling with currently is to figure out how to print out space (h - 1) times

    I would really appreciate any help, but rather then just giving me a finished code I would appreciate a "guidance". Thank you

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    You can't write a loop that repeats something h-1 times and, within the body of that loop, prints a single space?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Well I guess I wrote it wrongly. What I am struggling with is to create a loop that will print out space h-1, h-2, h-3 .... h - h times

  4. #4
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    Quote Originally Posted by Kecupochren View Post
    I would really appreciate any help, but rather then just giving me a finished code I would appreciate a "guidance". Thank you

    Hello there,

    What I understand from your problem (well, actually you may need to modify a bit my solution for the value range part inside a loop) is that:

    Assuming that numberOfRows is the number of rows:

    - For each row N you have to insert numberOfRows - N spaces before start printing sharps

    - For each row N you have to print N times the sharp character

    - And finally insert a new line character


    You can try the following:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        int numberOfRows = 0;
        int rowNumber = 0;
        int counter = 0;
        
        // start by asking the user the number of rows
        printf("Enter the number of rows: ");
        scanf("%d",&numberOfRows);
        
        for (rowNumber = 0 ; rowNumber <= numberOfRows ; rowNumber++)
        {
            // For each row N you have to insert numberOfRows - N spaces
            // before start printing sharps
            for (counter = 0 ; counter < numberOfRows - rowNumber ; counter++)
                printf(" ");
                
            // For each row N you have to print N times the sharp character
            for (counter = 0 ; counter < rowNumber ; counter++)
                printf("#");
            
            // And finally a new line character for the next loop iteration
            printf("\n");
        }
        
        
        return 0;
    }
    Which gives the following result (I tested on Linux)

    Code:
    $ gcc -Wall testscript.c -o testscript
    $ ./testscript
    Enter the number of rows: 0
    
    $ ./testscript
    Enter the number of rows: 1
     
    #
    $ ./testscript
    Enter the number of rows: 2
      
     #
    ##
    $ ./testscript
    Enter the number of rows: 3
       
      #
     ##
    ###
    $ ./testscript
    Enter the number of rows: 4
        
       #
      ##
     ###
    ####
    $ ./testscript
    Enter the number of rows: 5
         
        #
       ##
      ###
     ####
    #####
    $

    Regards,
    Dariyoosh

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    You can't write a nested loop?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Can be done in one loop using variable field width formatting for both the spaces and the number of '#' shown from a predefined array containing 24 '#' (specified as maximum needed) characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a Solid Square based on user input
    By ThatBoy1124 in forum C Programming
    Replies: 3
    Last Post: 03-01-2011, 10:07 PM
  2. User input how many times to run average
    By kgatihi in forum C++ Programming
    Replies: 2
    Last Post: 10-23-2009, 03:05 PM
  3. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 01:21 AM
  4. creating a struct based on user input
    By rodrigorules in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 07:16 PM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 06:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21