1. ## 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.

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;
}``` 2. 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. 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). 4. 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 