{"id":2713,"date":"2017-09-30T00:01:46","date_gmt":"2017-09-30T07:01:46","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2713"},"modified":"2017-10-07T08:30:00","modified_gmt":"2017-10-07T15:30:00","slug":"sorted-list-presentation-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2713","title":{"rendered":"Sorted List Presentation, Part I"},"content":{"rendered":"<p>Sometimes programming can be a snap, especially when you have a known data set and pretty much all the values are static. You can whip out code right away, it runs well and looks beautiful &mdash; but that&#8217;s because everything is known. The fewer variables, the easier to code.<br \/>\n<!--more--><br \/>\nAs an example, consider a list of items:<\/p>\n<pre class=\"screen\">\r\n    char *fruit[] = {\r\n        \"apple\", \"orange\", \"blueberry\", \"date\", \"melon\",\r\n        \"grape\", \"peach\", \"mango\", \"watermelon\", \"pear\",\r\n        \"olive\", \"plum\", \"nectarine\", \"kiwi\", \"loquat\",\r\n        \"honeydew\", \"lime\", \"grapefruit\", \"cherry\",\r\n        \"avocado\", \"tomato\", \"banana\", \"cantaloupe\",\r\n        \"huckleberry\", \"kumquat\", \"tangerine\", \"fig\",\r\n        \"raspberry\", \"papaya\", \"lychee\", \"guava\"\r\n    };<\/pre>\n<p>The <code>fruit[]<\/code> array consists of strings (<em>char<\/em> pointers). If you were to sort this array and present the list, the following code does the job:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define ITEMS 30\r\n\r\nint compare(const void *a, const void *b);\r\n\r\nint main()\r\n{\r\n    char *fruit[] = {\r\n        \"apple\", \"orange\", \"blueberry\", \"date\", \"melon\",\r\n        \"grape\", \"peach\", \"mango\", \"watermelon\", \"pear\",\r\n        \"olive\", \"plum\", \"nectarine\", \"kiwi\", \"loquat\",\r\n        \"honeydew\", \"lime\", \"grapefruit\", \"cherry\",\r\n        \"avocado\", \"tomato\", \"banana\", \"cantaloupe\",\r\n        \"huckleberry\", \"kumquat\", \"tangerine\", \"fig\",\r\n        \"raspberry\", \"papaya\", \"lychee\"\r\n    };\r\n    int x;\r\n\r\n<span class=\"comments\">\/* quicksort the list *\/<\/span>\r\n    qsort(fruit,ITEMS,sizeof(char *),compare);\r\n\r\n<span class=\"comments\">\/* display sorted strings *\/<\/span>\r\n    for(x=0;x&lt;ITEMS;x++)\r\n    {\r\n        printf(\"%s\\n\",fruit[x]);\r\n    }\r\n\r\n    return(0);\r\n}\r\n\r\n<span class=\"comments\">\/* compare routine for quicksort *\/<\/span>\r\nint compare(const void *a, const void *b)\r\n{\r\n    const char **pa, **pb;\r\n\r\n    pa = (const char **)a;\r\n    pb = (const char **)b;\r\n    return( strcmp(*pa,*pb) );\r\n}<\/pre>\n<p>The meat of this code is the quicksort function, <em>qsort()<\/em> at Line 23. You can read the details <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1293\">here<\/a>, and see that the bulk of the code was copied from that earlier Lesson.<\/p>\n<p>The output shows the sorted list of 30 items. The code relies upon the <code>ITEMS<\/code> constant, which reflects the number of elements in the array. So the programmer (me) assumes that the number of elements won&#8217;t change, which is okay &mdash; so far.<\/p>\n<p>Now assume that The Boss likes the code, but the single-column list is too long. So he directs you to present the list in rows and columns. That modification is simple enough: Only the output part of the code changes:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* display sorted strings *\/<\/span>\r\n    x = 0;\r\n    while(x &lt; ITEMS)\r\n    {\r\n        printf(\"%-12s\",fruit[x]);\r\n        x++;\r\n        <span class=\"comments\">\/* new row each 5 items *\/<\/span>\r\n        if( x%5 == 0)\r\n            putchar('\\n');\r\n    }<\/pre>\n<p>I elected to use a <em>while<\/em> loop because it&#8217;s easier to process the elements one at a time, using a single variable <code>x<\/code> as a counter, than to mess with an extra variable required for nested <em>for<\/em> loops.<\/p>\n<p>In the <em>printf()<\/em> statement, the <code>%-12s<\/code> placeholder left-aligns each string within 12 spaces, which helps line-up the output. (<a href=\"http:\/\/c-for-dummies.com\/blog\/?p=314\">Relevant Lesson here<\/a>.) And 12 is chosen because the longest string in the array, <code>huckleberry<\/code>, is 11 characters long; it&#8217;s a known value.<\/p>\n<p>The <code>if( x%5 == 0 )<\/code> statement pops out a newline after every five elements. The output presents the sorted list in six rows of five columns:<\/p>\n<pre><code>apple       avocado     banana      blueberry   cantaloupe   \r\ncherry      date        fig         grape       grapefruit  \r\nhoneydew    huckleberry kiwi        kumquat     lime        \r\nloquat      lychee      mango       melon       nectarine   \r\nolive       orange      papaya      peach       pear        \r\nplum        raspberry   tangerine   tomato      watermelon<\/code><\/pre>\n<p>It looks good to me, but I&#8217;m only the programmer. The Boss, on the other hand, is puzzled that the list isn&#8217;t sorted by columns. In other words, he wants this output instead:<\/p>\n<pre><code>apple       date        kiwi        melon       pear        \r\navocado     fig         kumquat     nectarine   plum        \r\nbanana      grape       lime        olive       raspberry   \r\nblueberry   grapefruit  loquat      orange      tangerine   \r\ncantaloupe  honeydew    lychee      papaya      tomato      \r\ncherry      huckleberry mango       peach       watermelon<\/code><\/pre>\n<p>In the first column is <code>apple<\/code> through <code>cherry<\/code>, then <code>date<\/code> through <code>mango<\/code> in the second column, and so on. The code for this output is presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2724\">next week&#8217;s Lesson<\/a>. Even so, this Lesson began as an Exercise, one that proved more difficult than I had planned. So if you want to concoct your own code for the above output, please do so.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy to spew out a list of items. When you mix in rows and columns, things can get twisted fast. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2713\">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-2713","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\/2713","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=2713"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2713\/revisions"}],"predecessor-version":[{"id":2756,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2713\/revisions\/2756"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}