I am grinding away at a simple for-loop exercise where the user can enter a width and a height, and the program prints out a square with stars and spaces, like this:
Enter width and height: 8 4
********
* *
* *
********
and I have made a solution, but it just feels like I could redsuce the number of blocks with loops some... Can somebody help me with this? Here is the code:
#include <stdio.h>
int main(void)
{
int width, height;
printf("Enter width and height: ");
scanf("%d %d", &width, &height);
for(int i = 0; i < 1; i++)
{
for (int j = 0; j < width; j++)
{
printf("*");
}
printf("\n");
}
for(int i = 0; i < height - 2; i++)
{
printf("*");
for (int j = 0; j < width -2; j++)
{
printf(" ");
}
printf("*\n");
}
for(int i = 0; i < 1; i++)
{
for (int j = 0; j < width; j++)
{
printf("*");
}
printf("\n");
}
}
Thank you in advance!
One way could be:
int main(void)
{
int width = 8;
int height = 4;
for (int h = 0; h < height; ++h)
{
for (int w = 0; w < width; ++w)
{
if (w == 0 || h == 0 || h == (height-1) || w == (width-1))
{
putchar('*');
}
else
{
putchar(' ');
}
}
putchar('\n');
}
return 0;
}
That said... IMO this isn't really better than your approach. The use of 3 simple "loop-blocks" makes it very easy to see what your code is doing. This approach with a single "loop-block" and an if
statement is a bit harder to read.
So I would say...
You approach/code is fine if you just delete the lines
for(int i = 0; i < 1; i++)
as they do absolutely nothing.
And use putchar
instead of printf
for printing a single character.
Then your approach is pretty okay. If you want to avoid the first and last loop-block to be the same (i.e. avoid repeating the same code), you could put the code into a function.