Thread: Need help with C homework problem

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I only see 7 rows high with the height of 8.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Adak is it ok if i use your code and get it checked?

  3. #18
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    38
    Quote Originally Posted by Adak View Post
    I only see 7 rows high with the height of 8.
    Yes, thanks for the remark, here is the corrected version. Now that I look to the picture he provided, as I understand, only height = 0 is ignored but not height = 1. Also for each height H and line number N, there is H - N spaces before the current line.

    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 = 1; 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

    Code:
    $ gcc -Wall testscript.c -o testscript
    $ ./testscript 
    Enter the height: 0
    
    $ ./testscript 
    Enter the height: 1
    
    ##
    $ ./testscript 
    Enter the height: 2
    
     ##
    ###
    $ ./testscript 
    Enter the height: 3
    
      ##
     ###
    ####
    $ ./testscript 
    Enter the height: 4
    
       ##
      ###
     ####
    #####
    $ ./testscript 
    Enter the height: 8
    
           ##
          ###
         ####
        #####
       ######
      #######
     ########
    #########
    $

    Regards,
    Dariyoosh
    Last edited by dariyoosh; 11-24-2012 at 04:08 PM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by asexynerd View Post
    Adak is it ok if i use your code and get it checked?
    It's OK, but be warned - I put it together in a hurry, and meant it only as "one example", and beyond running it with height = 8, I haven't tested it at all.

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