{"id":4391,"date":"2020-10-08T00:01:53","date_gmt":"2020-10-08T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4391"},"modified":"2020-10-10T07:49:49","modified_gmt":"2020-10-10T14:49:49","slug":"a-cumulative-total-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4391","title":{"rendered":"A Cumulative Total &#8211; Solution"},"content":{"rendered":"<p>I find it odd that the recursive solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4380\">this month&#8217;s Exercise<\/a> is far shorter than the non-recursive version. Yet, it has an elegance that&#8217;s evident in lots of recursive code examples.<br \/>\n<!--more--><br \/>\nFirst, the non-recursive solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10-Exercise-non-recursive.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10-Exercise-non-recursive.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nunsigned cumulative(unsigned n)\r\n{\r\n    unsigned a,total;\r\n\r\n    total = 0;\r\n    for(a=1; a&lt;=n; a++)\r\n    {\r\n        total=total+a;\r\n    }\r\n\r\n    return(total);\r\n}\r\n\r\nint main()\r\n{\r\n    unsigned v;\r\n\r\n    printf(\"Type a positive integer value: \");\r\n    scanf(\"%u\",&amp;v);\r\n\r\n    printf(\"The total of all numbers 1 through %u is %u\\n\",\r\n            v,\r\n            cumulative(v)\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>cumulative()<\/em> function at Line 3 loops from 0 to <code>n<\/code>, the <em>unsigned<\/em> value passed. As it loops, the value of variable <code>total<\/code> is increased by the value of the looping variable, <code>a<\/code>. This variable is initialized to one in the <em>for<\/em> loop statement at Line 8. I could have started it at zero, but 0+0=0, which wastes a spin of the loop. Further, the maximum value is set to <code>a&lt;=n<\/code>, to ensure the final value (which was passed in variable <code>n<\/code>) is added to the total.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Type a positive integer value: 10<br \/>\nThe total of all numbers 1 through 10 is 55<\/code><\/p>\n<p>The recursive version of the <em>cumulative()<\/em> function, shown below, is far more elegant &mdash; and short:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10-Exercise-recursive.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10-Exercise-recursive.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nunsigned cumulative(unsigned n)\r\n{\r\n    while(n)\r\n        return(n + cumulative(n-1));\r\n    return(n);\r\n}\r\n\r\nint main()\r\n{\r\n    unsigned v;\r\n\r\n    printf(\"Type a positive integer value: \");\r\n    scanf(\"%u\",&amp;v);\r\n\r\n    printf(\"The total of all numbers 1 through %u is %u\\n\",\r\n            v,\r\n            cumulative(v)\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I confess that I stewed over a solution. I knew it could be done, but I over-thought the problem. It was only when I decided to do some right-brain activities that the brilliance of <code>return(n + cumulative(n-1));<\/code> came to me.<\/p>\n<p>At Line 5, a <em>while<\/em> loop spins as long as the value of variable <code>n<\/code> is non-zero (positive). The <em>return<\/em> statement adds the value passed plus one minus the value, which is used to call the <em>cumulative()<\/em> function again. This process recurses until <em>n<\/em> is equal to zero, at which point the entire thing unwinds. A final <em>return<\/em> statement at Line 7 passes the cumulative total back to the caller.<\/p>\n<p>Here&#8217;s sample output from the recursive version of the code:<\/p>\n<p><code>Type a positive integer value: 200<br \/>\nThe total of all numbers 1 through 200 is 20100<\/code><\/p>\n<p>I hope you were successful in solving this month&#8217;s Exercise, especially if you attempted the recursive solution. Providing that the output is correct, consider the challenge completed successfully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I find it odd that the recursive solution for this month&#8217;s Exercise is far shorter than the non-recursive version. Yet, it has an elegance that&#8217;s evident in lots of recursive code examples.<\/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-4391","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\/4391","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=4391"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4391\/revisions"}],"predecessor-version":[{"id":4410,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4391\/revisions\/4410"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}