Loop to change the set while moving the array - C - arrays

How to keep the smoke set changing while the rocket goes up?
I don't understood very well, but the set only changes while the rocket is at the base.And he wasn't supposed to stand still.
veja o movimento do vídeo no link ->
https://i.imgur.com/RRvFqBR.mp4
The loop for(int h = 0; h < 29; h++){ maintains the set by changing the condition of the increment, and only takes off after that. Then the set stops changing.
#include <unistd.h>
#include <stdio.h>
#define LINE 11
#define COLN 12
#define R_COLN 12
#define R_LINE 9
#define R_SET 2
#define DELAY 95000
//string to display the rocket
const char rocket[LINE][COLN+1]={
" ^ ",
" /^\\ ",
" |-| ",
" |R| ",
" |O| ",
" |C| ",
" |K| ",
" |E| ",
" /|T|\\ ",
" / | | \\ ",
" | | | | "
};
const char smoke[R_SET][R_LINE][R_COLN+1]={
{
" ' * ",
" * + . ' ",
" - . + ",
" . ' : . ",
" + ' ' * . ",
" . * . ",
" . ' : ",
" . ' . ",
" ' "
},
{
" * ' ",
" ' . + * ",
" + . - ",
" . : ' . ",
" . * ' ' + ",
" . * . ",
" : ' . ",
" . ' . ",
" ' "}
};
int main(){
int jumpControlAtBottom = 0;
int shifControl = 0;
int smoke_set = 0;
for(int h = 0; h < 29; h++){ //frame
for (jumpControlAtBottom = 0; jumpControlAtBottom < 28; ++jumpControlAtBottom){
// Jump to bottom of console
printf("\n");
}
for(int i = 0; i< LINE; i++){
printf("%.*s\n", COLN, rocket[i]);
}
for(int y=0; y<R_LINE; ++y){
printf("%.*s\n", R_COLN, smoke[smoke_set][y]);
}
smoke_set=(smoke_set+1)%R_SET; // Advance to the next set
// (or go back to the first one).
fflush(stdout); // Draw the current frame on the screen.
usleep(DELAY); // Pause to be visible.
}
for (shifControl = 0; shifControl < 28; ++shifControl){
// Rocket move on the basis of delay
usleep(DELAY);
// move rocket a line upward
printf("\n");
}
return 0;
}

Currently your logic is:
Draw one frame.
Change smoke set.
Repeat 1-2 for 29 frames.
Draw line to push frame up.
Repeat 4 to keep pushing frames up.
From that it is obvious the smoke will stop changing at step 4. So the logic needs to include the take off elevation in step 1. The easiest way to do that is to put the draw frame into a function and add the elevation as a parameter.
Here is an example:
void draw_frame(int elevation)
{
int jumpControlAtBottom = 0;
static int smoke_set = 0;
for (jumpControlAtBottom = 0; jumpControlAtBottom < 28 + elevation; ++jumpControlAtBottom){
// Jump to bottom of console
printf("\n");
}
for(int i = 0; i< LINE; i++){
printf("%.*s\n", COLN, rocket[i]);
}
for(int y=0; y<R_LINE; ++y){
printf("%.*s\n", R_COLN, smoke[smoke_set][y]);
}
smoke_set=(smoke_set+1)%R_SET; // Advance to the next set
// (or go back to the first one).
// Push image up by elevation
for (int ix = 0; ix < elevation; ix++) {
printf("\n");
}
fflush(stdout); // Draw the current frame on the screen.
usleep(DELAY); // Pause to be visible.
}
int main(){
int shifControl = 0;
// No elevation - engine starting up
for(int h = 0; h < 29; h++){ //frame
draw_frame(0);
}
// take off has occured
for (shifControl = 0; shifControl < 28; ++shifControl){
// Rocket move on the basis of delay
// move rocket a line upward
draw_frame(shifControl);
}
return 0;
}

It seems to me like you're struggling to understand the procedural nature of C, which probably means you're guessing and playing around with code from other (probably poor) examples.
This is akin to guessing how to prepare a chicken for cooking and then asking your guests how to prepare a chicken for cooking, after the chicken is cooked. You have an application that does most of what you want. How did you come up with this code, yet not know how to apply such a simple tweak?
If you don't understand the procedural nature of C, it stands to reason that any exercise that has you read the procedural nature of C in order to extract some logic and repeat it is terrible for you. Where is your book? Start with the basics.
To be clear, this answer may not seem relevant, but it does actually answer your question: you need to hoist some of your smoke-related logic out into a different function so that you can reuse it later... but before you do that, you need to be able to read C, and we're not here to do your work for you, rather to guide you in the right direction.

Related

Regarding print a pattern

Question
How can I give spaces between numbers? When I add a <<" " after cout<<j the pattern changed. Is there any other way to give spaces between numbers?
code
#include<iostream>
using namespace std;
int main(){
int i,j=1,space,star,n;
cin>>n;
i=1;
Looping
while(i<=n){
space=n-i;
while(space){
cout<<" ";
space--;
}
star=i;
while(star){
cout<<j<<" ";
j++;
star--;
}
cout<<"\n";
i++;
}
return 0;
}
output
for n=4
1
23
456
78910
I want this output :-
1
2 3
3 4 5
7 8 9 10
For the expected output you only need to add a second space in the while(space) loop:
space = n - i;
while (space) {
std::cout << " "; // note: two spaces
space--;
}
or multiply space by 2 before the loop:
space = 2 * (n - i);
while (space) {
std::cout << ' ';
space--;
}
You could also #include <string> and skip the loop:
space = 2 * (n - i);
std::cout << std::string(space, ' ');
Another way of skipping the loop is to #include <iomanip> and use std::setw.
Note that you can use std::setw and std::left to correct the while (star) loop too to make this pattern hold for up to n = 13.
space = 2 * (n - i) + 1;
std::cout << std::setw(space) << "";
while (star) {
std::cout << std::setw(2) << std::left << j;
j++;
star--;
}
Demo

How is buffer processing causing different behaviors?

The context
I have written a program which takes two coordinates as input and modifies an ASCII world map based on where those coordinates land (land or water):
#include <stdio.h>
#define ROW 22
#define COL 73
#define EMPTY ' '
#define FILLED '.'
#define LAND '#'
#define WATER '~'
void initialize(char map[][COL]);
void show(char map[][COL]);
void colorArea(char map[][COL], int, int, char, char);
int main(void) {
char map[ROW][COL];
char type;
int r, c;
initialize(map);
show(map);
while(1) {
do {
printf("\nMax = (%d, %d). Per uscire inserire (0, 0).\n"
"Inserire le coordinate e premere invio: ", COL, ROW);
scanf("%d%d", &c, &r);
} while (r > ROW || c > COL);
if (r < 1 || c < 1) {
printf("Arrivederci!\n");
return 0;
}
for (int i = 0; i < 100; i++)
putchar('\n'); // Clear the screen.
type = map[--r][--c]; // Could be WATER, LAND, FILLED or EMPTY.
if (type == EMPTY)
colorArea(map, r, c, type, WATER);
else if (type == FILLED)
colorArea(map, r, c, type, LAND);
else if (type == LAND)
colorArea(map, r, c, type, FILLED);
else if (type == WATER)
colorArea(map, r, c, type, EMPTY);
map[r][c] = 'X'; // Mark the point on the map.
show(map);
}
return 0;
}
void initialize(char map[][COL]) {
char *s[ROW];
int r, c;
s[0] = " .. ";
s[1] = " ... .. ........ .. ";
s[2] = " ............... .. ....... ............... ";
s[3] = " ................. ... .... .. ...................... ";
s[4] = " .. ................... . .. ........................... ";
s[5] = " ................ .. ............................ ";
s[6] = " ............. ........ ........................... ";
s[7] = " ...... . .................................... . ";
s[8] = " ... ... . .......................... .. ";
s[9] = " ... . ..... ...................... . ";
s[10] = " .. .................. ............ ";
s[11] = " ..... ........... . .... ... ";
s[12] = " ......... ........ .. ... ";
s[13] = " ........... ...... . ";
s[14] = " . ......... ..... . .. ";
s[15] = " ....... .... . ........ ";
s[16] = " ...... .. ............ ";
s[17] = " .... .... ..... . ";
s[18] = " .. .. .. ";
s[19] = " . . ";
s[20] = " ";
s[21] = " ";
for (r = 0; r < ROW; r++)
for (c = 0; c < COL; c++)
map[r][c] = s[r][c];
}
// Print the map.
void show(char map[][COL]) {
int r, c;
printf("\n /");
for (c = 0; c < COL; c++)
printf("–");
printf("\\\n");
for (r = 0; r < ROW; r++) {
printf(" |");
for (c = 0; c < COL; c++)
printf("%c", map[r][c]);
printf("|\n");
}
printf(" \\");
for (c = 0; c < COL; c++)
printf("–");
printf("/\n");
}
void colorArea(char map[][COL], int r, int c, char type, char color) {
if (c > COL || r > ROW)
return;
if (map[r][c] == type) {
map[r][c] = color;
colorArea(map, r+1, c, type, color); // Down.
colorArea(map, r-1, c, type, color); // Up.
colorArea(map, r, c+1, type, color); // Right.
colorArea(map, r, c-1, type, color); // Left.
}
}
The problem(s)
Changing the content of printf alters the behavior of the program. If I remove the spacing characters from the show function (which prints the map one character away from the left margin) it works unexpectedly:
void show(char map[][COL]) {
int r, c;
printf("/");
for (c = 0; c < COL; c++)
printf("-");
printf("\\\n");
for (r = 0; r < ROW; r++) {
printf("|");
for (c = 0; c < COL; c++)
printf("%c", map[r][c]);
printf("|\n");
}
printf("\\");
for (c = 0; c < COL; c++)
printf("-");
printf("/");
}
but if doing so I also translate the first printf in English
printf("\nMax = (%d, %d). Enter (0, 0) to exit.\n"
"Enter the coordinates: ", COL, ROW);
it behaves again as it should. Of course the problem is the language, it's probably due to shortness of the English version, but I can't figure out why. A picture is worth a thousand words, so here it is (the second one is the one with the strange behavior):
Note that this only happens when the coordinates land on water (for example (1, 1)) and the content of printf is displayed again if I hit the water again, which make it even more confusing to me. Furthermore, if I enter (0, 0) to exit the program after the problem occurs, the program does exit but returns 139 (segmentation fault), but I don't get how it's possible since there is a check with a return 0; before calling any function.
What I figured
Buffer behavior is not standardized. Often buffers process one line at a time, and mine works this way (I tested it with another program). Sometimes characters like \n are left in the buffer and cause unwanted behaviors, but it shouldn't be the case here: scanf scans for integers, so it should ignore space characters (unlike when it scans for char).
The question(s)
What's causing the problem? What are the two dots that appear? What is happening to the buffer in each step?
you access out of your array including doing assignment producing undefined behavior, do :
void colorArea(char map[][COL], int r, int c, char type, char color) {
if (c >= COL || r >= ROW || c < 0 || r < 0)
and
void colorEverything(char map[][COL], int r, int c) {
if (c >= COL || r >= ROW || c < 0 || r < 0)
Also in show in printf("-"); the minus is not the simple ASCII character minus but a complex sequence of character probably produced by your editor (under Windows ?)

Can you solve my problem and simplify my code? I could not find out with the clean solution for this specific problem

Problem: To display the sum of this pattern for n terms like 1+11+111+1111+11111..n terms
Test Data:
Input the number of terms: 5.
Expected Output:
1 + 11 + 111 + 1111 + 11111
The Sum is : 12345
I am trying this way->
//To display the sum of series like 1+11+111+11111
#include <stdio.h>
int
main(void){
//Here i declared some variables for storing information
int number,iteration,value=1,j,summation=0;
//Message to user
printf("Input the number of terms : ");
//taking input from the user
scanf("%d",&number);
//this condition will work till the iteration reaches to the inputted number
for(iteration=1; iteration<=number; iteration++){
for(j=1; j<=iteration; j++){
//To display the series like 1 11 111 1111 11111
printf("%d",value);
if(j==1){
summation=summation+value;
}
else if(j==2){
summation=summation+value*10;
}
else if(j==3){
summation=summation+value*100;
}
else if(j==4){
summation=summation+value*1000;
}
else if(j==5){
summation=summation+value*10000;
}
}
printf(" ");
}
printf("\n");
//To display the summation
printf("The summation is : %d",summation);
return 0;}
Now my problem is: This code does not work according to my expectation. It is working up to input value 5. But when I want to give input 6 times then I need to add an else if condition additionally in my code. I need to do this task whenever I increase the input value.
When the input value is 6 and i need to add and make the condition like that->
else if(j==6){
summation=summation+value*100000;
}
So I think, this is not the way of a proper solution to a problem. Every time I need to do the same thing for the inputted value. How can I solve this problem?. After that how can I simplify the solution? I believe that you guys are expert than me. Please share your knowledge with me. Thank you in advance.
Pass the input number to this function.
int findSum(int n)
{
int sum=0, cnt= 1;
for (int i = 1; i <= n; i++) {
sum += cnt;
cnt = (cnt * 10) + 1;
}
return sum;
}
If you want to make this work for large N (say, 1,000 or 20,000,000), you won’t be able use int or long long values. Instead, you could allocate an array of uint8s, and do your own digit-by-digit addition arithmetic, including the carry operation. Then print the results at the end. It wouldn’t be fast but it would work.
To keep your code simple, think right-to-left. Start with the least significant digit in the zero-th array element.
Here's an example that uses uint64_t to represent larger numbers. It shows the output you want for 1 up to 20 digits (longer causes an overflow).
The trick is to generate the numbers 1, 11, 111, and so on from the previous one by multiplying by 10 and adding 1. For example, 11111 = 1111 * 10 + 1.
#include <inttypes.h>
#include <stdio.h>
void sum(int n) {
uint64_t t = 0;
uint64_t x = 1;
for (int i = 0; i < n; i++) {
if (i > 0) printf(" + ");
printf("%" PRIu64, x);
t += x;
x = (x * 10) + 1;
}
printf(" = %" PRIu64 "\n", t);
}
int main() {
for (int i = 1; i < 21; i++) {
sum(i);
}
}
Here's a version that works for any n. It computes the total in time linear in n, although printing the terms being summed necessarily requires O(n^2) time.
The code works by noting that the last digit of the total consists of n 1s being added, the next-to last n-1 1s and so on. Plus carry of course. Note that the result is always exactly n digits long.
#include <stdio.h>
#include <stdlib.h>
void sum(int n) {
for (int i = 1; i <= n; i++) {
if (i > 1) printf(" + ");
for(int j = 0; j < i; j++) putchar('1');
}
printf(" = ");
char *s = malloc(n + 1);
s[n] = '\0';
int t = 0;
for (int i = n - 1; i >= 0; i--) {
t += i + 1;
s[i] = '0' + (t % 10);
t /= 10;
}
printf("%s\n", s);
free(s);
}
int main() {
sum(50);
}
Output (wrapped):
1 + 11 + 111 + 1111 + 11111 + 111111 + 1111111 + 11111111 + 111111111 + 1111111111 + 11111111111 + 111111111111 +
1111111111111 + 11111111111111 + 111111111111111 + 1111111111111111 + 11111111111111111 + 1111111111111111 11 +
1111111111111111111 + 11111111111111111111 + 111111111111111111111 + 1111111111111111111111 + 11111111111111111111111 +
111111111111111111111111 + 1111111111111111111111111 + 11111111111111111111111111 + 11111111111 1111111111111111 +
1111111111111111111111111111 + 11111111111111111111111111111 + 111111111111111111111111111111 +
1111111111111111111111111111111 + 11111111111111111111111111111111 + 111111111111111111111111111111111 +
1111111111111111111111111111111111 + 11111111111111111111111111111111111 + 111111111111111111111111111111111111 +
1111111111111111111111111111111111111 + 11111111111111111111111111111111111111 + 1111111111111111111111111
11111111111111 + 1111111111111111111111111111111111111111 + 11111111111111111111111111111111111111111 +
111111111111111111111111111111111111111111 + 1111111111111111111111111111111111111111111 + 1111111111111111111111111
1111111111111111111 + 111111111111111111111111111111111111111111111 + 1111111111111111111111111111111111111111111111 +
11111111111111111111111111111111111111111111111 + 111111111111111111111111111111111111111111111111 +
1111111111111111111111111111111111111111111111111 + 11111111111111111111111111111111111111111111111111 =
12345679012345679012345679012345679012345679012340
For handling numbers greater than int/long limits, you can use an array to get the sums per digit and print the output as a string.
#include <stdio.h>
int
main (int argc, char *argv[])
{
int n, i, j;
scanf("%d", &n);
char ones[n];
char sum[n + 1]; // + 1 index in case of a carry out
char output[n + 2]; // +1 more index than sum for null byte
// initialize to 0s
for (i = 0; i < n; i++) {
ones[i] = sum[i] = output[i] = 0;
}
sum[n] = output[n] = output[n+1] = 0;
for (i = 0; i < n; i++) {
ones[i] = 1;
output[i] = '1';
for (j = 0; j <= i; j++) { // add the current number of ones to sum
sum[j] += ones[j];
if (sum[j] >= 10) { // if theres a carry
sum[j + 1] += (sum[j] / 10); // add the carry to the next index
sum[j] %= 10; // keep the last digit
}
}
if (i == n - 1) {
printf ("%s ", output);
} else printf ("%s + ", output);
}
if(sum[n] == 0) {// leading digit is 0
i = n - 1;
} else i = n;
for (j = 0; i >= 0; i--, j++) {
output[j] = sum[i] + '0';
}
printf ("The sum is: %s\n", output);
return 0;
}
Given your user input variable number, the code may look something like this:
//...
if (number < 0)
{
// do some error handling
return -1;
}
int value_to_add = 1;
int sum = 0;
while (number--)
{
sum += value_to_add;
value_to_add = value_to_add * 10 + 1;
}
// ... (result is in "sum")
You also may consider the possibility of overflow (when the result gets so big that it does not fit in an int). You could, for instance, limit the user input (number).
glad to help!
(it seems like a homework, so hope you can learn something)
You're doing this with many 'if's to decide how much it should plus. And another way is to use *10+1 every time.
Please see the code:
#include <stdio.h>
long long sum,tmp=1,n;
int main(void){
scanf("%lld",&n);
for(int i=0;i<n;i++){
if(i<n-1)printf("%lld + ",tmp);
else printf("%lld ",tmp);
sum+=tmp;
tmp=tmp*10+1;
}
printf("= %lld",sum);
return 0;
}
That's it.
Wish you a good day:)
If I understood correctly you want to be able to programmatically add new terms without having to use an if statement. To do it I suggest you
for (j=0; j<=iteration; j++){
int powerOf10 = (int) pow((double) 10,j); //power elevation: notice 10^0=1, 10^1=10..
summation+=value*powerOf10;
}
This was just to give you an idea. Obviously, this code can be further refined.
If you don't understand all the casting I performed to compute powerOf10 I leave you this post: Why is my power operator (^) not working?
Paul Hankin's answer shows how to solve this problem for values of n greater than the number of digits storable in a long long.
That approach could be combined with another, based on a simple observation. If we write the sum starting from the greatest number, we can note an emerging pattern.
111111111111111111111111111111111111111111111...111 +
11111111111111111111111111111111111111111111...111 +
...
1111111111111111111111111111111111111...111 =
----------------------------------------------------
123456789999999999999999999999999999999999999...999 +
111111111111111111111111111111111111...111 =
----------------------------------------------------
123456790111111111111111111111111111111111111...110 +
11111111111111111111111111111111111...111 +
...
1111111111111111111111111111...111 =
----------------------------------------------------
123456790123456789999999999999999999999999999...998 +
111111111111111111111111111...111 =
----------------------------------------------------
123456790123456790111111111111111111111111111...109 +
11111111111111111111111111...111 +
...
1111111111111111111...111 =
----------------------------------------------------
123456790123456790123456789999999999999999999...997 +
111111111111111111...111 =
----------------------------------------------------
123456790123456790123456790111111111111111111...108 +
^ ^ ^ ^ ...
In practice, we can start by "filling" the number (represented as a string of n characters) with the repeating pattern "123456790" from left to right (the most significant digit beeing always '1').
Then, starting from the least significant digit, we can apply the algorithm of the sum with carry, but only as long as the calculated digit is different from the one already there (except the last one, which is always n % 10).
Only a few steps are needed, just around the number of decimal digits of n.

Why does not this for-loop work?

Help, please.
This code does't work:
for (i = 0; i == userWhoIsInLineArray.GetNumberOfUsersOnline() - 1; i++) {
Log.d("DATA-----|", "UserName- "
+ userWhoIsInLineArray.GetUserName(i)
+ " UserHref- "
+ userWhoIsInLineArray.GetUserAccountHref(i));
}
When I write this one, all work!
while(i != userWhoIsInLineArray.GetNumberOfUsersOnline() - 1) {
Log.d("DATA-----|", "UserName- "
+ userWhoIsInLineArray.GetUserName(i)
+ " UserHref- "
+ userWhoIsInLineArray.GetUserAccountHref(i));
i++;
}
Why is happening?
I doubt you meant to use equality in your for loop test for continuance?
This bit:
i == userWhoIsInLineArray.GetNumberOfUsersOnline()-1
Perhaps you meant another comparison operator?
You should write as:
for (i = 0; i < userWhoIsInLineArray.GetNumberOfUsersOnline(); i++) {
Log.d("DATA-----|", "UserName- "
+ userWhoIsInLineArray.GetUserName(i)
+ " UserHref- "
+ userWhoIsInLineArray.GetUserAccountHref(i));
}
Only when the second sentence of for-clause is true, for-block is executed. So, when the variable i is smaller than userWhoIsInLineArray.GetNumberOfUsersOnline(), it must be true.
cf 1. i == userWhoIsInLineArray.GetNumberOfUsersOnline() means only when variable i equals userWhoIsInLineArray.GetNumberOfUsersOnline() it is true. Unless userWhoIsInLineArray.GetNumberOfUsersOnline() is 0, it results false for the first loop.
cf 2. In the same sence your while-loop is better to rewrite as:
int i = 0;
while (i < userWhoIsInLineArray.GetNumberOfUsersOnline()) {
Log.d("DATA-----|", "UserName- "
+ userWhoIsInLineArray.GetUserName(i)
+ " UserHref- "
+ userWhoIsInLineArray.GetUserAccountHref(i));
i++;
}

Nested for loops to make a christmas 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)
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;
}
I am not sure this is most efficient way, but you can give it a try.
logic i have used here is:
find the mid line for the tree.
print space till mid line and star at the end.
decrement spaces by one and increment stars by 2
Once a layer is printed decrement 4 stats and increment 2 spaces
Same way print the tree trunk.
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.

Resources