Need help with C homework problem

This is a discussion on Need help with C homework problem within the C Programming forums, part of the General Programming Boards category; Hello, My name is ali and I am currently enrolled in edx.org program for computer science. On my problem set ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    Need help with C homework problem

    Hello,


    My name is ali and I am currently enrolled in edx.org program for computer science. On my problem set I was assign to create the pillars in mario:


    jharvard@appliance (~/Dropbox/pset1): ./mario
    Height: 8
    ##
    ###
    ####
    #####
    ######
    #######
    ########
    #########

    ****The pillars are aligned to the left and not right but for some reason it won't let me post it that way.****


    I have to first prompt the user for the half-pyramid's height, a non-negative integer no greater than 23. (The height of the half-pyramid pictured above happens to be 8.) If the user fails to provide a non-negative integer no greater than 23, you should re-prompt for the same again. Then, generate (with the help of printf and one or more loops) the desired half-pyramid. Also I have take care to align the bottom-left corner of your half-pyramid with the left-hand edge of your terminal window.


    So far this is what I got from my solution. But the problem is They don't align properly and I don't understand how to use the \n in a loop to make that happen:


    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int h, s, i;
            
            do
            {
                printf("Height: ");
                scanf("%d", &h);
            }
            while(h <= 0 || h > 23);
            
            for(s = 0; s < h - 1; s++)
            {
                printf(" ");
            }    
            for(i = 0;i < h ; i++)
            {   
                printf("#");
            }    
            
            
            return 0;
        
    }

    I can only use "do" "while" and "for" to solve this problem. I would really appreciate it if someone could guide me here
    Last edited by Salem; 11-23-2012 at 10:52 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > int h, s, i;
    Consider naming your variables things like
    int numSpaces, numHashes, lineNumber;

    Then your code might look like this.
    Code:
    for ( lineNumber = 1 ; lineNumber <= 10 ; lineNumber++ ) {
        numSpaces = ?
        numHashes = ?
    }
    Once you've got the calculation for spaces and hashes sorted out, then you can do a simple loop to output the correct number of each symbol on a line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    I am a very beginner, I tried your problem and got the result by using function :
    Code:
    #include <stdio.h>
    void print (int);
    int main(void)
    {
        int h, i;
             
            do
            {
                printf("Height: ");
                scanf("%d", &h);
            }
            while(h <= 0 || h > 23);
            for (i=h-1;i>=0;i--)
            {
                print (h-i);
                printf("\n");
            }
            return 0;
    }
    void print (int h)
    {
        int i=0;
        do
        {
            printf("#");
            i++;
         }   
         while (i<=h-1);
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, asexynerd!

    It's very intuitive to solve these row & column type of pattern problems, with two nested for loops.
    Code:
    for(each row) {
       for(each column) {
           if() conditions with basic arithmetic, (addition and subtraction) using 
           row, column, and height, variables, determine what should be printed 
           in any column.
       }
    }

  5. #5
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    As Adak said, two nested loop does the job.

    The following works for me

    Code:
    #include <stdio.h>
    
    int main()
    {
        int height = 0;
        int line = 0;
        int counter = 0;
        
        while (height <= 0 || height > 23)
        {
            printf("Enter the height: ");
            scanf("%d", &height);
        }
        printf("\n");
        
        for(line = 1; line <= height; line++)
        {
            for(counter = 1; counter <= line + 1; counter++)
                printf("#");
            
                printf("\n");
        }
        
        
        return 0;
    }
    And here is the result

    Code:
    $ gcc -Wall testscript.c -o testscript
    $ ./testscript 
    Enter the height: 100
    Enter the height: 99
    Enter the height: -54
    Enter the height: 10
    
    ##
    ###
    ####
    #####
    ######
    #######
    ########
    #########
    ##########
    ###########
    $
    Regards,
    Dariyoosh
    Last edited by dariyoosh; 11-24-2012 at 03:37 AM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Salem, Adak, justine and dariyoosh thank you all for your replies. Dorivoosh I used your code as the frame but I still having some difficulties getting the right result. I need to have 7 space and start with 2 hashes. So if the user enters 0 or 1 then no pillars show but when they press 2 you will see the 2 pillars. After I have 9 stacks I don't want to print anymore and the last bottom pillar has to be with no spaces just 9 hashes. If the user enters "height: 10":

    Height: 2
    *******## (* represent space)

    if they enter 3 they get:
    [CODE]Height: 3
    *******##
    ******###

    I have got the the pillars to move to the right just like above but I am unable to stack them. How would I us the \n in a loop to get the pillars to stack. Also if a if the user enter a char how to i tell them to "retry: ".

    My code so far:

    Code:
    #include <stdio.h>
    int main()
    {
        int height = 0;
        int space = 0;
        int hash = 0;
          
        while (height <= 0 || height > 23)
        {
            printf("Enter the height: ");
            scanf("%d", &height);
        } 
    
    
        for(space = 8; space >= height; space--)
            printf(" ");
             {
                for(hash = 0; hash <= space; hash++)
                    printf("#");
                    
                                   
             }
        return 0;
    This is what happens so far:

    Code:
    Enter the height: 1
             #jharvard@appliance (~/Dropbox/pset1): ./test2 
    Enter the height: 2
            ##jharvard@appliance (~/Dropbox/pset1): ./test2
    Enter the height: 3    
           ###jharvard@appliance (~/Dropbox/pset1): ./test2
    Enter the height: 4
          ####jharvard@appliance (~/Dropbox/pset1): ./test2 
    Enter the height: 5
         #####jharvard@appliance (~/Dropbox/pset1): ./test2
    Enter the height: 6
        ######jharvard@appliance (~/Dropbox/pset1): ./test2
    Enter the height: 7
       #######jharvard@appliance (~/Dropbox/pset1): ./test2
    Enter the height: 8
      ########jharvard@appliance (~/Dropbox/pset1): ./test2
    Enter the height: 9
     #########jharvard@appliance (~/Dropbox/pset1):
    Last edited by asexynerd; 11-24-2012 at 01:26 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The pillars stack themselves. Just count the columns and the row, and get your arithmetic going.
    Code:
    spaces = widthOfRow - rowNum
    if(row<2)     //correction for Mario's pillars
       spaces+=rowNum;
    Although simple arithmetic will solve most of the problems, some fiddling and trial and error may be needed for some rows or columns. Just let the rows and column for loops, go around and around, and do as little logic as you can to make it right.

    Normal thing is to overthink it, and try to put all the rows logic, into the column's for loop, as if the loop was only going to loop a few times, instead of several times.

    Don't you print a newline at the end of each row? You should.
    Last edited by Adak; 11-24-2012 at 01:20 PM.

  8. #8
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    Quote Originally Posted by asexynerd View Post
    After I have 8 stacks I don't want to print anymore if the user enters "height: 9"
    This is in contradiction with the OP, because first you said:

    Quote Originally Posted by asexynerd View Post
    ... I have to first prompt the user for the half-pyramid's height, a non-negative integer no greater than 23 ...
    So finally the max is 8 or 23 ?

    Also according to your code
    Code:
    while(h <= 0 || h > 23);
    One reading this, understands that the user can enter 0 or 1 as a legal value for the height, but now you say for 0 and 1 nothing is printed.

    The problem is not clear!

    Regards,
    Dariyoosh

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Can the height be zero? How can the height be zero? >.<

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Adak can you give me a example of what you are saying because I am completely lost.

  11. #11
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    Quote Originally Posted by Adak View Post
    Can the height be zero? How can the height be zero? >.<
    I don't know why don't you ask him ??

    Regards,
    Dariyoosh

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Quote Originally Posted by dariyoosh View Post
    This is in contradiction with the OP, because first you said:


    So finally the max is 8 or 23 ?

    Also according to your code
    Code:
    while(h <= 0 || h > 23);
    One reading this, understands that the user can enter 0 or 1 as a legal value for the height, but now you say for 0 and 1 nothing is printed.

    The problem is not clear!

    Regards,
    Dariyoosh
    dairyoosh i am sorry that what my mistake on the first post. The pillar has to be in this order:

    Height = 8
      First row: 2 hashes, 7 spaces
      Second row: 3 #, 6 spaces
      Third row: 4 #, 5 spaces

    my intructions were from the problem set were:

    Write, in a file called mario.c in your ~/Dropbox/pset1 directory, a program that recreates this half-pyramid using hashes (#) for blocks. However, to make things more interesting, first prompt the user for the half-pyramid's height, a non-negative integer no greater than 23. (The height of the half-pyramid pictured above happens to be 8.) If the user fails to provide a non-negative integer no greater than 23, you should re-prompt for the same again. Then, generate (with the help of printfand one or more loops) the desired half-pyramid. Take care to align the bottom-left corner of your half-pyramid with the left-hand edge of your terminal window, as in the sample output below, wherein boldfaced text represents some user's input.


    Code:
    jharvard@appliance (~/Dropbox/pset1): ./mario
    Height: 8 
    *******## 
    ******### 
    *****#### 
    ****##### 
    ***###### 
    **####### 
    *########
    #########
    ```
    
    Note that the rightmost two columns of blocks must be of the same height. No need to generate the pipe, clouds, numbers, text, or Mario himself. Just the half-pyramid! And be sure that main returns 0.
    By contrast, if the user fails to provide a non-negative integer no greater than 23, your program's output should instead resemble the below, wherein boldfaced text again represents some user's input. (Recall that GetInt will handle some, but not all, re-prompting for you.)

    Code:
    jharvard@appliance (~/Dropbox/pset1): ./mario
    Height: -2
    Height: -1
    Height: foo
    Retry: bar
    Retry: 1
    ##
    
    Last edited by asexynerd; 11-24-2012 at 01:47 PM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by asexynerd View Post
    Adak can you give me a example of what you are saying because I am completely lost.
    OK.

    This is the figure with height = 8:
    Code:
    Height: 8
           ##  row 1 has 2 #'s
          ###  2
         ####  3
        #####  4
       ######  5
      #######  6
     ########  7
    #########  8  has 9 #'s
    Is that the right shape? I'm not that familiar with Mario!

    So every row has 9 char's in it.
    Code:
    #include <stdio.h>
    
    int main(void) {
       int r, c, spaces;
       int row,height;
       int col;
    
       printf("Enter height of the figure [1-23]: ");
       //scanf("%d",&height);
       printf("\n");
       height = 8;
    
       row=height;
       col=row+1;
    
       for(r=0;r<row+1;r++) {
          for(c=0;c<col;c++) {
             if(r>0) {
                spaces = col-(r+1);
                if(c<spaces)
                   printf("%c",' ');
                else
                   printf("%c",'#');
             }
          }
          if(r > 0)
             printf("  row %d\n",r);
       }
    
       printf("\n");
       return 0;
    }
    Last edited by Adak; 11-24-2012 at 02:17 PM.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    This is what I am trying to make:

    Name:  pyramid.png
Views: 1729
Size:  49.9 KB

  15. #15
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    Ok, I think I found it (I hope )

    Code:
    #include <stdio.h>
    
    int main()
    {
        int height = -1;
        int line = 0;
        int counter = 0;
        
        while (height < 0 || height > 23)
        {
            printf("Enter the height: ");
            scanf("%d", &height);
        }
        printf("\n");
        
        for(line = 2; line <= height; line++)
        {
            int spaceNum = height - line;
            int spaceCounter = 1;
            for (spaceCounter = 0; spaceCounter <= spaceNum ; spaceCounter++)
                printf(" ");
            for(counter = 0; counter < line ; counter++)
                printf("#");
            
                printf("\n");
        }
        
        
        return 0;
    }

    Which gives the following result for different heights


    Code:
    $ ./testscript 
    Enter the height: 0
    
    $ ./testscript 
    Enter the height: 1
    
    $ ./testscript 
    Enter the height: 2
    
     ##
    $ ./testscript 
    Enter the height: 3
    
      ##
     ###
    $ ./testscript 
    Enter the height: 8
    
           ##
          ###
         ####
        #####
       ######
      #######
     ########
    $

    Regards,
    Dariyoosh

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework problem!
    By arsenalftw067 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2012, 01:07 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Need help please with a homework problem
    By AxlRose in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2004, 01:25 PM
  4. homework problem
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2002, 07:12 PM
  5. Help Me with this homework problem Please!!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2002, 07:54 PM

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