{"id":4342,"date":"2020-09-08T00:01:03","date_gmt":"2020-09-08T07:01:03","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4342"},"modified":"2020-09-12T11:04:15","modified_gmt":"2020-09-12T18:04:15","slug":"spelling-numbers-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4342","title":{"rendered":"Spelling Numbers &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4329\">this month&#8217;s Exercise<\/a> is to write code that outputs strings for number values, 0 through 100. Your goal is to be clever with your solution, something that took me a few versions to get correct.<br \/>\n<!--more--><br \/>\nIn English, similar to other languages, the verbal output for numbers follows a pattern. Three groups are apparent: single digits, teens, and multiples of ten that follow. The multiples of ten are combined with the single digits to generate the between values, such as &#8220;twenty-one,&#8221; &#8220;twenty-two,&#8221; and so on. Any exceptions can be pulled out in the code.<\/p>\n<p>To craft my solution, I created three string arrays:<\/p>\n<pre class=\"screen\">\r\nchar *ones[10] = { \"one\", \"two\", \"three\", \"four\",\r\n                   \"five\", \"six\", \"seven\", \"eight\",\r\n                   \"nine\", \"ten\" };\r\nchar *teens[9] = { \"eleven\", \"twelve\", \"thirteen\",\r\n                   \"fourteen\", \"fifteen\", \"sixteen\",\r\n                   \"seventeen\", \"eighteen\", \"nineteen\" };\r\nchar *tens[8] = { \"twenty\", \"thirty\", \"forty\",\r\n                  \"fifty\", \"sixty\", \"seventy\",\r\n                  \"eighty\", \"ninety\" };<\/pre>\n<p>Each of these arrays handle the three number patterns. I could have tried to be clever with sixteen, seventeen, eighteen, and nineteen, but the effort didn&#8217;t seem worthy.<\/p>\n<p>For values 0 and 100, I coded special exceptions in my solution:<\/p>\n<p><code>if( n==0 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return(\"zero\");<br \/>\nif( n==100 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return(\"one hundred\");<\/code><\/p>\n<p>These statements are followed by a series of <em>if<\/em> tests that work through the other patterns. The key, however, was merging the <code>tens[]<\/code> strings with the <code>ones[]<\/code> strings for values 20 and above. Here&#8217;s my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_09-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_09-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* return string representing integers 0 through 100 *\/<\/span>\r\nchar *verbal(int n)\r\n{\r\n    char *ones[10] = { \"one\", \"two\", \"three\", \"four\",\r\n                       \"five\", \"six\", \"seven\", \"eight\",\r\n                       \"nine\", \"ten\" };\r\n    char *teens[9] = { \"eleven\", \"twelve\", \"thirteen\",\r\n                       \"fourteen\", \"fifteen\", \"sixteen\",\r\n                       \"seventeen\", \"eighteen\", \"nineteen\" };\r\n    char *tens[8] = { \"twenty\", \"thirty\", \"forty\",\r\n                      \"fifty\", \"sixty\", \"seventy\",\r\n                      \"eighty\", \"ninety\" };\r\n    int dec,uns;\r\n    static char buffer[17];\r\n\r\n    <span class=\"comments\">\/* no need for if-else with returns *\/<\/span>\r\n        <span class=\"comments\">\/* elimninate 0 and 100 *\/<\/span>\r\n    if( n==0 )\r\n        return(\"zero\");\r\n    if( n==100 )\r\n        return(\"one hundred\");\r\n    <span class=\"comments\">\/* process ones *\/<\/span>\r\n    if( n&lt;11 )\r\n        return(ones[n-1]);\r\n    <span class=\"comments\">\/* process teens *\/<\/span>\r\n    if( n&lt;20 )\r\n        return(teens[n-11]);\r\n    <span class=\"comments\">\/* process the rest *\/<\/span>\r\n        <span class=\"comments\">\/* get the ten multiple *\/<\/span>\r\n    dec = (n - 20) \/ 10;\r\n    uns = (n - 20) % 10 - 1;\r\n        <span class=\"comments\">\/* pull out tens *\/<\/span>\r\n    if( uns &lt; 0 )\r\n        return(tens[dec]);\r\n        <span class=\"comments\">\/* build string for everything else *\/<\/span>\r\n    snprintf(buffer,17,\"%s %s\",tens[dec],ones[uns]);\r\n    return(buffer);\r\n}\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=0; x&lt;=100; x++ )\r\n        printf(\"%s\\n\",verbal(x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This solution is specific to values 0 through 100, as used in the <em>main()<\/em> function. I didn&#8217;t write any safety valves in the <em>verbal()<\/em> function to deal with out-of-range values.<\/p>\n<p>Starting at Line 30, values 20 and greater are tested. To obtain the multiple of ten, the <code>dec<\/code> variable is assigned in this statement:<\/p>\n<p><code>dec = (n - 20) \/ 10;<\/code><\/p>\n<p>The first string in the <code>tens[]<\/code> array is <code>\"twenty\"<\/code>, element zero. So the value of <code>dec<\/code> must reference this element as zero when <code>n<\/code> is equal to 20, then incremented by one for each subsequent value of 10.<\/p>\n<p>Variable <code>uns<\/code> tracks what you could call the offset within each multiple of 10. Its value is set at Line 33:<\/p>\n<p><code>uns = (n - 20) % 10 - 1;<\/code><\/p>\n<p>As an example, if variable <code>n<\/code> is equal to 44, the value of <code>dec<\/code> will be 3 for string <code>\"forty\"<\/code> and the value of <code>uns<\/code> will be 3 for string <code>\"four\"<\/code>. When <code>n<\/code> is evenly divisible by 10, the value of <code>uns<\/code> is calculated as negative, a condition that&#8217;s tested for at Line 35:<\/p>\n<p><code>if( uns &lt; 0 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return(tens[dec]);<\/code><\/p>\n<p>At Line 38, the <code>tens[]<\/code> string and the <code>uns[]<\/code> string are thrust into a <code>buffer<\/code> to return as a single return string, dealing with the between numbers.<\/p>\n<p>The full output for my solution can be viewed <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/09\/verbal_output.txt\">here<\/a>.<\/p>\n<p>I hope your solution worked and that you devised some type of clever mechanism to handle the redundancy in the output. That&#8217;s really the key here. In fact, if you want, you can continue to modify the program to generate output to 10,000. Doing so adds only a few more lines of code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to write code that outputs strings for number values, 0 through 100. Your goal is to be clever with your solution, something that took me a few versions to get correct.<\/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-4342","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\/4342","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=4342"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4342\/revisions"}],"predecessor-version":[{"id":4374,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4342\/revisions\/4374"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}