Thread: Rather Simple Question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    Question Rather Simple Question

    I'm new here and it seems like this is exactly the place I need for help. I have this assignment having to do with the infamous "hollow rectangle". I've searched this forum and read my text book and the part I'm having trouble with is too specific to be mentioned anywhere.

    My problem is not getting the hollow part, what I need to do is be able for the user to enter the "thickness" of the border. I just can't wrap my head around how to do it. I know it needs to be within the for-loops I have (at least I'm pretty sure it does) .

    Here's what I wrote so far:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, height, width, border;
        
        printf("Enter the width: ");
        scanf("%d",&width);
        
        printf("Enter the height: ");
        scanf("%d", &height);
    
        printf("Enter border thickness: ");
        scanf("%d", &border);
    
    	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");
    	}
    	
    	for(i = 0; i < width; i++)
    	     printf("*");
    	
    	printf("\n");
    	
    	system("PAUSE");
    	return 0;
    }
    So to be clear, what I have now gives me a border size of 1, and whatever the user specifies as the width and height. I need to be able to choose the border thickness, for example if I chose a thickness of 2, a width of 6, and a height of 6, it would look like this:

    ******
    ******
    **00**
    **00** (zeros = blank space)
    ******
    ******

    I don't expect someone to write it out of course because I would much rather learn, but any help at all would greatly be appreciated

    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    ask width
    ask height
    ask thickness
    
    for each row of height
        for each column
            if row counter is less than thickness, this is a 'row wall'
            if column counter is less than thickness, this is a 'column wall
            if row counter is not less than height - thickness, this is a 'row wall'
            if column counter is not less than thickness, this is a 'column wall'
            if none of the above, it's a 'hollow'
    The logic works something like that.


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

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by quzah View Post
    Code:
    ask width
    ask height
    ask thickness
    
    for each row of height
        for each column
            if row counter is less than thickness, this is a 'row wall'
            if column counter is less than thickness, this is a 'column wall <----------------
            if row counter is not less than height - thickness, this is a 'row wall'           
            if column counter is not less than thickness, this is a 'column wall' <-----------
            if none of the above, it's a 'hollow'
    The logic works something like that.
    Thank you for responding I have a few questions though.

    Wouldn't the two lines with column walls be pointless?

    "If it's less, it's a column wall.
    If it isn't less, it's a column wall."

    And by row/column counters, do you mean my variables i and j?

  4. #4
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Quote Originally Posted by NeedHelpWithC View Post
    Wouldn't the two lines with column walls be pointless?

    "If it's less, it's a column wall.
    If it isn't less, it's a column wall."
    Why yes? Why not?
    Those lines are just logic steps before code...
    And by row/column counters, do you mean my variables i and j?
    Yes, he does
    Last edited by ZaC; 06-18-2009 at 04:19 PM.
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to either add more if statements inside your current for loops, or break it up, and include more nested loops, inside the major for loop.

    Another way to break it down into parts:

    1 for loop for the top border rows
    1 for loop for the central area, with if statements to handle:

    a) the leftmost border
    b) either the center section or the rightmost border (the other will be handled by default in your loop, perhaps.

    1 loop for the bottom border rows

    Instead of just "banging it out" on the keyboard, do it by hand and as you do it, ask yourself "what am I doing, step by step?".

    Record that on a separate paper, and that's the start of your pseudo code to code the program.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if row counter is not less than height - thickness, this is a 'row wall'           
    if column counter is not less than width - thickness, this is a 'column wall'
    Well if you actually understood the logic, you'd have understood that it was a typo or omission.


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

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Thanks for all the help. I was thinking you meant to put width, but I just started c programming and wanted to make sure.

    Thanks again

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    Thank you everyone, this helped me out quite a bit as well, I was able to write the program and understand exactly what I did.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    I have been racking my head on this same exact problem. I can make the box but have no idea how to create the thickness border, I even read through the posted logic and now I'm even more confused.

    Anyone please help?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a new thread, post your attempt.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM