Thread: Star/character Box

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Star/character Box

    I need a little help with this program. I need it to ask the user to enter a number and then a character. It will print out for example with values 5 and R,
    RRRRR
    R___R
    R___R
    R___R
    RRRRR

    (where the _ is white space)

    I know I need to use another while or for loop (I'm guessing while), but I can't figure out where to put it. Here's the portion of the code that needs the work.
    Thanks guys
    Code:
    int printSquare(int size, char character)
    {
    int i = 1;
    int counter = 1;  
       for (i = 0; i < size; i++)
       {
          while (counter <= size)
          {
             counter++;
             printf("%c", character);
          }
    
       printf("\n");  
       counter = 1;
       }  
    return;
    }
    It currently prints out:
    RRRRR
    RRRRR
    RRRRR
    RRRRR
    RRRRR.

    Any hints or suggestions? Thanks again!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Don't try to use just a single nested loop to do it all, instead use several loops. One to print the top line, one to print the bottom line, and one with another nested inside to handle the lines with spaces in the middle:
    Code:
    #include <stdio.h>
    
    void printsquare ( int width, int height, char c )
    {
      int i;
      int j;
    
      for ( i = 0; i < width; i++ )
        putchar ( c );
      putchar ( '\n' );
      for ( i = 1; i < height; i++ ) {
        putchar ( c );
        for ( j = 1; j < width - 1; j++ )
          putchar ( ' ' );
        printf ( "%c\n", c );
      }
      for ( i = 0; i < width; i++ )
        putchar ( c );
      putchar ( '\n' );
    }
    
    int main ( void )
    {
      printsquare ( 5, 5, '*' );
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Actaully, a Single nested loop would work fine:
    Code:
    void drawSquare(int s, char c) {
    	for( int i = 0; i < s; i++ ) {
    		for( int j = 0; j < s; j++ ) {
    			if( (i == 0 || i == s-1) || (j == 0 || j == s-1) )
    				printf("%c",c);
    			else
    				printf(" ");
    		}
    		printf("\n");
    	}
    }
    // use me like this in main program:
    drawSquare( 5, 'R');
    :P

    P.S: FIgured I'd put SOME explination to the code
    basicly the nested loop gives you the sqaure traversal row by column.
    Code:
    i == 0
    // this accounts for any and all cases where we are in the top row of the square
    i == s-1
    // this accounts for any and all cases where we are in the bottom row of the sqaure
    j == 0
    // this accounts for any and all cases where ew are in the left most column of the square
    j == s-1
    // this accounts for any and all cases where we are in the right most column of the square
    // any other situation we are inside the actualy sqaure boundry, so we draw a space.
    Last edited by dbgt goten; 11-05-2003 at 12:33 PM.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Actaully, a Single nested loop would work fine
    I didn't say it wouldn't work, I meant that a single nested loop would be more difficult to visualize. There are a number of ways to get the correct output.
    My best code is written with the delete key.

  5. #5
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Originally posted by Prelude
    >Actaully, a Single nested loop would work fine
    I didn't say it wouldn't work, I meant that a single nested loop would be more difficult to visualize. There are a number of ways to get the correct output.
    All I ment was that it isn't that difficult to come up with a single nested loop solution. I merely think its a better practice to go a different rout then the easiest way. Im not makin this a personal attack on you or your code. I think it woulda been better to give a solution more related to what he was trying to do then to give him and entirely different solution. This way he can find/realize the error in his solution, and fix it.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Awesome! Sorry it took so long to reply. I appreciate the help from the both of you, Prelude and dbgt goten! Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM