{"id":2774,"date":"2017-11-04T00:01:01","date_gmt":"2017-11-04T07:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2774"},"modified":"2017-10-28T11:26:05","modified_gmt":"2017-10-28T18:26:05","slug":"sorted-list-presentation-part-vi","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2774","title":{"rendered":"Sorted List Presentation, Part VI"},"content":{"rendered":"<p>With almost all the former-constants converted to variables, only one puzzle remains: Padding each string&#8217;s output so that the columns line up. (Yes, this is the final part of this series!)<br \/>\n<!--more--><br \/>\nFrom <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2761\">last week&#8217;s Lesson<\/a>, the preferred output width for each string is calculated based on the widest string in the array:<\/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>The widest string in the <code>fruit<\/code> array must be padded with spaces to the right. Otherwise, the output columns look funky. With the widest string value stored in variable <code>width<\/code>, two or three spaces are added as padding, depending on whether <code>width<\/code> is odd or even.<\/p>\n<p>The <code>width<\/code> value is used to display all strings in neat columns. For example, when <code>width<\/code> is 14 and the string is <code>\"apple\"<\/code>, you must output the following to get the string to line up in a column with the next row&#8217;s text:<\/p>\n<p><code>\"apple&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"<\/code><\/p>\n<p>Above, that&#8217;s &#8220;apple&#8221; plus nine spaces. I can think of a number of ways to accomplish this task.<\/p>\n<p>One way to set consistent column width is to count characters: Display the string one character at a time, then count up to <code>width<\/code> while outputting space characters. That&#8217;s not the method I used, however. Instead, I looked at a <em>printf()<\/em> example from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2724\">an earlier Lesson<\/a>:<\/p>\n<p><code>printf(\"%-12s\",fruit[ROWS*c+r]);<\/code><\/p>\n<p>The <code>%-12s<\/code> placeholder outputs a string, left-justified, within a 12-character space. The value 12 is what needs to change at runtime, based on the value of the <code>width<\/code> variable. To get  <code>width<\/code> into a <em>printf()<\/em> format string, I used the <em>sprintf()<\/em> function:<\/p>\n<p><code>sprintf(format,\"%%-%02ds\",width);<\/code><\/p>\n<p>Variable <code>format<\/code> is a 6-byte <em>char<\/em> buffer. The <code>%%-%02ds<\/code> monster creates a formatting string: <code>%<\/code> signs must be doubled, and the <code>%02d<\/code> is replaced by the value of variable <code>width<\/code>. So if <code>width<\/code> equals 14, the output <em>sprintf()<\/em> sends to the <code>format<\/code> buffer is <code>%-14s<\/code>, which left-justifies a string in a 14-character space. When variable <code>width<\/code> holds another value, the <code>format<\/code> string adjusts accordingly, which helps corral output into the proper-sized columns.<\/p>\n<p><em>Whew!<\/em><\/p>\n<p>All the pieces are together and most everything required to output the <code>fruit<\/code> array in neat, sorted columns is a variable. It&#8217;s a massive puzzle, which I hope that you appreciate why I didn&#8217;t set this task as an Exercise; it takes me six Lessons to explain it!<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/10\/1104.c\">Click here<\/a> to view the final code, which contains all the bits and pieces described over the past few Lessons.<\/p>\n<p>Now The Boss can add or remove fruit from the array and the only code modification you need to make is to the array itself. The rest of the code accommodates any changes. Try it on your own: Add or remove array elements, make up a long name for some fruit, or shorten all the names to only a few characters. See how the changes are accommodated, thanks to the code&#8217;s built-in flexibility.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this final installment, all the pieces are brought together. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2774\">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-2774","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\/2774","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=2774"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2774\/revisions"}],"predecessor-version":[{"id":2807,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2774\/revisions\/2807"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}