Cesare
Cesare

Reputation: 9419

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?

Upvotes: 3

Views: 9313

Answers (4)

Gabriel
Gabriel

Reputation: 11

your ideea is good, and the code written is intuitive and easy to read. Mabye a little more attention next time on if statements and everything would be fine :) for (a = 0; a != height; a++) {

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

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

variable a equals to height-1 means that the last line will be filled with (*) and width-1 will fill the last colimn. Think at your rectangle as a matrix

Upvotes: 1

Motomotes
Motomotes

Reputation: 4237

To print a rectangular box:

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

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

        if (a == 0 || a== height-1 ) {
            printf("*");
       }else{
            if(b == 0 || b == width-1){
                printf("*");
            }else{
                printf(" ");
            }
       }
    }

    // ok, the line has been filled, new line
    printf("\n");
}

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

There is a logical problem. You have to print * under following boundary conditions

  • a == 0
  • a == height-1
  • b == 0
  • b == width-1

Also, when you're not printing the boundary * s, you need to print space [] to make the structure form.

For some more good practices, check the below code and the comments

#include <stdio.h>

int main()           //put proper signature

{
        int width = 0, height = 0, b = 0, a = 0;  // initalize local variables

        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) || (a == height-1) || (b == width-1) || (b == 0)){  // put all * printing condition in one place
        //also, conditions, (a == height-1) and (b == width-1) to be used
                                printf("*");
                        }
                        else   // if not to print *, print space
                                printf(" ");
                }
                        printf("\n");
        }
        return 0;    // add a return statement.
}

Upvotes: 1

glglgl
glglgl

Reputation: 91119

You are missing the parts where no * should be printed. What else do you imagine to be printed then? Probably a blank.

So you should do something like

if (cond1 || cond2 || cond3 ||cond4) {
    printf("*");
} else {
    printf(" ");
}

Upvotes: 1

Related Questions