{"id":1685,"date":"2015-12-19T00:01:45","date_gmt":"2015-12-19T08:01:45","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1685"},"modified":"2015-12-12T09:12:50","modified_gmt":"2015-12-12T17:12:50","slug":"99-bottles-of-beer","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1685","title":{"rendered":"99 Bottles of Beer"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1653\">Exercise<\/a> involves coding the lyrics for a cumulative song. Perhaps the most famous, and certainly the most obnoxious, cumulative song is the old warhorse, <em>99 Bottles of Beer<\/em>.<br \/>\n<!--more--><br \/>\nI don&#8217;t know anyone who sang the song all the way through, from 99 bottles down to the last one. And I remember singing the song most often in elementary school, where the notion of drinking beer was particularly naughty.<\/p>\n<p>The cumulative song is easy to code. Here&#8217;s what I slapped together as a first stab:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int b = 99;\r\n\r\n    while(b)\r\n    {\r\n        printf(\"%d bottles of beer on the wall,\\n\",b);\r\n        printf(\"%d bottles of beer!\\n\",b);\r\n        printf(\"You take one down, pass it around,\\n\");\r\n        b--;\r\n        printf(\"%d bottles of beer on the wall!\\n\\n\",b);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I won&#8217;t bother displaying the entire output, but here is the final stanza:<\/p>\n<pre><code>1 bottles of beer on the wall,\r\n1 bottles of beer!\r\nYou take one down, pass it around,\r\n0 bottles of beer on the wall!<\/code><\/pre>\n<p>The last verse of the song presents a special case. Specifically, in human languages &#8220;1 bottles&#8221; is improper. It should be &#8220;1 bottle.&#8221; Not only that, the final line &mdash; if you ever bother <em>really<\/em> singing the entire thing &mdash; is <em>You take it down, pass it around, No more bottles of beer!<\/em><\/p>\n<p>To properly code that last stanza, I need to modify my quick-and-dirty code above. I could do that within the <em>while<\/em> loop with an <em>if<\/em>&#8211;<em>else<\/em> structure. Instead, I opted for this solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int b = 99;\r\n\r\n    while(b &gt; 1)\r\n    {\r\n        printf(\"%d bottles of beer on the wall,\\n\",b);\r\n        printf(\"%d bottles of beer!\\n\",b);\r\n        printf(\"You take one down, pass it around,\\n\");\r\n        b--;\r\n        printf(\"%d bottles of beer on the wall!\\n\\n\",b);\r\n    }\r\n    printf(\"%d bottle of beer on the wall,\\n\",b);\r\n    printf(\"%d bottle of beer!\\n\",b);\r\n    printf(\"You take it down, pass it around,\\n\");\r\n    printf(\"No more bottles of beer!\\n\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop counts down from 99 to 2. After it&#8217;s done, the <em>printf()<\/em> statements display the last stanza, which is grammatically proper for the final bottle of beer and last line of the song.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implementing a cumulative song exercise. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1685\">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-1685","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\/1685","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=1685"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1685\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1685\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}