draw an empty square of asterisks

This is a discussion on draw an empty square of asterisks within the C Programming forums, part of the General Programming Boards category; Hi everyone, im new to C and I'm stuck doing an exercise that asks me to draw an empty square ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65

    draw an empty square of asterisks

    Hi everyone, im new to C and I'm stuck doing an exercise that asks me to draw an empty square of " * " using "while" and where n = side (side must be >=1 and <= 20) I succeeded to make a square of asterisks like this:
    Code:
    n = 6
    ******
    ******
    ******
    ******
    ******
    ******
    now i need to do the same thing but the box needs to be empty:
    Code:
    n = 6
    ******
    *    *
    *    *
    *    *
    *    *
    ******
    This is the code i wrote for the first exercise:
    Code:
    #include <stdio.h>
    int main()
    {
    	int n, i, j;
    		
    	printf("Insert a number between 1 and 20:\n");
    	scanf("%d", &n);
    
    
    			
    	if ((n >= 1)&&(n <=20))
    	{
    			i = 0;
    			while( i<n )
    			{
    				i++;
     				   j = 0;
    					while( j<n )
    					{
    						printf("* ") ;
    						j++;
    					}
    				printf("\n");			
    			}
    	}
    		else
    			printf("the square's side must be between 1 and 20.\n");
    
    return 0;
    }
    Any help would be appreciate, I know this should be easy to do but im still a beginner..

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of while( j < n) print asterisks, how about

    while (j < n)
    if (j == 1)
    print asterisk

    Now combine the left and right border (which are the only places you need to print up an asterisk, into one if statement:

    if( j == left border || j == right border)
    print asterisk

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    same like my assignment lastweek.. hahaha..
    if you look the combination
    Code:
    if N=5
      1 2 3 4 5
    1* * * * *   
    2*        *
    3*        *
    4*        *
    5* * * * *
    the number is up to you(when you start, 0 or 1 at for)
    row one, row five, column 1, and column 5, else print space...
    hehehe.. hope you get the way...

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    I solved it:
    Code:
    #include <stdio.h>
    int main()
    
    {
    	int n, i, j, f;
    		
    	printf("Insert a number (1-20):\n");
    	scanf("%d", &n);
    
    
    			
    	if ((n >= 1)&&(n <=20))
    	{
    
    		i = 0;
    		while(i < n)
    		{
    			i++;
    				j = 0;
    				while(j <= n)
    				{		
    
    					if (((j > 1)&&(j < n))&&((i > 1)&&(i < n))) 
    					{
    						printf("  "); 
    					}
    					else if((j !=0 )&&(i!=0))
    					{
    						printf("* ");
    					}
    
    					j++;
    				}
    			printf("\n");			
    
    		}
    	}
    	else
    	printf("the number must be between 1 and 20.\n");
    
    return 0;
    }

    Thanks for the help ;D
    Last edited by rob90; 11-01-2009 at 06:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 10:24 AM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  3. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 08:33 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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