{"id":5509,"date":"2022-09-08T00:01:29","date_gmt":"2022-09-08T07:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5509"},"modified":"2022-09-03T11:20:09","modified_gmt":"2022-09-03T18:20:09","slug":"cyclops-numbers-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5509","title":{"rendered":"Cyclops Numbers &#8211; Solution"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/09-exercise1-cyclops.png\" alt=\"\" width=\"150\" height=\"174\" class=\"alignleft size-full wp-image-5500\" \/>I hope <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5499\">this month&#8217;s Exercise<\/a> didn&#8217;t terrify you. I remember seeing <a href=\"https:\/\/www.youtube.com\/watch?v=a77pycC78Q0\" rel=\"noopener\" target=\"_blank\"><em> The Seventh Voyage of Sinbad <\/em><\/a> when I was a kid. The cyclops bothered me so much I asked my mom questions about him for a month.<br \/>\n<!--more--><br \/>\nA cyclops number is neither 10 meters tall nor impervious to javelins. It is, however, easy to catch when you do as I did and use the <em>sprintf()<\/em> function to convert the value into a string. Afterwards, you scan the string for the presence of a zero digit and then determine whether that single zero dwells in the center of the odd-character string.<\/p>\n<p>For my solution, I crafted the <em>zero_count()<\/em> function. It consumes a string and returns the number of zeros. This function helped reduce my solution&#8217;s main loop to a handful of <em>if<\/em> tests that pluck out a cyclopean value.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_09-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_09-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n<span class=\"comments\">\/* count the zero '0' characters in the string *\/<\/span>\r\nint zero_count(char *v)\r\n{\r\n    int a = 0;\r\n\r\n    <span class=\"comments\">\/*\r\n       - crawl through the string\r\n       - okay to modify v here\r\n    *\/<\/span>\r\n    while( *v )\r\n    {\r\n        if( *v == '0' )\r\n            a++;        <span class=\"comments\">\/* count '0' character *\/<\/span>\r\n        v++;            <span class=\"comments\">\/* increment pointer *\/<\/span>\r\n    }\r\n\r\n    return(a);\r\n}\r\n\r\nint main()\r\n{\r\n    const char digits = 6;        <span class=\"comments\">\/* storage for up to 5 digits *\/<\/span>\r\n    const int total = 100;        <span class=\"comments\">\/* total numbers to find *\/<\/span>\r\n    const int columns = 9;        <span class=\"comments\">\/* number of columns for output table *\/<\/span>\r\n    int cyclops,count,len,mid,x,y;\r\n    char buffer[digits];\r\n    int results[total];            <span class=\"comments\">\/* store the results for fancy output *\/<\/span>\r\n\r\n    printf(\"The first %d cyclops numbers\\n\",total);\r\n    cyclops = count = 0;\r\n    while( count&lt;total )\r\n    {\r\n        sprintf(buffer,\"%d\",cyclops);\r\n        len = strlen(buffer);\r\n        <span class=\"comments\">\/* process strings greater than two characters\r\n           and only odd-length strings *\/<\/span>\r\n        if( len&gt;2 &amp;&amp; len%2 )\r\n        {\r\n            <span class=\"comments\">\/* must have only one zero *\/<\/span>\r\n            if( zero_count(buffer)==1 )\r\n            {\r\n                mid = len\/2;            <span class=\"comments\">\/* middle character offset *\/<\/span>\r\n                if(buffer[mid]=='0' )    <span class=\"comments\">\/* is zero? *\/<\/span>\r\n                {\r\n                    <span class=\"comments\">\/* save the value and update the count *\/<\/span>\r\n                    results[count] = cyclops;\r\n                    count++;\r\n                }\r\n            }\r\n        }\r\n\r\n        <span class=\"comments\">\/* next potential cyclops value *\/<\/span>\r\n        cyclops++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the results in a fancy table *\/<\/span>\r\n    x = 0;\r\n    while( count )\r\n    {\r\n        for( y=0; y&lt;columns; y++ )\r\n        {\r\n            printf(\"%5d\",results[(x*columns)+y]);\r\n            count--;                <span class=\"comments\">\/* decrement the count *\/<\/span>\r\n            if( !count )            <span class=\"comments\">\/* if count is zero, stop *\/<\/span>\r\n                break;\r\n            if( y&lt;columns-1)\r\n                putchar('\\t');\r\n        }\r\n        x++;\r\n        putchar('\\n');\r\n    }\r\n\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>zero_count()<\/em> function plows through string <code>v<\/code> looking for zero digit characters. It counts the total and returns this value.<\/p>\n<p>In the <em>main()<\/em> function, a <em>while<\/em> loop uses variable <code>count<\/code> to tally the first 100 cyclops values. Three <em>if<\/em> tests cull the results as variable <code>cyclops<\/code> increments from zero.<\/p>\n<p>The first <em>if<\/em> test ensures that the string length (<code>len<\/code>) value has at least three digits and an odd length: <code>if( len>2 &amp;&amp; len%2 )<\/code><\/p>\n<p>The second <em>if<\/em> test confirms that the value has only one zero:<\/p>\n<p><code>if( zero_count(buffer)==1 )<\/code><\/p>\n<p>The final <em>if<\/em> test uses variable <code>mid<\/code> to seek out the string&#8217;s center digit: <code>mid = len\/2;<\/code> As these variables are integers, the result is rounded. Then the final <em>if<\/em> test checks the center digit: <code>if(buffer[mid]=='0' )<\/code> Upon success, the value is stored in the <code>results[]<\/code> array, and variable <code>count<\/code> is incremented.<\/p>\n<p>The final <em>while<\/em> loop generates a multi-column table displaying the results. You can read details on how this loop works in a future Lesson.<\/p>\n<p>I hope your solution met with success. From the earlier <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5485\">post on counting digits<\/a>, obviously logarithms and division can also be used to determine the number of digits in a value. If you used one of these approaches in your solution, all the better!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope this month&#8217;s Exercise didn&#8217;t terrify you. I remember seeing The Seventh Voyage of Sinbad when I was a kid. The cyclops bothered me so much I asked my mom questions about him for a month.<\/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-5509","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\/5509","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=5509"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5509\/revisions"}],"predecessor-version":[{"id":5523,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5509\/revisions\/5523"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}