{"id":7006,"date":"2025-06-08T00:01:46","date_gmt":"2025-06-08T07:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7006"},"modified":"2025-06-08T17:02:47","modified_gmt":"2025-06-09T00:02:47","slug":"pick-the-base-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7006","title":{"rendered":"Pick the Base &#8211; Solution"},"content":{"rendered":"<p>Your task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6987\">this month&#8217;s Exercise<\/a> is to write a program that outputs a value in any base, between 2 and 10. Sure, you can use this code to communicate with alien species that use a base other than decimal, but this benefit is only a happy coincidence to the exercise.<br \/>\n<!--more--><br \/>\nFor my solution, I stole a bit from my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4212\">earlier blog post<\/a>, though that code used only one base for translation. My solution has three parts:<\/p>\n<ol>\n<li>Gather and validate user input.<\/li>\n<li>Build a powers table in the given base.<\/li>\n<li>Output the value in the given base, avoiding leading zeros.<\/li>\n<\/ol>\n<p>Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_06-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_06-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int size = 12;\r\n    long power[size];\r\n    int base,value,x,r,pre;\r\n\r\n    <span class=\"comments\">\/* prompt for input *\/<\/span>\r\n    printf(\"Enter base (&gt;1 &lt;11): \");\r\n    scanf(\"%d\",&amp;base);\r\n    if( base&lt;2 || base&gt;10 )\r\n    {\r\n        puts(\"The base must be greater than 1 and less than 11\");\r\n        return 1;\r\n    }\r\n    printf(\"Enter value (&gt;1): \");\r\n    scanf(\"%d\",&amp;value);\r\n    if( value&lt;2 )\r\n    {\r\n        puts(\"The value must be greater than 1\");\r\n        return 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* build the powers table *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n        <span class=\"comments\">\/* calculate powers; power 0 is always 1 *\/<\/span>\r\n        power[x] = x ? power[x-1]*base : 1;\r\n\r\n    <span class=\"comments\">\/* output the value in the given base *\/<\/span>\r\n    printf(\"%d in base %d is \",value,base);\r\n    pre = 0;\r\n    for( x=size-2; x&gt;=0; x-- )\r\n    {\r\n        <span class=\"comments\">\/* process digits from largest to smallest *\/<\/span>\r\n        r = (value % power[x+1])\/power[x];\r\n        if( r==0 &amp;&amp; pre==0 )    <span class=\"comments\">\/* skip over leading zeros *\/<\/span>\r\n            continue;\r\n        else\r\n        {\r\n            pre = 1;            <span class=\"comments\">\/* no more leading zeros *\/<\/span>\r\n            printf(\"%d\",r);\r\n        }\r\n    }\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The powers table is necessary to calculate the value in a specific base. Here&#8217;s the key statement within the <em>for<\/em> loop:<\/p>\n<p><code>power[x] = x ? power[x-1]*base : 1<\/code><\/p>\n<p>Any value to the power of zero is 1, which is what the ternary expression returns above. Otherwise, the power is the previous element&#8217;s value, <code>[x-1]<\/code>, multiplied by the base:<\/p>\n<p>0 = 1<br \/>\n1 * 5 = 5<br \/>\n5 * 5 = 25<br \/>\n25 * 5 = 125<br \/>\n125 * 5 = 625<br \/>\nand so on . . .<\/p>\n<p>After the powers table is built, a second <em>for<\/em> loop works backwards through the power table, array <code>power[]<\/code>. It builds the final value in the given base by processing the digits from largest to smallest:<\/p>\n<p><code>r = (value % power[x+1])\/power[x]<\/code><\/p>\n<p>The modulus operation is performed on the value input and an element from the the powers table. The result is divided by the next value in the powers table, which yields a place value. Unless the value is larger, a series of zeros is output to prefix the result, which isn&#8217;t what a human expects to see.<\/p>\n<p>To eliminate the leading zeros, variable <code>pre<\/code> is used as a flag. The <em>if-else<\/em> test identifies the leading zeros in the result <code>r<\/code> and the value of <code>pre<\/code>. Once a digit in <code>r<\/code> other than zero is encountered, <code>pre<\/code> is set to one and the digits are then output. I wish a more elegant way to eliminate the zeros was available, though I can&#8217;t think of one at the moment.<\/p>\n<p>Here&#8217;s a sample run of my solution:<\/p>\n<p><code>Enter base (>1 <11): 10\nEnter value (>1): 12345<br \/>\n12345 in base 10 is 12345<\/code><\/p>\n<p>I hope your solution met with success. I&#8217;m going to continue on this topic with next month&#8217;s Exercise, which will be a degree more difficult.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your task for this month&#8217;s Exercise is to write a program that outputs a value in any base, between 2 and 10. Sure, you can use this code to communicate with alien species that use a base other than decimal, &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7006\">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-7006","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\/7006","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=7006"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7006\/revisions"}],"predecessor-version":[{"id":7014,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7006\/revisions\/7014"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}