Thread: Hollow Rectangle function

  1. #1
    Registered User IDC Tyler's Avatar
    Join Date
    Sep 2015
    Posts
    6

    Hollow Rectangle function

    I know this question probably gets asked often enough, but I was just hoping someone could tell me what is wrong with my code.. or really just hint at it. I am creating a function that takes in a width and height and gives a trace of a rectangle with those dimensions, however I cannot get the inside to be hollow. As of now, I have the middle showing as 0's so I can see what my code is doing.

    Thanks for any help you can offer.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void drawRect(int x, int y)
    {
        int i;
        int j;
    
    
        for (i = 0; i < x; i++)
        {
            for (j = 0; j < y; j++)
            {
                if(0 < i < x && 0 < j < y)
                {
                    printf("x");
                }
                else
                {
                    printf("0");
                }
            }
            printf("\n");
        }
    }
    
    
    
    
    //Testing function
    int main()
    {
    
    
        drawRect(3,2);
        printf("\n");
    
    
        drawRect(8,8);
        printf("\n");
    
    
        drawRect(4,7);
        printf("\n");
    
    
        return 0;
    }
    Last edited by IDC Tyler; 09-10-2015 at 03:23 PM.

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Hint: Don't think of a rectangle as a single 2D figure. Think of it as made of three parts, vertically:
    The top,
    XXXXX
    the inside rows,
    X000X
    X000X
    and the bottom,
    XXXXX
    Furthermore, each of the three can be separated horizontally to three parts: Left edge, the center columns, and right edge. Whether you need to split it in practice is debatable, and really up to you.

    The point is, split the rectangle to its constituent parts, and work each part out at a time, in the correct order.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Two observations about your code:

    1.
    Code:
     0 < i < x && 0 < j < y
    This syntax is most likely not doing what you want it to. Let me try to explain why. The above line is what's called an 'expression', and an expression is a thing which can contain sub-expressions, and is evaluated according to operator precedence. For instance:

    Code:
    // The individual expressions are evaluated according to the precedence of the operators
    y * width + x; // ((y * width) + x);
    So when you write

    Code:
    // Same precedence, but with left to right associativity
    4 > 3 > 2; // ((4 > 3) > 2)
    This will evaluate to false, because (4 > 3) evaluates to a true or false value, either 1 or 0 (respectively). So picture it being evaluated:
    Code:
    ((4 > 3) > 2);
    ((1) > 2); // false;
    2. You are printing straight to output, which naturally inserts new characters to the right of older ones, and then goes downward (in the same manner that a person reads). This can make drawing difficult, because you are forced to do things in a specific order (eg, draw the top, then sides, then bottom). Instead, what if you mimicked the output by creating a character buffer:

    Code:
        char output[CONSOLEHEIGHT][CONSOLEWIDTH];
    Then you could more easily draw to the array, and then only copy the array to output when the characters for the rectangle are already set (and you can do so in any order).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  4. #4
    Registered User IDC Tyler's Avatar
    Join Date
    Sep 2015
    Posts
    6
    I got my code to work with the following if statement:
    Code:
    if((i == 0 || i == x - 1) || ((i > 0 && i < x - 1) && (j == 0 || j == y - 1)))
                {
                    printf("x");
                }
                else
                {
                    printf(" ");
                }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about drawing the hollow rectangle and diamond
    By man silence in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2013, 10:18 AM
  2. Hollow triangle
    By CProgramming11 in forum C Programming
    Replies: 3
    Last Post: 10-01-2010, 12:29 PM
  3. Help me to draw a hollow rectangle in C++!
    By blackhawk65 in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2010, 01:12 PM
  4. Replies: 19
    Last Post: 06-23-2010, 02:58 AM
  5. hollow rectangle
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-07-2002, 07:03 AM