{"id":1674,"date":"2015-12-08T00:01:01","date_gmt":"2015-12-08T08:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1674"},"modified":"2015-12-05T08:47:58","modified_gmt":"2015-12-05T16:47:58","slug":"merry-cumulative-song-exercise-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1674","title":{"rendered":"Merry Cumulative Song Exercise &#8211; Solution"},"content":{"rendered":"<p>I crafted my solution to the cumulative song exercise by separating the parts of the song that are repeated from the unique text. Specifically, the ordinal days are unique as well as the gifts. The rest of the text is sung over and over.<br \/>\n<!--more--><br \/>\nTwo arrays hold the repeating text, <code>day[]<\/code> for the days of Christmas and <code>gift[]<\/code> for the specific items.<\/p>\n<p>The repeating text is <em>On the _____ day of Christmas, my true love gave to me<\/em>. That&#8217;s followed by each subsequent gift. An exception is the first day, where the line is <em>A partridge in a pear tree<\/em>. On subsequent days the line is <em><strong>And<\/strong> a partridge in a pear tree<\/em>.<\/p>\n<p>Here&#8217;s my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *day[12] = { \"first\", \"second\", \"third\", \"fourth\",\r\n        \"fifth\", \"sixth\", \"seventh\", \"eighth\", \"ninth\",\r\n        \"tenth\", \"eleventh\", \"twelfth\" };\r\n    char *gift[12] = {\r\n        \"And a partridge in a pear tree.\",\r\n        \"Two turtle doves,\",\r\n        \"Three French hens,\",\r\n        \"Four calling birds,\",\r\n        \"Five gold rings!\",\r\n        \"Six geese a-laying,\",\r\n        \"Seven swans a-swimming,\",\r\n        \"Eight maids a-milking,\",\r\n        \"Nine ladies dancing,\",\r\n        \"Ten lords a-leaping,\",\r\n        \"Eleven pipers piping,\",\r\n        \"Twelve drummers drumming,\" };\r\n    int d,g;\r\n\r\n    for(d=0;d&lt;12;d++)\r\n    {\r\n        printf(\"On the %s day of Christmas, my true love gave to me\\n\",\r\n                day[d]);\r\n        if( d == 0 )\r\n        {\r\n            printf(\"\\tA partridge in a pear tree.\\n\\n\");\r\n        }\r\n        else\r\n        {\r\n            for(g=d;g&gt;0;g--)\r\n                printf(\"\\t%s\\n\",gift[g]);\r\n            printf(\"\\t%s\\n\\n\",gift[g]);\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Two loops manage the lyrics. The outer <em>for<\/em> loop at Line 23 counts up the 12 days of Christmas.<\/p>\n<blockquote><p>The 12 days are the days <em>after<\/em> Christmas, leading up to Twelfth Night (as in the Shakespeare play), the evening before the religious holiday <a href=\"https:\/\/en.wikipedia.org\/wiki\/Epiphany_(holiday)\" target=\"_blank\">Epiphany<\/a>.<\/p><\/blockquote>\n<p>For the first day, when variable <code>d<\/code> is equal to zero (Line 27), the special text at Line 29 is displayed. Otherwise, the second, inner <em>for<\/em> loop works to rattle off the various gifts. That loop counts down from the value of variable <code>d<\/code> to zero.<\/p>\n<p>Other solutions are possible, of course. Some C programmers get really clever and fish out all the repetitive words, tangling what&#8217;s left into a Gordian knot of unreadable text. The code then fishes out the words in the proper order in some amazing and incomprehensible fashion. I applaud you if you can craft such a solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I crafted my solution to the cumulative song exercise by separating the parts of the song that are repeated from the unique text. Specifically, the ordinal days are unique as well as the gifts. The rest of the text is &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1674\">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":[5],"tags":[],"class_list":["post-1674","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1674","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=1674"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1674\/revisions"}],"predecessor-version":[{"id":1684,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1674\/revisions\/1684"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}