W4nnaCryy
W4nnaCryy

Reputation: 41

C - Drawing rectangle using * special character based on input (For loop)

I am quite new to programming and I have got an assignment which I cannot move with.

So the program must take the user input, the length and width of a rectangle and then draw the rectangle using stars * based on the parameters length and width. I have to do it using for loops, more precisely one for loop inside the other. It does not work and I am stuck with it, so please if someone would be so kind to help me, I would be thankful.

Here is what I got:

int main() {
    
    printf("\n\n***Rectangle***");

    printf("\n\n\nInsert side a: ");
    scanf("%f", &sideA);
    printf("\n\nInsert side b: ");
    scanf("%f", &sideB);
    
    printf("\n\nRectangle: \n\n");
    
    for (int i = 0; i < sideA; i++) {
        for (int j = 0; j < sideB; j++) {
            if (i == 0 || i == sideB - 1 || j == 0 || j == sideA - 1) {
                printf("*");
            } else {
                printf(" ");
            }
            printf("\n");
        }
    }
}

Thanks so much in advance

Upvotes: 2

Views: 2346

Answers (2)

chqrlie
chqrlie

Reputation: 144969

There are multiple problems:

  • you should include <stdio.h>
  • sideA and sideB should be defined as int.
  • scanf("%f", &sideA); should be scanf("%d", &sideA); and the same for sideB. Furthermore, you should test for input failure: scanf() returns the number of successful conversions, which must be 1 in this case.
  • you have a simple confusion on the coordinates: i should be compared to sizeA and j to sizeB.
  • the newline should be printed outside the inner loop:

Here is a corrected version:

#include <stdio.h>

int main() {
    int sideA, sideB;

    printf("\n\n***Rectangle***");

    printf("\n\n\nInsert side a: ");
    if (scanf("%d", &sideA) != 1)
        return 1;

    printf("\n\nInsert side b: ");
    if (scanf("%d", &sideB) != 1)
        return 1;
    
    printf("\n\nRectangle: \n\n");
    for (int i = 0; i < sideA; i++) {
        for (int j = 0; j < sideB; j++) {
            if (i == 0 || i == sideA - 1 || j == 0 || j == sideB - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

Upvotes: 2

Clifford
Clifford

Reputation: 93554

You have two problems with this code:

  1. You are printing newline for each character rather then each row.
  2. You have i and j the wrong way around in your '*' test.

Move the newline to the outer loop, swap 'i' and 'j' in the if condition:

for (int i = 0; i < sideA; i++) 
{
    for (int j = 0; j < sideB; j++) 
    {
        if (j == 0 || j == sideB - 1 || i == 0 || i == sideA - 1 ) // MODIFIED
        {
            printf("*");
        }
        else 
        {
            printf(" ");
        }
    }
    printf("\n");  // MOVED
}

Alternatively be "less clever" and break it down thus:

  1. Print a line of width asterisks ('*') followed by newline.
  2. Print length - 2 lines starting '*', followed by width - 2 spaces, then '*' + newline.
  3. Repeat 1.

Upvotes: 0

Related Questions