{"id":4989,"date":"2021-10-01T00:01:06","date_gmt":"2021-10-01T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4989"},"modified":"2021-10-08T10:25:08","modified_gmt":"2021-10-08T17:25:08","slug":"alphabedecimal-revisited","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4989","title":{"rendered":"Alphabedecimal Revisited"},"content":{"rendered":"<h2>Difficulty: Medium<\/h2>\n<p>The goal of the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3344\">November 2018 Exercise<\/a> on this blog was to count in &#8220;alphabedecimal,&#8221; or base 26 using the uppercase letters of the alphabet. The solution cycles from AAAA to ZZZZ flipping each character (or digit) one at a time, just as numbers are counted. The challenge is to do so without employing a nested loop.<br \/>\n<!--more--><br \/>\nThe <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3355\">Exercise&#8217;s solution<\/a> used a complex <em>if<\/em> structure to determine when to change digits &mdash; and to avoid the dreaded nested loop. I provided two copies of the solution. The second one is shown here:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_10_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_10_01-Lesson.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#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 the <em>main()<\/em> function, each <em>if<\/em> statement in the cascading structure examines a different character in the <code>digits[]<\/code> string. The <em>inc()<\/em> function is called to increment a character at a specific position. If the character is greater than <code>'Z'<\/code>, it&#8217;s reset to <code>'A'<\/code> with the next character in the array incremented. This solution works, but it&#8217;s clunky.<\/p>\n<p>Your task for this month&#8217;s Exercise is to recreate the program&#8217;s output, generating alphabedcimal values from AAAA through ZZZZ. Unlike the solution from three years ago, you cannot use a cascading <em>if<\/em> structure &#038;mdash or a nested loop. While your solution can use the same philosophy that&#8217;s employed by the <em>inc()<\/em> function (shown above), though the the key is to use recursion to perform this task. This recursive function avoids the nested <em>if<\/em> structure and clumsy way it references the digits in the array.<\/p>\n<p>Please try this Exercise on your own, devising a recursive function to handle the character\/digit flipping. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4995\">Click here<\/a> to view my solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once again, the challenge is to count from AAAA to ZZZZ &mdash; but without using nested loops or a cascading <em>if<\/em> structure. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4989\">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":[3],"tags":[],"class_list":["post-4989","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4989","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=4989"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4989\/revisions"}],"predecessor-version":[{"id":5018,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4989\/revisions\/5018"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}