Tuesday, April 16, 2024
HomeC ProgrammingO Christmas Tree | C For Dummies Weblog

O Christmas Tree | C For Dummies Weblog



It’s Christmas time, nerds rejoice! Welcome this festive season with a little bit of programming acumen to festoon your previous CRT monitor with some yuletide cheer.

A number of programming strategies exist to output the Christmas tree proven above. The best means, most likely how I might have coded the factor in BASIC again within the Eighties, can be to output every line by itself. A stack of 20 PRINT statements generates the tree, adorning the glowing (and aggravating) white phosphor on my beloved previous TRS-80 Mannequin III.

For the C language, nonetheless, I sought to be intelligent. Ideally I’d love to make use of a single assertion in a loop to output the Christmas tree. Such can be a factor of magnificence.

Alas, the C language lacks a perform that outputs a string of the identical character. For instance, producing a line of asterisks with a given size. Different programming languages have such instruments, however in C you will need to code them by yourself. I cowl such a perform for subsequent week’s Lesson, however for now, right here’s my try to tightly code Christmas tree output:

2022_12_24-Lesson.c


#embody <stdio.h>

int most important()
{
    int x,y,stars;
    const int peak = 20;    

    for( x=0,stars=1 ; x<peak; x++,stars+=2 )
    {
        
        printf("%*c",height-x,' ');
        for( y=0; y<stars; y++ )
        {
            putchar('*');    
        }
        putchar('n');
    }

    return(0);
}

Line 6 units the tree’s peak at 20 rows, which inserts nicely on a normal 80 column by 24 row textual content display.

The for loop at Line 8 modifies two circumstances. The primary is x, which is the row rely, capped on the worth of integer fixed peak. The second is stars, that are the asterisks that create the tree. This worth is elevated by two every iteration of the for loop: stars+=2. The 2 additional asterisks account for the tree’s triangular form.

Inside the loop, a printf() assertion outputs a given size of areas. The variable width specifier, *, units the variety of areas output, which is inversely proportional to the row peak, minus x.

I needed to make use of an analogous printf() placeholder to output the row of asterisks, however right here is the place the usual C library lacks a perform to output a string of repeated characters. I attempted utilizing a variable width argument, however it works greatest solely with areas. Alas.

To generate the Christmas tree branches, I exploit a nested for loop, with the worth of stars to create ever rising evergreen branches.

A last putchar() assertion spews out the newline, ending every row.

This sort of easy and enjoyable output is usually used as a programming drawback, particularly for an obfuscated C problem. A number of methods exist to output the tree. For those who really feel like exercising your C programming kung fu, take into account making an attempt this problem. How would you output a easy Christmas tree sample as proven above?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments