Phlash6
Phlash6

Reputation: 149

Nested For loops, X-mas tree in C

So I have the task of creating a Christmas tree in C, I know this has been done to death but there are some conditions that have to be meet that leave me beyond confused, I don't even know where to start.

So we ask the user for the number of levels(how many lines in the layer) and then the number of layers.

Now, each line after the first in each layer will add 2 " * " one to each side of the first( which is just a line with one " * ".) And we do this until the number of levels in the layer is meet, then the next layer is started. When I new layer is started we subtract 4( 2" * " from each side, of the last level in the previous layer, and then repeat the process of adding 1 " * " to each side until the number of levels is meet( the number of levels is decided upon in the beginning and is constant.)

Finally the last part is finishing off the tree of the tree with a width 3, height 4 trunk made of " # ". I have no idea how I'm supposed to be setting up these loops, I'll post what I've done so far( not much I'm unsure how to proceed)

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


int main(){


int levels;
int levelcount;
int layers;
int i;
int j;

    printf(" How many levels should this tree have?\n");
    scanf("%d\n", levels);

    printf(" How many layers should this tree have?\n");
    scanf("%d\n");

for (level = 0;level <= levelcount ; levelcount++){


}








return 0;

}

for example a sample out put with 2 layers of 3 levels would be

     *
    ***
   *****
     *
    ***
   *****
    ###
    ###
    ###
    ###

Below is some updated code for where i'm at now

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


int main(){


int level;
int levelcount;
int layercount;
int layer;
int star;
int starcount;
int space;
int spacecount;
int spacenumber;

    printf("How many levels should this tree have?\n");
    scanf("%d\n", &level);

    printf(" How many layers should this tree have?\n");
    scanf("%d\n", &layer);

for (layer = 0;layer <= layercount ; layercount++){


                star= 2*level - 1;
                space= levelcount + level - star;

            for (spacecount =0; spacecount <=spacenumber; spacecount++)
                printf(" ");

            for (starcount= 0; star < starcount; starcount++)
                printf("%c" , '*');

            for (levelcount=1; levelcount=1;levelcout++)







}







return 0;

}

Upvotes: 0

Views: 471

Answers (1)

SverreN
SverreN

Reputation: 177

I'm guessing this is the result you want to see:

         *
        ***
       *****
      *******
     *********
    ***********
   *************
     *********
    ***********
   *************
  ***************
 *****************
*******************
        ###
        ###

This would be 2 levels of 6 layers. So you're first idea to use a for-loop over the levels should be good:

        ***
       *****
      *******
     *********
    ***********
   *************

This is the first level of the tree. To create the layer of this level, we'd need another, nested for-loop:

 for (level = 0;level <= levelcount ; levelcount++){
    for (layer=0; layer <= layers; layer++) {
       do_something
    }
 }

So what about the do_something? Well here is the tricky part, you have to calculate the number of stars to be printed in this line.
For this, let's add the level/layer/# of stars information to the tree:

         *            
        ***           1/1/3
       *****          1/2/5
      *******         1/3/7
     *********        1/4/9
       *****          2/1/5
      *******         2/2/7
     *********        2/3/9
    ***********       2/4/11

As for every line, two stars are added to the line we can infer that the number of start must be somthing with two times the number of lines. So for the lines my first guess would be stars = x + [(layer - 1) * 2]. This can easily be approved by setting x to the number of stars at the top of every layer. So for second layer x = 5 then 2nd line of this is 5 + [(2 - 1) * 2] = 7 or for 4th line in first layer 3 + [(4 - 1) * 2] = 9.
To get this x we need to know the number of stars in the last layer of the last level and substract 4. We could use some calculation for this as well, but let's make life easy and just remeber the number of stars we used:

 int x = 3;
 for (level = 0;level <= levelcount ; levelcount++){
    for (layer=0; layer <= layers; layer++) {
       print_num_of_stars(x + ((layer - 1) * 2)
    }
    x = x + ((layers - 1) * 2) - 4
 }

Now the last problem you have to solve is filling all the lines with the needed amount of spaces in print_num_of_stars, but this is just another for-loop too.

Hope this helped, please forgive if I messed up layers and levels sometimes.

Upvotes: 1

Related Questions