Home > Software design >  C Programming ASCII Christmas Tree with different heights
C Programming ASCII Christmas Tree with different heights

Time:01-11

I am trying to make a Christmas tree using for loops. The left and right tree side have the same pattern with whitespaces in-between with different amount of "i" inside while growing in width when the users inputs a different height.

The Left side is printed after a specific amount of spaces on the left which i want to use a variable for(leftspace).

I also want to use a variable for the inner space in order to print the "i" with modulo % periodically, equal to how much height the user inputs.

The last Colum does never have an "i" a _ (ground variable) is printed there.

Iam not even getting the first height right and iam not sure if i should use strings or more nested loops here, but i think i got lost while trying for 2 days now can you help me out please?

#include <stdio.h>
#include <stdlib.h>

void christmastree(int n){
    
        while (n > 0) {
        n--;

        int colums = 3;
        int leftspace, innerspace, leftslash, rightslash, ground;
        for (int colums = 1; colums <= 3; colums  ) {
            for (leftspace = colums; leftspace <= 5; leftspace  ) {
                printf(" ");
            }
            for (int leftslash = 1; leftslash <= 1; leftslash  ) {
                printf("/");
                //I want to print the innerspace till the point where the 
                //first rightslash   1 is 
                //to create the pyramid shape by that
                if (colums > 1) {

                    for (innerspace = 1; innerspace <= colums ; innerspace  )
                    {
                        printf(" ");
                    }
                }
                for (int rightslash = 1; rightslash <= 1; rightslash  ) {

                    printf("\\");
                }
            }
            
            printf("\n");

            //Printing the ground from the leftslash to the rightslash also created error and did
 
            //not work right

            /**if (leftslash <= 1 || colums = 3)

            {
                for (int ground = 1 ; ground <= rightslash; ground  ) {

                        printf("_");

                    }

                }**/

        }

    }

}

int main()

{
    christmastree(1);


    return 0;

}

heres a picture of my current Output:Output

and this is how the Output should look with height (1):Height 1 (4):Height 4 (10):Height 10

CodePudding user response:

I have read your code. Here are some insights from me (would also provide the code based on them):

  • Here (also generally in such questions) we have to preferably keep a record of total_items such that the current iteration would need to be subtracted or added to the current value. So, I would suggest always keeping the value of the actual argument stored without any change. So, taking a variable such as total_height as a parameter and then assigning it's value to n will make it easier.
  • leftslash and rightslash have no such use, there is no need to define them specifically.
  • ground also don't seem to have any significance, and one thing to note is: It would always be a part of innerspace, so would be calculated within the same loop of that of innerspace, and not separately as you were proposing in the later commented part of the code.
  • As you go more into coding, you would prefer to use loops starting from 0 because that's how arrays are intended to work, however, it's not a big deal.

Where you went wrong?

leftspace <=5 is not correct, as the space required per height has to be set as per the current height of the tree, so it has to be dynamic. Similarly, innerspace <= colums is inaccurate, as it don't just depends on column but also what height it is, and within that height what column it is.


Based on all the above points, I have developed a code for the same with comments, if you feel any issue, please feel free to discuss. Here is my code:
#include <stdio.h>
#include <stdlib.h>

void christmastree(int total_height){
    int n= total_height;
    int space_per_height= 2; //Extra Space Required per Unit Height Increase, also for every Column Height
    
    // Body of Tree
    while (n > 0) {
        int leftspace, innerspace;
        for (int colums = 1; colums <= 3; colums  ) {
            for (leftspace = colums; leftspace < n   space_per_height; leftspace  ) {
                printf(" ");
            }

            printf("/");
            
            int total_inner_space= (total_height-n)*space_per_height   (colums-1)*space_per_height; 
            // Inner space increase by 2 according to tree height; also Inner space increase by 2 for every column within that height as well
            
            for (innerspace = 1; innerspace <= total_inner_space; innerspace  )
            {
                if(colums==3){
                    // For every first and final space of final column of every height, '_' has to be placed
                    if(innerspace==1 || innerspace== total_inner_space){
                        printf("_");
                        continue;
                    }
                }
                // In the final column of final height, all the inner-spaces will be '_'
                if(n==1 && colums==3){
                    printf("_");
                    continue;
                }
                // At every 4th inner-space 'i' will be placed, but it starts this pattern from inner-space:2 
                if((innerspace 2)%4==0)
                    printf("i");
                else
                    printf(" ");
            }

            printf("\\");
            
            printf("\n");
        }
        n--;
    }

    // Base of Tree: ## (Fixed Length:3)
    for (int i = 0; i < 3; i  )
    {
        for (int j = 0; j <= total_height; j  )
        {
            printf(" ");
        }
        printf("##\n");
    }

}

int main()

{
    christmastree(10);

    return 0;

}
  •  Tags:  
  • Related