hollow square

This is a discussion on hollow square within the C Programming forums, part of the General Programming Boards category; Ok here is my exercise ive wrote code that doesn't work prints a square on the screen but not as ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    hollow square

    Ok here is my exercise ive wrote code that doesn't work prints a square on the screen but not as the patren described below

    Exercise 1: Display a Square Learning Objectives: To verify data entry, use while loops, and use ifstatements.Read This First You should have already read up on ‘while’ loops in your text. You are expected to use this command to display the box. You are alsoexpected to use only a single ‘if-else’ command when displaying the box (this will require a bit of thought). Do NOT use ‘for’ loops.What To Do Write a program (square.c) that reads in the size of the square and prints a hollow square of that size using asterisks and blanks. Your program must make a decision of whether to print a '*' or ' ' at aparticular location. Keep in mind that, using the DOS window, the output can only be displayed from left to right and (when you reachthe end of a line) from top to bottom. Your program should onlyaccept and work only for even side sizes between 2 and 20. Forexample, if your program reads in a size of 5, the following outputwould be displayed:

    *****
    * *
    * *
    * *
    *****

    ok here is my bad code
    Code:
    #include <stdio.h>
    
    /* function main begins program execution */
    int main()
    {
       int row; /* initialize row */
       int column;   /* define column */
       int x;
    
       printf( "Enter a number of row to be printed\n" );
       scanf ( "%d", &x );
    
       row = x;
    
       while ( row >= 1 ) { /* loop until row < 1 */
          column = 1;       /* set column to 1 as interation begins */
    
          while ( column <= row ) {               /* loop  times */
    		 printf("%s", row  ? "*" : "*"); /* output */
             column++;/* increment column */
    	  }
    
    		 
    	  
    	  printf( "\n" ); /* begin new output line */
    	  
    	  x--;
    	  if ( x < 1 ) break;
          
    		  
    		 
       } /* end outer while */
    
       return 0; /* indicate program ended successfully */
    
    } /* end function main */
    any help or point into the right direction would be wounderful I'm about to pull my hair out on this one.
    joe

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    #include <stdio.h>
    
    int main()
    {
       int row;
       int column;
       int x;
    
       printf( "Enter a number of row to be printed\n" );
       scanf ( "%d", &x );
    
       row = x;
       /* draw top */
       while ( row > 0 ) {
            printf("*");
            row--;
       }
       printf("\n");
    
       row = x - 2;
       column = x;
    
       /* draw middle */
       while (row > 0)
       {
            while (column > 0)
            {
                    if ((column == x) || (column == 1))
                            printf("*");
                    else printf(" ");
                    column--;
            }
            printf("\n");
            column = x;
            row--;
       }
    
       row = x;
       /* draw bottom */
       while ( row > 0 ) {
            printf("*");
            row--;
       }
       printf("\n");
    
       return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    thank you so much it works perfect sl4nted

    the only part of your code i do not understand 100% is
    Code:
    while (column > 0)
            {
                    if ((column == x) || (column == 1))
                            printf("*");
                    else printf(" ");
                    column--;
            }
    i would die to know how that works
    thanks again
    joe

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    since your printing a hollow box...each row other than the top and bottom only contains 2 *'s, one at the very start and one at the very end.

    if you look, column = x when the loop starts

    so if (column == x) || (column == 1) will be true for the first iteration through the loop
    and * will print.

    it will never be true again untill column = 1, which is the very last iteration through the loop.

    notice how after the inside while is done column is set back to x, to make sure that each inside row prints the first time throught the loop, and the last time through the loop (left side and right side)

    hopefully that clear enough

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    the way I read your teachers outline, it looks like you should also check to make sure
    the user has entered an integer between 2 and 20. So you will edit though while loops a bit since you only are allowed 1 if statement.

    while ((row > 0) && (row < 21))

    for each one, if I understand the project right that is

    edit:
    actually it looks like it has to be between 2 and 20 and be even
    so make a big while loop holding all the other while loops
    Code:
       scanf
       while ((x >= 2) && (x <= 20) && (x % 2 == 0))
       {
             all the code
             x = 0;
       }
    Last edited by sl4nted; 11-30-2006 at 11:24 PM.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* function main begins program execution */
    int main()
    {
        int row; /* initialize row */
        int column;   /* define column */
        int x;
        
        printf( "Enter a number of row to be printed\n" );
        scanf ( "%d", &x );
        
        row = x;
        
        while ( row >= 1 ) { /* loop until row < 1 */
            column = 1;       /* set column to 1 as interation begins */
            
            while ( column <= row ) {   /* loop  times */
                printf("%s", x==1||x==row||column==1||column==row  ? "*" : " "); /* output */
                column++;/* increment column */
            }
            printf( "\n" ); /* begin new output line */
            
            x--;
            if ( x < 1 ) break;
        } /* end outer while */
        
        system("pause");
        return 0; /* indicate program ended successfully */
    
    } /* end function main */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hollow square
    By mdnealy in forum C# Programming
    Replies: 8
    Last Post: 07-05-2009, 04:23 PM
  2. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 02:27 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  4. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 08:33 AM
  5. Hollow square
    By Kayoss in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 05:49 PM

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