{"id":2724,"date":"2017-10-07T00:01:01","date_gmt":"2017-10-07T07:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2724"},"modified":"2017-10-14T07:49:09","modified_gmt":"2017-10-14T14:49:09","slug":"sorted-list-presentation-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2724","title":{"rendered":"Sorted List Presentation, Part II"},"content":{"rendered":"<p>It&#8217;s easy to code single-column list output. It takes a bit more programming skill to present the list in columns. To sort the list by columns is the most difficult. Like many programming tasks, it&#8217;s one of those things that once you figure it out, you just keep stealing your own code to do it again and again.<br \/>\n<!--more--><br \/>\nOr, as is the case these days, programmers look to github to purloin tricks they don&#8217;t have the patience to concoct themselves. Anyway . . .<\/p>\n<p>I hope it&#8217;s your desire to be a <em>coder<\/em> and not a mere programmer. Sure, others have invented the wheel before you, but to hone your skills, I recommend figuring out things on your own. A good example is you to take list output like this:<\/p>\n<pre><code> 1\t 2\t 3\t 4\t 5\r\n 6\t 7\t 8\t 9\t10\r\n11\t12\t13\t14\t15\r\n16\t17\t18\t19\t20\r\n21\t22\t23\t24\t25\r\n26\t27\t28\t29\t30<\/code><\/pre>\n<p>And instead output this:<\/p>\n<pre><code> 1\t 7\t13\t19\t25\t\t\r\n 2\t 8\t14\t20\t26\t\t\r\n 3\t 9\t15\t21\t27\t\t\r\n 4\t10\t16\t22\t28\t\t\r\n 5\t11\t17\t23\t29\t\t\r\n 6\t12\t18\t24\t30<\/code><\/pre>\n<p>If you had cursor control, you could write code that marches output down each row, then hops to the top of the next column to repeat until done. You do not have cursor control, however, so you must output the list by rows, sorted by columns.<\/p>\n<p>Because the data set is constant in the sample program, you know that you have 30 items. The existing code sorts that list into six rows of five columns: 30, 6, 5. When I struggled to solve this problem, I knew all these constants and I scrutinized the second list (above) to divinate a solution. Here&#8217;s my observation:<\/p>\n<p>Each column element is <em>n+rows<\/em> greater than the element to the left. If you count the column position, the equation is:<\/p>\n<p><code>rows * column_position+n<\/code><\/p>\n<p>For the first row, <em>n<\/em> is 1. At row-column 0,0, the equation is <code>6 * 0 + 1 = 1<\/code>. At position 0,1 you get <code>6 * 1 + 1 = 7<\/code>, then <code>6 * 2 + 1 = 13<\/code>, <code>6 * 3 + 1 = 19<\/code>, and so on.<\/p>\n<p>For the second row, first column you get <code>6 * 0 + 2 = 2<\/code>, <code>6 * 1 + 2 = 8<\/code>, and so on.<\/p>\n<p>For the third row, <code>6 * 0 + 3 = 3<\/code>, <code>6 * 1 + 3 = 9<\/code>, and you get the idea.<\/p>\n<p>One you have that equation,  you can plug it into nested <em>for<\/em> loops to plow through the rows and columns:<\/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#define ROWS 6\r\n#define COLUMNS 5\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 r,c;\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(r=0;r&lt;ROWS;r++)\r\n    {\r\n        for(c=0;c&lt;COLUMNS;c++)\r\n            printf(\"%-12s\",fruit[ROWS*c+r]);\r\n        putchar('\\n');\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 code defines the constants I know: <code>ITEMS<\/code>, <code>ROWS<\/code>, and <code>COLUMNS<\/code>. These values for <code>ROWS<\/code> and <code>COLUMNS<\/code> are used in the nested <em>for<\/em> loops at Lines 28 and 30. The <em>rows*column_position+n<\/em> algorithm appears in the <em>printf()<\/em> statement at Line 31 to determine which array element is displayed:<\/p>\n<p><code>ROWS*c+r<\/code><\/p>\n<p><code>c<\/code> is the column number. <code>r<\/code> is the row number, 0 through <code>ROWS<\/code> (6). The result is output sorted by columns:<\/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>This is exactly the output the Boss wants. But . . . (and if you&#8217;re a programmer, you know what&#8217;s coming next) . . . the Boss wants you to add five fruits to the list: guava, persimmon, coconut, cranberry, and chestnut.<\/p>\n<p>You can continue to use constants (bad) or (better) accept that you need variables in the code instead. I begin this process in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2730\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solving a programming puzzle involves looking at where you are comparing it with where you want to go. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2724\">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-2724","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\/2724","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=2724"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2724\/revisions"}],"predecessor-version":[{"id":2771,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2724\/revisions\/2771"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}