lundi 2 février 2015

How do I draw an empty rectangle given its width and height in C?

I will get tons of down votes eventually but I have literally been struggling with this logical exercise for hours and I would appreciate some help. Given the width and the height, the compiler should draw a rectangle.


I have therefore decided to make use of for loops, which look like the smartest way to do the job.


Here is the code I have been using:



#include <stdio.h>

main()

{

int width, height, b, a;

printf("Please insert the value of the width.\n");
scanf("%d", & width);

printf("Please insert the value of the height.\n");
scanf("%d", & height);

for (a = 0; a != height; a++) {

// fill the width
for (b = 0; b != width; b++ ) {

if (a == 0) {
printf("*");}

else if (a == width-1) {
printf("*");
}

else if (b == height-1) {
printf("*");
}

else if (b == 0) {
printf("*");
}

else {
printf(" ");
}}

printf("\n");
}
}


I feel like I am missing something, but this is so confusing. May someone please tell me why my code doesn't draw a rectangle?


Aucun commentaire:

Enregistrer un commentaire