{"id":5681,"date":"2022-12-24T00:01:44","date_gmt":"2022-12-24T08:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5681"},"modified":"2022-12-31T09:13:50","modified_gmt":"2022-12-31T17:13:50","slug":"o-christmas-tree","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5681","title":{"rendered":"O Christmas Tree"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/12\/1224-figure1.png\" alt=\"\" width=\"550\" height=\"343\" class=\"aligncenter size-full wp-image-5682\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/12\/1224-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/12\/1224-figure1-300x187.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/12\/1224-figure1-481x300.png 481w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><br \/>\nIt&#8217;s Christmas time, nerds rejoice! Welcome this festive season with a bit of programming acumen to festoon your old CRT monitor with some yuletide cheer.<br \/>\n<!--more--><br \/>\nMultiple programming methods exist to output the Christmas tree shown above. The simplest way, probably how I would have coded the thing in BASIC back in the 1980s, would be to output each line by itself. A stack of 20 <code>PRINT<\/code> statements generates the tree, adorning the glowing (and irritating) white phosphor on my beloved old TRS-80 Model III.<\/p>\n<p>For the C language, however, I sought to be clever. Ideally I&#8217;d love to use a single statement in a loop to output the Christmas tree. Such would be a thing of beauty.<\/p>\n<p>Alas, the C language lacks a function that outputs a string of the same character. For example, generating a line of asterisks with a given length. Other programming languages have such tools, but in C you must code them on your own. I cover such a function for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5687\">next week&#8217;s Lesson<\/a>, but for now, here&#8217;s my attempt to tightly code Christmas tree output:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_12_24-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_12_24-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x,y,stars;\r\n    const int height = 20;    <span class=\"comments\">\/* 20 rows tall *\/<\/span>\r\n\r\n    for( x=0,stars=1 ; x&lt;height; x++,stars+=2 )\r\n    {\r\n        <span class=\"comments\">\/* print the indent *\/<\/span>\r\n        printf(\"%*c\",height-x,' ');\r\n        for( y=0; y&lt;stars; y++ )\r\n        {\r\n            putchar('*');    \r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 6 sets the tree&#8217;s height at 20 rows, which fits well on a standard 80 column by 24 row text screen.<\/p>\n<p>The <em>for<\/em> loop at Line 8 modifies two conditions. The first is <code>x<\/code>, which is the row count, capped at the value of integer constant <code>height<\/code>. The second is <code>stars<\/code>, which are the asterisks that create the tree. This value is increased by two each iteration of the <em>for<\/em> loop: <code>stars+=2<\/code>. The two extra asterisks account for the tree&#8217;s triangular shape.<\/p>\n<p>Within the loop, a <em>printf()<\/em> statement outputs a given length of spaces. The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2972\">variable width specifier<\/a>, <code>*<\/code>, sets the number of spaces output, which is inversely proportional to the row <code>height<\/code>, minus <code>x<\/code>.<\/p>\n<p>I wanted to use a similar <em>printf()<\/em> placeholder to output the row of asterisks, but here is where the standard C library lacks a function to output a string of repeated characters. I tried using a variable width argument, but it works best only with spaces. Alas.<\/p>\n<p>To generate the Christmas tree branches, I use a nested <em>for<\/em> loop, with the value of stars to create ever increasing evergreen branches.<\/p>\n<p>A final <em>putchar()<\/em> statement spews out the newline, ending each row.<\/p>\n<p>This kind of simple and fun output is often used as a programming problem, especially for an obfuscated C challenge. Multiple ways exist to output the tree. If you feel like exercising your C programming kung fu, consider attempting this challenge. How would you output a simple Christmas tree pattern as shown above?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This trick would have blown them away back in 1977. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5681\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5681","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5681"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5681\/revisions"}],"predecessor-version":[{"id":5704,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5681\/revisions\/5704"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}