{"id":3355,"date":"2018-11-08T00:01:34","date_gmt":"2018-11-08T08:01:34","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3355"},"modified":"2018-11-04T07:54:19","modified_gmt":"2018-11-04T15:54:19","slug":"counting-in-alphabedecimal-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3355","title":{"rendered":"Counting in Alphabedecimal &#8211; Solution"},"content":{"rendered":"<p>Yes, it&#8217;s possible to code a program that counts in alphabedecimal from AAAA to ZZZZ without using nested loops. You must use a single loop, of course, but no cheating as shown in the original <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3344\">Exercise post<\/a>.<br \/>\n<!--more--><br \/>\nThe key is to use nested <em>if<\/em> statements instead of nested loops. Yes, something must be nested!<\/p>\n<p>One loop is required, of course, because the program counts from AAAA to ZZZZ. I don&#8217;t want you to use <em>goto<\/em> statements, so for my solution I used a <em>while<\/em> loop that tests the <em>strcmp()<\/em> function. This function returns zero only when strings match:<\/p>\n<p><code>while(strcmp(digits,\"ZZZZ\"))<\/code><\/p>\n<p>Character array <code>digits<\/code> is initialized to hold the original string, <code>\"AAAA\"<\/code>, which is incremented within the loop. As long as the value of <code>digits<\/code> doesn&#8217;t compare equally to string <code>\"ZZZZ\"<\/code>, the loop spins.<\/p>\n<p>To increment the loop, I compare each element in the <code>digits<\/code> array with the character <code>'Z'<\/code>. If after incrementing the character, <code>'Z'<\/code> is encountered, a nested <em>if<\/em> test repeats the same process for the next character in the array:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char digits[] = \"AAAA\";\r\n\r\n    while(strcmp(digits,\"ZZZZ\"))\r\n    {\r\n        puts(digits);\r\n        digits[3]++;\r\n        if( digits[3] &gt; 'Z' )\r\n        {\r\n            digits[3] = 'A';\r\n            digits[2]++;\r\n            if( digits[2] &gt; 'Z' )\r\n            {\r\n                digits[2] = 'A';\r\n                digits[1]++;\r\n                if( digits[1] &gt; 'Z' )\r\n                {\r\n                    digits[1] = 'A';\r\n                    digits[0]++;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    puts(digits);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A <em>puts()<\/em> statement is added at Line 28 to output the final string, <code>\"ZZZZ\"<\/code>. That&#8217;s because the loop stops when the final string is built, so the <em>puts()<\/em> statement at Line 10 doesn&#8217;t send it to output.<\/p>\n<p>I wasn&#8217;t really happy with this solution because it looks kind of clunky. I see items in the code that repeat, so I tried again with a function, <em>inc()<\/em>, to do the heavy-lifting:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint inc(char *a)\r\n{\r\n    *a += 1;\r\n    if( *a &gt; 'Z' )\r\n    {\r\n        *a = 'A';\r\n        return(TRUE);\r\n    }\r\n    return(FALSE);\r\n}\r\n\r\nint main()\r\n{\r\n    char digits[] = \"AAAA\";\r\n\r\n    while(strcmp(digits,\"ZZZZ\"))\r\n    {\r\n        puts(digits);\r\n        if( inc(&amp;digits[3]) )\r\n            if( inc(&amp;digits[2]) )\r\n                if( inc(&amp;digits[1]) )\r\n                    inc(&amp;digits[0]);\r\n    }\r\n    puts(digits);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this rendition of the code, the <em>main()<\/em> function looks far more elegant.<\/p>\n<p>I tried to reduce the <em>inc()<\/em> function, but my efforts resulted in a less-elegant version of the <em>main()<\/em> function. I also attempted to craft a recursive version of the solution, but my brain imploded.<\/p>\n<p>If you solution resulted in the proper output without using a nested loop, great!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes, it&#8217;s possible to code a program that counts in alphabedecimal from AAAA to ZZZZ without using nested loops. You must use a single loop, of course, but no cheating as shown in the original Exercise post.<\/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-3355","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\/3355","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=3355"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3355\/revisions"}],"predecessor-version":[{"id":3362,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3355\/revisions\/3362"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}