{"id":2761,"date":"2017-10-28T00:01:39","date_gmt":"2017-10-28T07:01:39","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2761"},"modified":"2017-11-04T08:54:28","modified_gmt":"2017-11-04T15:54:28","slug":"sorted-list-presentation-part-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2761","title":{"rendered":"Sorted List Presentation, Part V"},"content":{"rendered":"<p>The process of converting the sample program from one that relies upon constants to one that exclusively uses variables is nearly complete. Only two steps are left:<br \/>\n<!--more--><\/p>\n<ol>\n<li>The number of elements in the array &#8211; DONE.<\/li>\n<li>The length of the widest string in the array &#8211; DONE.<\/li>\n<li>The number of columns in the output.<\/li>\n<li>The number of rows in the output.<\/li>\n<\/ol>\n<p>By obtaining the widest string in the array (from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2749\">last week&#8217;s Lesson<\/a>), and assuming that output is sent to a screen that&#8217;s 80-columns wide, you can calculate the maximum number of columns possible and then determine the rows required for output. Both values, columns and rows, are then used for the sorted-column process outlined in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2724\">an earlier Lesson<\/a> in this series.<\/p>\n<p>The first step is to determine how many columns of strings can fit comfortably across the screen. The assumed width is 80 characters. So, logically, you divide 80 by the widest string in the array. The problem with this approach is that the width  value could divide evenly into 80. That would leave no space between the columns. Therefore, you must also add padding to the end of each string displayed, then calculate the number of columns:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* set string width for printf() format *\/<\/span>\r\n    width = widest(fruit,items);\r\n    if( width % 2 )\r\n        width += 3;     <span class=\"comments\">\/* assert even width *\/<\/span>\r\n    else\r\n        width += 2;     <span class=\"comments\">\/* at least two spaces padding *\/<\/span><\/pre>\n<p>Above, the <em>widest()<\/em> function, presented in last week&#8217;s Lesson, obtains the longest string length from array <code>fruit[]<\/code>. The <code>items<\/code> variable represents the number of elements in <code>fruit[]<\/code>. The value returned, the length of the longest string, is stored in variable <code>width<\/code>.<\/p>\n<p>The <em>if-else<\/em> structure adds padding to the length of the widest string. If the string is an odd number, determined when <code>width%2<\/code> is true, <code>width<\/code> is increased by 3. Otherwise <code>width<\/code> is increased by 2. That way at least 2 spaces of padding are added to each column in the output.<\/p>\n<p>To determine how many columns of output can fit across the screen, I use this calculation:<\/p>\n<p><code>columns = OUTPUT_WIDTH\/width;<\/code><\/p>\n<p>The <code>OUTPUT_WIDTH<\/code> constant is set to 80. So if the widest string is 11 characters, the code would pad that out to 14 characters: 80\/14 is just under six, so the integer result is 5. The output for an array with the widest string at 11 characters would be displayed in 5 columns.<\/p>\n<p>To obtain the number of rows, I use this equation:<\/p>\n<p><code>rows = items\/columns+1;<\/code><\/p>\n<p>The number of <code>items<\/code> in the array divided by <code>columns<\/code> obtains the number of <code>rows<\/code> necessary in a table &mdash; but, 1 is added to the result. This increment accounts for the rounding error in <code>OUTPUT_WIDTH\/width<\/code> for the <code>columns<\/code> calculation.<\/p>\n<p>For example, suppose the array has 36 items and the <code>OUTPUT_WIDTH\/width<\/code> calculation results in 5 columns: 36\/5 results in 7 rows (due to integer rounding). Yet 7 rows of 5 columns shows only 35 items. Therefore, you must increment the value of <code>rows<\/code> to ensure that all array elements are displayed in the table.<\/p>\n<p>Now you have all four former-constants translated into variables. It&#8217;s possible at this point to present the final code, but I have one more trick to show you, which I&#8217;ll save for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2774\">next week&#8217;s Lesson<\/a>, the final installment of this series.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With almost all the pieces in place, it&#8217;s time to calculate the number of columns and rows for program output. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2761\">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-2761","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\/2761","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=2761"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2761\/revisions"}],"predecessor-version":[{"id":2808,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2761\/revisions\/2808"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}