{"id":4238,"date":"2020-07-08T00:01:44","date_gmt":"2020-07-08T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4238"},"modified":"2020-07-04T09:53:46","modified_gmt":"2020-07-04T16:53:46","slug":"nested-processing-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4238","title":{"rendered":"Nested Processing &#8211; Solution"},"content":{"rendered":"<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4223\">This month&#8217;s Exercise<\/a> wasn&#8217;t astronomically difficult, but I did spend some time devising a solution. My goal was to try to avoid writing a nested loop. This goal proved unattainable.<br \/>\n<!--more--><br \/>\nTo review, given an array of five integers, write code that outputs a text-mode bar graph based on the integers&#8217; values. Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_07-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_07-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int v[5] = { 5, 3, 8, 7, 2 };\r\n    int a,b;\r\n\r\n    for(a=0;a&lt;5;a++)\r\n    {\r\n        printf(\"%d: \",a+1);\r\n        for(b=0;b&lt;v[a];b++)\r\n            putchar('*');\r\n        printf(\" (%d)\\n\",v[a]);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Obviously, a nested loop is required. The outer loop, governed by variable <code>a<\/code>, plows through the array elements. The inner loop, powered by variable <code>b<\/code>, outputs asterisks to make the bar chart.<\/p>\n<p>Extra fluff in the code helps make the output pretty:<\/p>\n<p>Line 10 outputs the element number, adding one to variable <code>a<\/code> to make the value sensical to humans: <code>printf(\"%d: \",a+1);<\/code><\/p>\n<p>Line 13 outputs a summary after each bar is generated, listing the number of asterisks: <code>printf(\" (%d)\\n\",v[a]);<\/code><\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>1: ***** (5)<br \/>\n2: *** (3)<br \/>\n3: ******** (8)<br \/>\n4: ******* (7)<br \/>\n5: ** (2)<\/code><\/p>\n<p>This solution could also be done with <em>while<\/em> loops or a mixture of <em>while<\/em> and <em>for<\/em> loops.<\/p>\n<p>For my non-nested loop attempts, the closest I cam to devising a single-loop solution is the following code, which isn&#8217;t an official solution, so it doesn&#8217;t count:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int v[5] = { 5, 3, 8, 7, 2 };\r\n    int a;\r\n\r\n    for(a=0;a&lt;5;a++)\r\n    {\r\n        printf(\"%d: %*c (%d)\\n\",\r\n                a+1,\r\n                v[a],\r\n                '*',\r\n                v[a]\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In Line 10, the <em>printf()<\/em> variable-width placeholder is used as <code>%*c<\/code> to fake a kinda bar graph. The <code>*<\/code> specifies a variable width, which can work for a single character. Here&#8217;s the output:<\/p>\n<p><code>1:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* (5)<br \/>\n2:&nbsp;&nbsp;&nbsp;* (3)<br \/>\n3:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* (8)<br \/>\n4:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* (7)<br \/>\n5:&nbsp;&nbsp;* (2)<\/code><\/p>\n<p>Alas, this solution just doesn&#8217;t cut it because a bar graph isn&#8217;t output.<\/p>\n<p>I thought about using a function to generate the asterisks, but it too would have contained a loop. And there&#8217;s no way I&#8217;m going to use <em>goto<\/em>, which would be cheating in my opinion.<\/p>\n<p>So the solution remains simple and not impossible to attain. I hope you arrived at something similar.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise wasn&#8217;t astronomically difficult, but I did spend some time devising a solution. My goal was to try to avoid writing a nested loop. This goal proved unattainable.<\/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-4238","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\/4238","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=4238"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4238\/revisions"}],"predecessor-version":[{"id":4244,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4238\/revisions\/4244"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}