I am trying to print a rectangle using this symbol *. When I print it, it draws an L.
What is wrong with my code?

#include <stdio.h>

void rect(int base, int height)
{

int x = 0,y = 0;

while(x < height)
{
	x++;
		if(x == 1 || x == height)
		{
			while(y < base)
			{
			y++;
			printf("*");
			}
		}
		printf("\n*");

}

}
int main ()
{
	rect(5,5);
	system("pause");

  return 0;
}

The device you're working with, only works from top line to bottom line (unless you do some repositioning of the cursor, which is not needed here).

print the top line of *'s

in a loop:
print the left side * print the spaces needed, then print the right side *
continue printing the above sides of the rectangle, until you've reached the
full height of the diagram.
end loop

Now print the bottom line of *'s.

That is your pseudo code logic. The empty space inside the rectangle is the width of the rectangle - 2, of course.

Give that a shot! ;)

Edited 6 Years Ago by Adak: n/a

Thanks for your help.

#include <stdio.h>

void rect(int base, int height)
{

int x = 2,y = 0,space;

while(x < height)
{
	x++;

	while(y < base)
	{
		y++;
		printf("*");
	}
	printf("\n");
	printf("*");
	space = base - 2;
	while(space)
	{
	space--;
	printf(" ");
	}
	printf("*");
}
	y = 0;

	printf("\n");
	while(y < base)
	{
		y++;
		printf("*");
	}
printf("\n");
}
int main ()
{
	rect(5,5);
	system("pause");

  return 0;
}
This question has already been answered. Start a new discussion instead.