why the last line of the chess rook is not being printed? [closed]

enter image description here
Start by accepting an integer as input, the integer will be the height of the Rook (the number of rows).
You need to use the function “drawRow” to draw the Rook as follows:
Start drawing the first row with the ‘x’ letter and then use a dash sign ‘-‘ to draw the second row, Keep alternating between the two characters each row until the top half of the Rook is drawn, If the height of the Rook is odd, you need to draw the center using the uppercase ‘H’ letter only, Start drawing the bottom half of the Rook using the same character you used to draw the last row of the top half, Keep alternating between the two characters each row until the bottom half of the Rook is drawn. It might be helpful to think about drawing the Rook in 3 separate parts: Draw the top half, draw the middle part if necessary, then finally draw the bottom half.

#include <stdio.h>

void drawRow(int numSpacesBefore, int numCharacters, char character)
{
    for (int i = 0; i < numSpacesBefore; ++i)
        printf(" ");

    for (int i = 0; i < numCharacters; ++i)
        printf("%c", character);

    printf("\n");
}

int main()
{
    int h, n, letter = 0, letter2 = 0, n2 = 0, s2 = 0, s = 0;
    char c, c2 = 'x';
    scanf("%d", &h);
    n = h;
    
    for (int i = 0; i < n; n -= 2)
    {
        if (letter == 0 && n != 1)
        {
            c="x";
            letter = 1;
            if (n == 3 || n == 2)
            {
                letter2 = 0;
            }
        }
        else if (letter == 1 && n != 1)
        {
            c="-";
            letter = 0;
            if (n == 3 || n == 2)
            {
                letter2 = 1;
            }
        }
        else
        {
            c="H";
            n2 += 2;
            s2 -= 1;
        }
        drawRow(s, n, c);
        s += 1;
    }
    
    n += 2 + n2;
    s -= 1 - s2;
    
    for (int i = h; i >= n; n += 2, --i)
    {
        if (letter2 == 0 && n != 1)
        {
            c="x";
            letter2 = 1;
        }
        else if (letter2 == 1 && n != 1)
        {
            c="-";
            letter2 = 0;
        }
        drawRow(s, n, c);
        s -= 1;
    }
    return 0;
}

Ihis is my code when I tried the input 7, my output was

xxxxxxx
 -----
  xxx
   H
  xxx
 -----

but the expected output is

xxxxxxx
 -----
  xxx
   H
  xxx
 -----
xxxxxxx

  • 2

    This code does not look overly complex. Did you even try to debug this?

    – 

  • Welcome to SO. We don’t ever want a nigh unreadable image of a homework assignment or any other text. just your code and a description of desired and actual output. I see a scanf in there. Can you provide a version with a number hard coded there so the input is included in the code?

    – 

  • Maybe OT, but your for loops with i used as a constant for 0 or for h are rather odd.

    – 




  • for (int i = 0; i < n; n -= 2) would be more clearly written as for (n; n > 0; n -= 2)

    – 

  • 1

    Welcome to Stack Overflow. Please read How to Ask. We do not write answers here that “find the bug”; we require a specific question – which will come out of your best attempt to understand and locate a specific problem, and showcase it in a minimal reproducible example. A question that is suitable for Stack Overflow is one where you have already figured out the specific part of the code that does something different from what you expect (and you should concretely expect something), and don’t understand why.

    – 

Delete , --i.

Do not modify two things in the third part of a for loop control until you have more experience and know what you are doing. Write simpler for loop statements that iterate one variable by setting it to a start value, comparing it to an end value, and incrementing or decrementing just that variable.

Do not initialize one variable and modify another. When you have more experience, you will know when more complicated loop controls are appropriate.

Leave a Comment