Help making a hollow rectangle with thickness border

This is a discussion on Help making a hollow rectangle with thickness border within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> main (void) { //variables int width, height, thick, i, j; //user input printf("Enter the width of the ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    9

    Help making a hollow rectangle with thickness border

    Code:
    #include <stdio.h>
    
    main (void)
    {
        //variables
        int width, height, thick, i, j;
        
        //user input
        printf("Enter the width of the rectangle.\n");
        scanf("%d", &width);
        
        printf("Enter the height of the rectangle.\n");
        scanf("%d", &height);
        
        printf("Enter the thickness of the rectangle's border.\n");
        scanf("%d", &thick);
        
       //making the box top
        for(i = 0; i < width; i++)
        {    
            printf("*");
        }
    
        printf("\n");
    
        //
        for(i = 0; i < (height-2); i++)
        {    
            printf("*");
    		
            for(j = 0; j < (width-2); j++)
            {
                printf(" ");
            }
            
            printf("*");
    
            printf("\n");
    	}
    	
    	//making the box bottom
    	for(i = 0; i < width; i++)
    	{
    	   printf("*");
        }
    	
    	printf("\n");
    	
    	system("PAUSE");
    	return 0;
    }
    This code will produce hollow rectangles but I cannot put my head around where to insert or how to insert the border pieces.

    Very new to this so I am looking for code help as well as advice. Please do not just use logic.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    Sample output: BOXES SHOULD LOOK NORMAL, FORUM FORMATTING ERROR

    Problem 2: Hollow Rectangle
    Print a hollow rectangle of asterisks to the screen. You must read in the width, height, and border thickness of the rectangle from the user. The meaning of these numbers is best understood through examples. If the user enters numbers that don't produce a valid rectangle, your program should instead print a message indicating that there is a problem with the input.

    Sample output 1 (Program output is bold, user input is in italics)
    Enter the width of the rectangle
    8
    Enter the height of the rectangle
    7
    Enter the thickness of the rectangle's border
    3
    ********
    ********
    ********
    *** ***
    ********
    ********
    ********

    Sample output 2 (Program output is bold, user input is in italics)
    Enter the width of the rectangle
    12
    Enter the height of the rectangle
    6
    Enter the thickness of the rectangle's border
    2
    ************
    ************
    ** **
    ** **
    ************
    ************

    Sample output 3 (Program output is bold, user input is in italics)
    Enter the width of the rectangle
    6
    Enter the height of the rectangle
    10
    Enter the thickness of the rectangle's border
    4
    The thickness you specified is too great relative the width of the rectangle.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Here is the code for making the top and bottom borders:
    Code:
       //making the box top
        for(i = 0; i < width; i++)
        {    
            printf("*");
        }
    Now, just do that border times.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    Border should be on all sides

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As I posted in the old thread:
    Code:
    for each row
        for each column
            if row < thickness, print a wall
            if row > box size - thickness, print a wall
            if col < thickness, print a wall
            if col > box size - thickness, print a wall
            otherwise, print a space
        print newline
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    Quote Originally Posted by quzah View Post
    As I posted in the old thread:
    Code:
    for each row
        for each column
            if row < thickness, print a wall
            if row > box size - thickness, print a wall
            if col < thickness, print a wall
            if col > box size - thickness, print a wall
            otherwise, print a space
        print newline
    Quzah.
    I didn't understand if that means that my whole structure is wrong? (Very new)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It means exactly what it says it means. You can do it like that or you can do it other ways. Basically, you need to print N lines for the top thickness. Then you need to print N left edge and width - (N+N) spaces, and then N right edges ... for every line in the middle. Then you need to print N bottom edges.


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

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    Quote Originally Posted by quzah View Post
    It means exactly what it says it means. You can do it like that or you can do it other ways. Basically, you need to print N lines for the top thickness. Then you need to print N left edge and width - (N+N) spaces, and then N right edges ... for every line in the middle. Then you need to print N bottom edges.


    Quzah.
    Yea, like Ive said multiple times have no experience at this found some problems online to practice. If you just dont want to give an outright answer... well here is what I got with your logic, can you help.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, colum, rows, border;
        
        printf("Enter the width: ");
        scanf("%d",&colum);
        
        printf("Enter the height: ");
        scanf("%d", &rows);
    
        printf("Enter border thickness: ");
        scanf("%d", &border);
    
    // rectangle start
    	for(i = 0; i < rows; i++)
    	{
            for(j = 0; j < colum; j++)
            {
                if(rows < border)
                {
                    printf("*");
                }
                if(rows > (rows - border))
                {
                    printf("*");
                }
                if(colum < border)
                {
                    printf("*");
                }
                if(colum > (rows - border))
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
                printf("\n");
            }
        }
        system("PAUSE");
        return(0);
    }
    doesnt work at all

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are printing a newline after each column, not after each row. "Doesn't work" doesn't tell me what your problem is.


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

  10. #10
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    It might be easier for you if you add a function to print n characters

    void print_char( int n, char ch);

    now in this function do a loop to print ch n times; then all you will have to do is call this to print the characters. It will help you clean up the loop you are trying to make to do the box.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    There are several problems in the code. The first most glaring and obvious mistake is the "rows < border" lines. You are comparing against rows, which is the number of rows the rectangle is supposed to be made of.
    Perhaps you've misinterpreted the logic. if row < thickness, print a wall basically means "if current row < thickness, print a wall."
    Fix that first.

    Secondly is another logic problem and the third is a off-by-one error. I recommend you try to take quzah's algorithm and write it down on paper and see how it behaves before going further.
    Besides that, avoid system("pause"). Alternatives can be found here: http://cpwiki.sourceforge.net/pause_console
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    i am doing the same program and quzah you have helped alot but im stuck on one part. i cant figure out how to get the right border
    when i run program for width : 12 height :6 and thickness:2 i get
    **********
    **********
    **
    **
    **********
    **********

    heres my code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, height, width, thickness;
        
        printf("Enter the width of the rectangle\n");
        scanf("%d",&width);
        printf("Enter the height of the rectangle\n");
        scanf("%d", &height);
        printf("Enter the thickness of the rectangle's border\n");
        scanf("%d", &thickness);
    
        if(thickness > width || thickness > height)
            printf("The thickness you specified is too great relative the width of the rectangle.\n");
            
    	for(i = 0; i < height; i++)
            {
            for(j = 0; j < width - thickness*2; j++)
            {
                
                if(i < thickness)
                    {
                        printf("*");   //prints top part
                    }    
                if(i >= (height - thickness)) 
                    {       
                        printf("*");   // prints bottom part
                    }
                    
               if(j < thickness)
                   {
                       printf("*");  //prints left side
                   }
                //if(j >= (width - thickness))
                //    {
               //         printf("*");         // for some reason this side wont print not sure if i did it wrong
               //         i++;
               //     }
                    
            }     
            
               printf("\n"); 
               
    }	
    
    
    	system("PAUSE");
    	return 0;
    }

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Isn't width supposed to be the entire width of the rectangle?
    Then you may want to ask yourself why you have a condition in your loop that looks like
    width - thickness*2

    The best advice I can give is that you write down on paper how exactly the algorithm is supposed to work.
    Once you realize how it works and picture it in your head, then it's time to code.
    Also take a look at http://cpwiki.sourceforge.net/pause_console
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    Quote Originally Posted by quzah View Post
    You are printing a newline after each column, not after each row. "Doesn't work" doesn't tell me what your problem is.


    Quzah.
    That's because I can not understand what you're telling me before you at least critique what is and is not correct about my original piece of code. Also, it is difficult to express empathy in short text conversations, so I do want to state that I am asking in respect.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    You are printing a newline after each column, not after each row. "Doesn't work" doesn't tell me what your problem is.


    Quzah.
    Oh that must be a lie - noobs tell us "it doesn't work", all the time!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. GUI structure in C
    By officedog in forum C Programming
    Replies: 36
    Last Post: 11-19-2008, 01:33 PM
  3. Making Rectangle List Non-Overlapping
    By SMurf in forum Game Programming
    Replies: 2
    Last Post: 05-06-2006, 01:23 PM
  4. hollow rectangle
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-07-2002, 07:03 AM
  5. more with my 1st structure program
    By sballew in forum C Programming
    Replies: 42
    Last Post: 10-22-2001, 08:03 PM

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