{"id":4380,"date":"2020-10-01T00:01:49","date_gmt":"2020-10-01T07:01:49","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4380"},"modified":"2020-10-09T08:18:05","modified_gmt":"2020-10-09T15:18:05","slug":"a-cumulative-total","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4380","title":{"rendered":"A Cumulative Total"},"content":{"rendered":"<p>I remember the first time I heard the puzzle: If you&#8217;re paid a penny on the first day, doubled on the second day, and doubled every day after, by the end of the month you&#8217;d be a millionaire many times over. This concept is mind boggling.<br \/>\n<!--more--><br \/>\nHere&#8217;s code I wrote that demonstrates the puzzle:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10_01-Example.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10_01-Example.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int days,d;\r\n    float total;\r\n\r\n    printf(\"Number of days in the month: \");\r\n    scanf(\"%d\",&amp;days);\r\n\r\n    total = 0.01;\r\n    for(d=1;d&lt;days;d++)\r\n    {\r\n        total *= 2;\r\n    }\r\n    printf(\"At the end of %d days, you have $%.2f\\n\",\r\n            days,\r\n            total\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This month has 31 days, so here&#8217;s the sample run:<\/p>\n<p><code>Number of days in the month: 31<br \/>\nAt the end of 31 days, you have $10737418.00<\/code><\/p>\n<p>Mathematicians and others with large brains explain that the value obtained is calculated as 2<sup><em>n<\/em>-1<\/sup> where <em>n<\/em> is the number of days in the month.<\/p>\n<p>Another fun, cumulative math thing is the factorial. This value is calculated as the product of a whole number and all smaller integers down to 1. So 5! (read as &#8220;five factorial&#8221;) is 5&times;4&times;3&times;2&times;1 = 120. Calculating a factorial is also possible in C; refer to my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1060\">2014 series<\/a> on this topic, which also covers recursion.<\/p>\n<p>One other cumulative math thingie, without a clever name I&#8217;m aware of, is to add the numbers. Yes, it&#8217;s far more boring than the penny-a-day puzzle or factorials, but it&#8217;s something you can program in C: Take a value, such as 200. If you were to add all the integers from 1 to 200, what would be the result?<\/p>\n<p>Don&#8217;t bother doing the math in your head! Code the puzzle in C: Your task is to write a program with the <em>cumulative()<\/em> function. Prompt the user for positive integer input. Call the <em>cumulative()<\/em> function to return the cumulative total of the value input, <em>n<\/em> + <em>n<\/em>-1 + <em>n<\/em>-2 + <em>n<\/em>-3 all the way down to one.<\/p>\n<p>Further, to make the program more interesting, make the <em>cumulative()<\/em> function recursive to counterbalance the boring aspect of this Exercise.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4391\">Click here<\/a> to view my solution, though please try this Exercise on your own before checking out what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many math puzzles explore doubling values. This time, I&#8217;m going to be boring and just add them. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4380\">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":[3],"tags":[],"class_list":["post-4380","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4380","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=4380"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4380\/revisions"}],"predecessor-version":[{"id":4408,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4380\/revisions\/4408"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}