{"id":1997,"date":"2016-07-08T00:01:41","date_gmt":"2016-07-08T07:01:41","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1997"},"modified":"2016-07-02T09:20:23","modified_gmt":"2016-07-02T16:20:23","slug":"tally-the-digits-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1997","title":{"rendered":"Tally the Digits &#8211; Solution"},"content":{"rendered":"<p>To solve <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1992\">this month&#8217;s Exercise<\/a>, you must create a function that tallies digits in an integer value, what&#8217;s known as a <em>digit sum<\/em>. The key issue is how to peel off individual values in an integer and then total the result.<br \/>\n<!--more--><br \/>\nI can think of two ways to process digits in a value, though more methods are possible.<\/p>\n<p>The first way is to use the modulus operator in the equation <code><em>n<\/em> % 10<\/code>, where <em>n<\/em> is any integer value. The result of that operation is the final digit in the value. So:<\/p>\n<p><code>123456789 % 10 = 9<\/code><\/p>\n<p>And:<\/p>\n<p><code>12345678 % 10 = 8<\/code><\/p>\n<p>And so on.<\/p>\n<p>To get from 123456789 to 12345678 you divide by 10. For a real number, you&#8217;d get:<\/p>\n<p><code>123456789 \/ 10 = 12345678.9<\/code><\/p>\n<p>But when working with integers, you get:<\/p>\n<p><code>123456789 \/ 10 = 12345678<\/code><\/p>\n<p>So my first solution for the <em>digit_sum()<\/em> function is:<\/p>\n<pre class=\"screen\">\r\nint digit_sum(unsigned n)\r\n{\r\n    int total = 0;\r\n\r\n    while(n)\r\n    {\r\n        total += n % 10;\r\n        n \/= 10;\r\n    }\r\n\r\n    <span class=\"comments\">\/* if the value is over two-digits wide, recurse *\/<\/span>\r\n    if(total > 9)\r\n        return(digit_sum(total));\r\n\r\n    return(total);\r\n}<\/pre>\n<p>The <em>while<\/em> loop peels through each digit in integer value <code>n<\/code>. The modulus operator obtains the left-most digit, which is added to the sum in <code>total<\/code>. Then variable <code>n<\/code> is divided by 10 and the <em>while<\/em> loop repeats.<\/p>\n<p>Once all the digits are peeled away, an <em>if<\/em> statement tests to see whether the result is greater than 9, which means it&#8217;s more than a single-digit wide. If so, the function is recursively called in the <em>return<\/em> statement. Otherwise, the total is returned. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/06\/07exercise-a.c\">Click here<\/a> to view the full solution.<\/p>\n<p>My second solution was actually the first one I thought of: Convert the integer value into a string, then read the digits from the string, converting each one into a value. Here is the version of the <em>digit_sum()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nint digit_sum(unsigned n)\r\n{\r\n    char digits[32];\r\n    char *d;\r\n    int total;\r\n\r\n    <span class=\"comments\">\/* convert value to string *\/<\/span>\r\n    snprintf(digits,32,\"%u\",n);\r\n\r\n    <span class=\"comments\">\/* initialize variables *\/<\/span>\r\n    d = digits;\r\n    total = 0;\r\n\r\n    <span class=\"comments\">\/* tally all the individual digits *\/<\/span>\r\n    while(*d)\r\n    {\r\n            <span class=\"comments\">\/* subtract '0' from the character to\r\n               retrieve its value *\/<\/span>\r\n        total += *d - '0';\r\n        d++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* if the value is over two-digits wide, recurse *\/<\/span>\r\n    if(total > 9)\r\n        return(digit_sum(total));\r\n\r\n    return(total);\r\n}<\/pre>\n<p>As you can see, this solution is more complex, but it still works: The <em>snprintf()<\/em> statement converts an integer value into a string. The argument 32 ensures that the string generated fits into the <code>digits[]<\/code> buffer without any overflow. (I&#8217;m assuming that an <em>unsigned int<\/em> value is 32 digits wide.)<\/p>\n<p>Once the value is in a string, the <em>while<\/em> loop processes each character, left-to-right. (Order doesn&#8217;t matter.) The statement <code>total += *d - '0'<\/code> converts each digit character into a value, which is added to the <code>total<\/code> variable. Yes, this solution involves pointers, which is scary, but you could do another version that doesn&#8217;t use pointers.<\/p>\n<p>As with my first solution, the value of <code>total<\/code> is compared with 9. If it&#8217;s greater, then the <em>digit_sum()<\/em> function is recursively called in the <em>return<\/em> statement.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/06\/07exercise-b.c\">Click here<\/a> to view the full solution.<\/p>\n<p>I&#8217;m certain that other solutions are possible, even if they&#8217;re just variations on my two solutions. As long as the <em>digit_sum()<\/em> function returns the proper value, consider your solution good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To solve this month&#8217;s Exercise, you must create a function that tallies digits in an integer value, what&#8217;s known as a digit sum. The key issue is how to peel off individual values in an integer and then total the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1997\">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-1997","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\/1997","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=1997"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1997\/revisions"}],"predecessor-version":[{"id":2017,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1997\/revisions\/2017"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}