Home > OS >  Printing a christmas tree using stars and dots in C
Printing a christmas tree using stars and dots in C

Time:12-29

I'm trying to write a program that prints out a christmas tree that looks like this:

enter image description here

The user inputs the height, in this example the height is 6. If the input is in range from 0 to 3, the height should be 3, because otherwise it's not printable, and if the input is less than 0, the program should terminate.

My code for some odd reason is infinitely printing the 'Input height'. Where is the error?

Here's my code snippet:

#include <stdio.h>
    
void main(){
    int i, j, n, s;

    while (1){

        printf("Input height: ");
        scanf("%d", &n);

        if (n < 0) break;

        if (n == 0 || n == 1 || n == 2 || n == 3)
            s == 3;
        else
            s == n;

        for (i = 0; i < s; i  ){
            for (j = 0; j < 2*s - 1; j  ){
                if (j > s - (i - 1) && j < (s   (i - 1)) - 1)
                    printf("*.");
                if (j == s   (i - 1))
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }

        for (j = 0; j < 2*s - 1; j  ){
            if (j == s - 1 || j == s || j == s   1)
                printf("*");
            else
                printf(" ");
        }
    }
}

CodePudding user response:

The lines: s == 3; and s == n; do absolutely nothing.
== is a comparison, not an assignment.

Here is much better code:

#include <stdio.h>

int main(void) {
    int n = 8;
    
    char row[2*n];
    for( int i=0; i<2*n-1; i =2 )
    {
        strcpy(row i, "*.");
    }

    for(int i=0; i<n;   i)
    {
        printf("%*.*s\n", n i 1, 2*i 1, row);
    }
    printf("%*s\n", n 2, "***");
    
    return 0;
}

Result:

Success #stdin #stdout 0s 5464KB

        *
       *.*
      *.*.*
     *.*.*.*
    *.*.*.*.*
   *.*.*.*.*.*
  *.*.*.*.*.*.*
 *.*.*.*.*.*.*.*
       ***

With a little creativity, I made the program even shorter with only a single for-loop.

#include <stdio.h>

int main(void) {
    int n = 8;
    
    char row[2*n];
    strcpy(row, "*");
    for( int i=0; i<n;   i )
    {
        printf("%*s\n", n i, row);
        strcat(row, ".*");
    }
    printf("%*s\n", n 1, "***");
    
    return 0;
}

CodePudding user response:

As mentioned by others there are some issues with you mixing up == and =. I will be posting a version that prints out the christmas tree but leaves out the . that you also want to include, as you should be able to finish it yourself.

#include <stdio.h>

int main()
{
    int i, j, n, s;

    while (1)
    {
        printf("Input height: ");
        scanf("%d", &n);

        // if input is negative, exit
        if (n < 0)
        {
            break;
        }

        // if input is 0,1,2 or 3 change to 3
        if (n == 0 || n == 1 || n == 2 || n == 3)
        {
            s = 3;
        }
        else
        {
            s = n;
        }

        // loop through each row
        for (i = 0; i < s; i  )
        {
            // loop through each column
            for (j = 0; j < 2 * s - 1; j  )
            {
                // if column is within the tree print a star
                if (j >= s - i - 1 && j <= s   i - 1)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }

        // print base of tree
        for (j = 0; j < 2 * s - 2; j  )
        {
            // if column is part of base print star
            if (j == s - 2 || j == s - 1 || j == s)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

CodePudding user response:

A simple solution:

#include <stdio.h>

int main(){
    int i=0, j=0, n=0, s=0;

    while (1){

        printf("Input height: ");
        scanf("%d", &n);
        printf("\n");
        
        if (n < 0) break;
        s = (n <= 3) ? 3 : n;

        for (i=0; i < s;   i){  // rows
            for (j=0; j < s-i;   j) // white spaces
                printf(" ");
            
            for (int k=0; k < i;   k) // *.
                printf("*.");

            printf("*\n"); // always, unique or last *
        }
        
        for (i=0; i < s-1 ;   i)
            printf(" ");
        printf("***\n\n");
        
    }
    return 0;
}
  • Related