{"id":4172,"date":"2020-06-06T00:01:14","date_gmt":"2020-06-06T07:01:14","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4172"},"modified":"2020-06-13T09:04:23","modified_gmt":"2020-06-13T16:04:23","slug":"dreaming-of-the-ternary_out-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4172","title":{"rendered":"Dreaming of the <em>ternary_out()<\/em> Function"},"content":{"rendered":"<p>To generate a string of digits representing a value in a specific power base, such as base 3 (ternary), you need a power table. Using this power table, you can translate any positive integer into a string representation of the number in the given base. Sounds complex. Is complex.<br \/>\n<!--more--><br \/>\nIn <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4149\">last week&#8217;s Lesson<\/a>, I presented code that built a powers table for a value. For my ternary insanity, the table contains powers of 3. From this table I can construct a function that outputs a positive integer as a base-3 value, which consists only of digits 0, 1, and 2.<\/p>\n<blockquote><p>Ideally, I think the ternary counting system would show digits 1, 0, and -1 &mdash; or better: +, 0 and -. This base plays well in to three-way evaluations, which was the topic of an <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4027\">older blog post<\/a>. My point with devising a <em>ternary_out()<\/em> function is merely to express a value in base 3.<\/p><\/blockquote>\n<p>Figure 1 illustrates how base 3 counting works for the value 196.<\/p>\n<div id=\"attachment_4180\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4180\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0606-figure1.png\" alt=\"Figure 1\" width=\"390\" height=\"174\" class=\"size-full wp-image-4180\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0606-figure1.png 390w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0606-figure1-300x134.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><p id=\"caption-attachment-4180\" class=\"wp-caption-text\">Figure 1. Base 3 counting.<\/p><\/div>\n<p>The value 196 is expressed as 21021 in base 3. It works like this:<\/p>\n<p><code>(2&times;3<sup>4<\/sup>) + (1&times;3<sup>3<\/sup>) + (0&times;3<sup>2<\/sup>) + (2&times;3<sup>1<\/sup>) + (1&times;3<sup>0<\/sup>)<\/p>\n<p>(2&times;81) + (1&times;27) + (0&times;9) + (2&times;6) + (1&times;1)<\/p>\n<p>162 + 27 + 0 + 6 + 1<\/code><\/p>\n<p>This same type of translation is done for all counting systems. In Figure 2, you see the calculations for value 196 is bases 16, 10, 8, and 2.<\/p>\n<div id=\"attachment_4181\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4181\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0606-figure2.png\" alt=\"Figure 2.\" width=\"390\" height=\"500\" class=\"size-full wp-image-4181\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0606-figure2.png 390w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0606-figure2-234x300.png 234w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><p id=\"caption-attachment-4181\" class=\"wp-caption-text\">Figure 2. Counting systems.<\/p><\/div>\n<p>For base 3, a similar calculation is made. But first, I decided to craft the code as shown below, creating a skeleton of the <em>ternary_out()<\/em> function with error checking for input, the powers table, and a dummy string:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_06_06-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_06_06-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nchar *ternary_out(unsigned n)\r\n{\r\n    static char tstring[11];\r\n    int powers[12];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* check for overflow *\/<\/span>\r\n    if( n&lt;0 || n&gt;65535 )\r\n    {\r\n        fprintf(stderr,\"%d is out of range\\n\",n);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* create the 3 powers table *\/<\/span>\r\n    powers[0] = 1;\r\n    for(x=1;x&lt;12;x++)\r\n    {\r\n        powers[x] = powers[x-1]*3;\r\n    }\r\n\r\n    <span class=\"comments\">\/* build the return string *\/<\/span>\r\n    tstring[0] = '\\0';\r\n\r\n    return(tstring);\r\n}\r\n\r\nint main()\r\n{\r\n    unsigned t;\r\n\r\n    <span class=\"comments\">\/* prompt for input *\/<\/span>\r\n    printf(\"Enter a valued: \");\r\n    scanf(\"%d\",&amp;t);\r\n\r\n    printf(\"%d in ternary is %s\\n\",t,ternary_out(t));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the unimpressive output:<\/p>\n<p><code>Enter a value in ternary: 196<br \/>\n196 in ternary is<\/code><\/p>\n<p>This result is fine because I haven&#8217;t yet coded the guts of the function. This issue presents the tallest hurdle &mdash; especially because I wasn&#8217;t going to rush off to StackOverflow to see how someone else might have done it or how the compiler itself outputs values in hex and octal. No, I had only my brain, a pad of graph paper, and Excel. Yes, I used a spreadsheet to help me refine my algorithm. After all, I prefer the computer to do the math.<\/p>\n<p>Here&#8217;s my thought process:<\/p>\n<p>1. Take a value and determine the largest power divisible by that value.<br \/>\n2. Subtract the power (or multiple thereof) from the value.<br \/>\n3. Repeat the process on the remainder as you descend through the power table.<\/p>\n<p>For example, the value 196: It&#8217;s less than 3<sup>5<\/sup> or 243, so you divide by the next-lowest power of three,  3<sup>4<\/sup> or 81:<\/p>\n<p><code>196 &div; 3<sup>4<\/sup> = 196 &div; 81 = 2.419753086419753<\/code><\/p>\n<p>Lop off the change and you 2 for the 3<sup>4<\/sup> place digit.<\/p>\n<p>Subtract <code>2 * 81<\/code> from 196 and you get: <code>196 - 162 = 34<\/code>. The process repeats with 34:<\/p>\n<p><code>34 &div; 3<sup>3<\/sup> = 34 &div; 27 = 1.259259259259259<\/code><\/p>\n<p>Minus the change, 1 is the 3<sup>3<\/sup> place digit. Then: <code>34 - 27 = 7<\/code>. And . . . you get the idea.<\/p>\n<p>A problem appears with this approach, however, which is why it didn&#8217;t work as I coded it and I gave up. Read more about why and see my code for <em>ternary_out()<\/em> in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4192\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To output a value in ternary (base 3), you must convert the value from an integer into a string of 0, 1, and 2 characters expressing the proper value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4172\">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":[2],"tags":[],"class_list":["post-4172","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4172","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=4172"}],"version-history":[{"count":16,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4172\/revisions"}],"predecessor-version":[{"id":4211,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4172\/revisions\/4211"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}