Thread: Slight difficulty with hourglass shape program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    Slight difficulty with hourglass shape program

    Hello. I've been trying to make an hourglass shape using any character the users inputs. I made the program but I can only make the top half of the hour glass with what i have right now. I just dont understand how to take the same code and make it so that it does the opposite after it gets to the middle of the hour glass. Here's what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {int height, row, stars, spaces;
    char symbol;
    
    printf ("Enter a single character \n");
    scanf ("%c", &symbol);
    
    printf ("Enter an uneven integer between 1-19 \n");
    scanf ("%d", &height);
    
    for (row =height/2; row < height; row++) 
    { 
        for (spaces = 0; spaces < row; spaces ++) 
            printf(" "); 
        for (stars = 1; stars <  2 *  (height-row); stars++) 
            printf("%c", symbol); 
        printf("\n");

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I would do it like this:
    Code:
    itsme@itsme:~/C$ cat hourglass.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void draw_line(int row, int height, char symbol)
    {
      int i;
    
      for(i = 0;i < row;++i)
        putchar(' ');
      for(;i < height-row;++i)
        putchar(symbol);
      putchar('\n');
    }
    
    int main(void)
    {
      int height, row;
      char symbol;
      char buf[100];
    
      do
      {
        printf("Enter a single character: ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        symbol = *buf;
      } while(!isprint(symbol));
    
      do
      {
        printf("Enter an odd integer between 1 and 19: ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        height = atoi(buf);
      } while(height < 1 || height > 19 || !(height%2));
    
      for(row = 0;row < height/2;++row)
        draw_line(row, height, symbol);
    
      for(row = height/2;row >= 0;--row)
        draw_line(row, height, symbol);
    
      return 0;
    }
    itsme@itsme:~/C$ ./hourglass
    Enter a single character: I
    Enter an odd integer between 1 and 19: 19
    IIIIIIIIIIIIIIIIIII
     IIIIIIIIIIIIIIIII
      IIIIIIIIIIIIIII
       IIIIIIIIIIIII
        IIIIIIIIIII
         IIIIIIIII
          IIIIIII
           IIIII
            III
             I
            III
           IIIII
          IIIIIII
         IIIIIIIII
        IIIIIIIIIII
       IIIIIIIIIIIII
      IIIIIIIIIIIIIII
     IIIIIIIIIIIIIIIII
    IIIIIIIIIIIIIIIIIII
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is a variation of the 'diamond' or 'pyramid' homework assignment. Search the forum for either of those and you should come up with a number of posts.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Think I got it:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main ()
    {int x, y, num;
    char symbol;
    
    printf ("Enter a single character \n");
    scanf ("%c", &symbol);
    
    printf ("Enter an uneven integer between 1-19 \n");
    scanf ("%d", &num);
    
    
    
    
    
         for (x= num / 2 ; x >= 1 ; x--)      
           {
               for(y = -num; y <= num - x; y++)
               {
                   printf (" ");
               }
               for(y = 1; y <= 2*x- 1; y++) 
               {
                   printf ("%c", symbol);
               }
               printf ("\n") ;
           }            
        
        
    
    
    
    
    for(x = 2; x < num / 2 + 1; x++)
    {
        for(y = -num; y <= num - x; y++)
        {
            printf (" ");
        }
        for(y = 1; y <= 2*x - 1; y++)
        {
            printf ("%c", symbol);
        }
        
            printf ("\n");
            
        }    
    system("pause");        
            return 0;
           
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Newbie- having trouble with functions
    By bnmwad in forum C Programming
    Replies: 7
    Last Post: 02-22-2005, 04:41 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM