Wrong output when printing a tent shape with stars

87 views Asked by At

I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.

following is my code:

void printTent(int n)
{   
    int j = 1;
    int i = 1;

    if (n == 1) {
        printf("*");

    } else {
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                printf(" ");
            }
            if(j == n) {
                printf("*");
                for(j = 1; j <= n; j++) {
                    printf(" ");

                }
            }
        }
    }
}


int main()
{
    printTent(4);
}

Output obtained:

    *        *        *        *    

Desired output:

   *
  * *
 *   *
*     *
3

There are 3 answers

0
J...S On BEST ANSWER

I don't think you will need that

if (n == 1) {
    printf("*");
}

We can take care of that in what you've written in the else part.

For n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.

You seem to be trying to accomplish that with your first inner loop. But

for(j = 0; j < n; j++) {
    printf(" ");
}

will always print n spaces. We need to reduce the number of spaces printed by 1 on each iteration of the outer loop.

Coming to your second loop,

for(j = 1; j <= n; j++) {
    printf(" ");
}

This has a similar problem only difference being the incrementation of the number of spaces printed.

Try something like this

void printTentNMMod(int n)
{
    int j;
    int i;

    for(i = 0; i < n; i++) {
        for(j = i; j < n; j++) {
            printf(" ");
        }
        printf("*");
        if(i!=0)
        {
            for(j=0; j<2*(i-1)+1; ++j)
            {
                printf(" ");
            }
            printf("*");
        }
        printf("\n");

    }
}

Also, you could shorten this to

void printTent(int n)
{
    int j;
    int i;

    for(i = 0; i < n; i++) {
        printf("%*c", n-i, '*');
        if(i!=0)
        {
            printf("%*c", 2*i, '*');
        }
        printf("\n");

    }
}

The * in %*c will set the number of places occupied by the character printed by the %c.

0
MiniMax On

Another way.

#include <stdio.h>

int main() {
    int i, j;
    int height = 5;
    for(i = height; i > 0; i--) {
        for(j = 1; j < height * 2; j++) {
            if(j == i || j == height * 2 - i)
                printf("*");
            else
                printf(" ");
        }
        puts("");
    }
    return 0;
}

Output

    *    
   * *   
  *   *  
 *     * 
*       *
0
jyj6536 On

I've finished it and I have written annotation.

void printTent(int n)
{
    int j = 1;
    int i = 1;

    if (n == 1) {
        printf("*");

    }
    else {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
                printf(" ");
            }
            if (j == n-i) { //
                printf("*");
                for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
                {
                    printf(" ");
                }
                if (i != 0)//the first line only needs one "*"
                    printf("*");
                printf("\n"); //Line breaks
            }
        }
    }
}