Reputation: 149
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)
I will now post my code. I'm sort of stuck on where to go in the for loop that makes the first line(level) of the next layer, have 4 less stars(2 from each side) than the last level of the previous layer.
#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;
int i;
int printstar;
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++) {
for (level = 0 ; level < levelcount ; levelcount++) {
star = levelcount + (layer - 1) * 2;
space = levelcount + level - star;
for (spacecount = 0 ; spacecount <= spacenumber ; spacecount++)
printf(" ");
for (starcount = 0 ; star < starcount ; starcount++)
printf("%c" , '*');
printstar = i + ((level-1) * 2);
}
i = i + ((levelcount - 1) * 2) - 4;
}
return 0;
}
Upvotes: 0
Views: 5704
Reputation: 1105
I am not sure this is most efficient way, but you can give it a try.
logic i have used here is:
Here is a sample code using your code snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int level;
int levelcount;
int layercount;
int layer;
int star;
int starcount;
int spacecount;
int space;
int length;
printf("How many layers: ");
scanf("%d", &layercount);
printf("How many levels: ");
scanf("%d", &levelcount);
printf("\n Chrismas Tree \n");
length = (layercount*levelcount);
starcount = 1;
spacecount = length;
for (layer = 1 ; layer <= layercount ; layer++) {
for (level = 1 ; level <= levelcount ; level++) {
for (space = 1 ; space <= spacecount ; space++)
printf(" ");
for (star = 1 ; star <= starcount ; star++)
printf("*");
printf("\n");
starcount += 2;
spacecount--;
}
// since starcount and spacecount are incremented
// just before level loop exit
starcount -= 2;
spacecount++;
if(levelcount <= 3){
starcount -= 2;
spacecount += 1;
}
else{
starcount -= 4;
spacecount += 2;
}
}
spacecount = length;
for (layer = 1 ; layer <= 4; layer++) {
for (space = 1 ; space <= spacecount-1 ; space++)
printf(" ");
for (star = 1 ; star <= 3 ; star++)
printf("#");
printf("\n");
}
return 0;
}
Output:
How many layers: 2
How many levels: 6
Chrismas Tree
*
***
*****
*******
*********
***********
*******
*********
***********
*************
***************
*****************
###
###
###
###
some of the mistakes in your code was, you have not properly handled any of the for loops exit condition, not incrementing layer
and level
variables, and using uninitialized spacenumber
etc.
Do some reading on for
loops it will help you to understand.
Upvotes: 2