{"id":4062,"date":"2020-04-08T00:01:10","date_gmt":"2020-04-08T07:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4062"},"modified":"2020-04-04T09:16:32","modified_gmt":"2020-04-04T16:16:32","slug":"no-nines-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4062","title":{"rendered":"No Nines &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4057\">this month&#8217;s Exercise<\/a> is to determine whether an integer, 1 to 100, is divisible by nine or contains the digit &#8216;9&#8217;. It seems like an easy puzzle for a C programmer to solve, but what I found interesting was crafting the output &mdash; specifically when a number both contains &#8216;9&#8217; and is divisible by nine.<br \/>\n<!--more--><br \/>\nFor my solution, which you can view in full <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_04-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">on GitHub<\/a>, I used a series of enumerated constants to determine the return value from my <em>no_nine()<\/em> function:<\/p>\n<p><code>enum { LEGAL, DIVIDEDBY, NINE };<\/code><\/p>\n<p>The three constants are <code>LEGAL<\/code> (0) for a legal value; <code>DIVIDEDBY<\/code> (1) for a value evenly-divisible by nine; and <code>NINE<\/code> (2) for a value containing the digit &#8216;9&#8217;.<\/p>\n<p>Here is my <em>no_nine()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nint no_nine(int n)\r\n{\r\n    char number[16];\r\n    int r,t;\r\n\r\n    t = LEGAL;        <span class=\"comments\">\/* it's zero *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* is the number divisible by nine? *\/<\/span>\r\n    if( n % 9 == 0 )\r\n        t += DIVIDEDBY;\r\n\r\n    <span class=\"comments\">\/* convert the number into a string *\/<\/span>\r\n    r = snprintf(number,16,\"%d\",n);\r\n        <span class=\"comments\">\/* test for overflow, though the size 16 is\r\n           beyond the range of a standard int value *\/<\/span>\r\n    if( r &gt; 16 )\r\n    {\r\n        fprintf(stderr,\"Number overflow\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* does the digit 9 appear in the number? *\/<\/span>\r\n    if( strchr(number,'9') != NULL )\r\n        t += NINE;\r\n\r\n    <span class=\"comments\">\/* no nines found *\/<\/span>\r\n    return(t);\r\n}<\/pre>\n<p>The first assumption is that the number passed, <code>n<\/code>, is legal: <code>t = LEGAL<\/code>, which also initializes return variable <code>t<\/code>.<\/p>\n<p>An <em>if<\/em> statement determines whether the value is evenly divisible by 9: <code>if( n % 9 == 0 )<\/code>. If so, the value of variable <code>t<\/code> is increased: <code>t += DIVIDEDBY;<\/code> But the function doesn&#8217;t stop there.<\/p>\n<p>To check for the digit &#8216;9&#8217;, the <em>snprintf()<\/em> function converts <em>int<\/em> variable <code>t<\/code> into a string, <code>number<\/code>. The <em>strchr()<\/em> function then checks for the presence of the digit &#8216;9&#8217; within the string:<\/p>\n<p><code>if( strchr(number,'9') != NULL )<\/code><\/p>\n<p>If a <code>NULL<\/code> is returned from the <em>strchr()<\/em> function, the string lacks a &#8216;9&#8217; digit, so this function checks for a positive return value, in  which case variable <code>t<\/code> is increased: <code>t += NINE;<\/code><\/p>\n<p>When the value of variable <code>t<\/code> is then returned to the <em>main()<\/em> function, its possible values are:<\/p>\n<p><code>LEGAL<\/code> (0), no nines<br \/>\n<code>DIVIDEDBY<\/code> (1), divisible by nine<br \/>\n<code>NINE<\/code> (2), contains the digit &#8216;9&#8217;<br \/>\n<code>DIVIDEDBY+NINE<\/code> (3), both divisible by nine and containing the digit &#8216;9&#8217;<\/p>\n<p>In the <em>main()<\/em> function, the return value from <em>no_nine()<\/em> is compared in a complex <em>if-else<\/em> structure to output the proper string. Here is the code snippet, where <code>x<\/code> is the <em>int<\/em> tested and <code>r<\/code> is the return value from the <em>no_nines()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n    if( r == LEGAL )\r\n        printf(\"%3d is legal\\n\",x);\r\n    else\r\n    {\r\n        printf(\"%3d is illegal - \",x);\r\n        if( r == DIVIDEDBY+NINE )\r\n            printf(\"divisible by 9 and contains '9'\\n\");\r\n        else if ( r == DIVIDEDBY )\r\n            printf(\"divisible by 9\\n\");\r\n        else\r\n            printf(\"contains '9'\\n\");\r\n    }<\/pre>\n<p>For me, the easy part of this Exercise was crafting the <em>no_nines()<\/em> function. Determining whether a value is divisible by 9 is cinchy when you know how to use the <code>%<\/code> operator. The only way I could figure out how to check for the digit &#8216;9&#8217; is to convert the number to a string and then search for it.<\/p>\n<p>In my first draft of the solution, the <em>no_nines()<\/em> function returned <code>TRUE<\/code> or <code>FALSE<\/code>, but then I wanted more detail in the output, so I devised the <em>enum<\/em> constants.<\/p>\n<p>For the <em>main()<\/em> function, I tried to devise a more clever way to generate the &#8220;both&#8221; output string, but settled on the <em>if-else<\/em> contraption as it works. It would be nifty to craft a method to generate the <code>\"divisible by 9 and contains a '9'\"<\/code> string by combining output from the two other &#8220;illegal&#8221; conditions, but my brain was sapped. Perhaps you came up with a better solution?<\/p>\n<p>Keep in mind that any programming solution for this challenge is valid, providing it generates the proper output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to determine whether an integer, 1 to 100, is divisible by nine or contains the digit &#8216;9&#8217;. It seems like an easy puzzle for a C programmer to solve, but what I found &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4062\">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-4062","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\/4062","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=4062"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4062\/revisions"}],"predecessor-version":[{"id":4074,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4062\/revisions\/4074"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}