Thread: help on making a box of size n

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    Angry help on making a box of size n

    I have a problem...i have to make a box of size n, where the box would roughly look like this: **** for n = 4
    * *
    * *
    ****

    heres what i have so far: i have a "c" shaped one, i cant get the right vertical side...please help

    //////////////////////////////////////////////////////////////////

    #include <stdio.h>

    int
    main(void)
    {

    int i, j; /* loop control variables */
    printf("type number of rows\n");
    scanf("%d", &j);


    for (i = 0; i < j; i++)
    printf ("*");
    printf("\n");


    for (i = 0; i < j - 2; i++)
    printf("*\n");



    for (i = 0; i < j; i++)
    printf ("*");

    printf("\n");



    return (0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use code tags.
    Code:
    Print N dots for the top line.
    Repeat N-2 times:
        draw 1 dot
        draw N-2 spaces
        draw 1 dot and a newline character
    Print N dots for the bottom line.
    There's your answer. Let's see you implement it.

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

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    yea...i got the logic, but where i got stuck is how do i print out blank characters for n-2?
    i have no idea how to write the code for the middle part of the box...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf(" ");

    Or perhaps...

    puts( " " );

    Or maybe...

    printf("%c", ' ' );

    Or you might want to try...

    putchar( ' ' );

    Or...

    Basicly just output a space in any way you prefer.

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

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    okay, i got the box to close, but what is wrong with my code where i can't loop the middle part n-2 times? i cant seem to find out whats wrong. thank you




    Code:
    
    #include <stdio.h>
     
    int
    main(void)
    {		
    		
            int i, j;   /* loop control variables */
            printf("type number of rows\n");      /* prints column labels */
    		scanf("%d", &j);
    		
    		
    		for (i = 0; i < j; i++)
    			printf ("*");
    		printf("\n");
    		
    		
    			
    			
    		for (i = 0; i < j - 2; i++)
    		{
    			printf("*");
    			for (i = 0; i < j - 2; i++)
    				printf(" ");
    			printf("*\n");
    			}
    		
    		
    		
    		
    		for (i = 0; i < j; i++)
    			printf ("*");
    		
    		printf("\n");
            
    	
                  
            return (0);
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void fun( int f ){
             char u[]="*%%%dc*\n";
             char n[BUFSIZ]={0};
             sprintf(n,u,f-2);
             for(f-=2;f>0;f--)
                      printf(n,' ' );
    }
    Like it says... fun.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. No data showing in combo box
    By PJYelton in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2005, 07:20 PM
  4. char problem
    By eXistenZ in forum Windows Programming
    Replies: 36
    Last Post: 02-21-2005, 06:32 AM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM