{"id":2749,"date":"2017-10-21T00:01:19","date_gmt":"2017-10-21T07:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2749"},"modified":"2017-10-28T08:27:06","modified_gmt":"2017-10-28T15:27:06","slug":"sorted-list-presentation-part-iv","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2749","title":{"rendered":"Sorted List Presentation, Part IV"},"content":{"rendered":"<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2730\">Last week&#8217;s Lesson<\/a> accomplished the task of obtaining the number of elements in the <code>fruit[]<\/code> array. This week, the task is to obtain the next item, the widest string in the array. This item is currently a constant, but it must become a variable to ensure the code&#8217;s flexibility.<br \/>\n<!--more--><br \/>\nHere are the variables the code needs to calculate:<\/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.<\/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>You must first know the widest string in the array before you can determine how many columns of output you need, and then calculate the number of rows. Remember, this series assumes the terminal window is fixed to 80-characters-wide for output. So, if the widest string is 78 characters, output has only one column. If the widest string is 9 characters, you could squeeze in 10 columns.<\/p>\n<p>To determine the widest string in an array you need a type of <em>max()<\/em> function. This is a typical demo function found  basic programming instruction courses: Return the maximum value in an array.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint max(int a[])\r\n{\r\n    int x,max;\r\n\r\n    max = 0;        <span class=\"comments\">\/* must initialize max *\/<\/span>\r\n    for(x=0;x&lt;8;x++)\r\n    {\r\n        if(a[x] &gt; max)\r\n            max = a[x];\r\n    }\r\n\r\n    return(max);\r\n}\r\n\r\nint main()\r\n{\r\n    int array[] = {\r\n        2, 13, 3, 17, 5, 19, 7, 11\r\n    };\r\n\r\n    printf(\"The largest value is %d\\n\",\r\n            max(array));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the <em>max()<\/em> function, each array element is compared with variable <code>max<\/code>. If the element is larger, it&#8217;s assigned as the new value for <code>max<\/code>. After all elements are scanned, the value of <code>max<\/code> is returned.<\/p>\n<blockquote><p>The <em>max()<\/em> function has many variations. It&#8217;s often a challenge in C to see how few lines of code can be used to write it, as well as how obnoxiously obfuscated it can read.<\/p><\/blockquote>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>The largest value is 19<\/code><\/pre>\n<p>A similar approach can be taken for strings. For example:<\/p>\n<pre class=\"screen\">\r\nint widest(char **fruit, int i)\r\n{\r\n    int x,len,max;\r\n\r\n    max = 0;\r\n    for(x=0;x&lt;i;x++)\r\n    {\r\n        len = strlen(*(fruit+x));\r\n        max = len &gt; max ? len : max;\r\n    }\r\n\r\n    return(max);\r\n}<\/pre>\n<p>The first, and perhaps most frightening thing in the <em>widest()<\/em> function, is the <code>**fruit<\/code> declaration. This monster is required to pass an array of strings to a function. (Refer to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=441\">this Lesson<\/a> for specifics.)<\/p>\n<p>The <em>widest()<\/em> function itself works similar to the <em>max()<\/em> function presented earlier. For each element in the array (variable <code>i<\/code>), string length is obtained. The <code>*(fruit+x)<\/code> monster grabs each element. The ternary operator sets the value of variable <code>max<\/code>. This method is yet another way to express the &#8220;max&#8221; expression. And the largest value &mdash; the widest string length &mdash; is returned.<\/p>\n<p>When processing the <code>fruit[]<\/code> array (the example used in this series), here&#8217;s the output:<\/p>\n<pre><code>The widest string is 11 chars long.<\/pre>\n<p><\/code><\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/10\/1021b.c\">Click here<\/a> to view the entire code. This example also incorporates the trick from last week's Lesson that determines the number of elements in the array.<\/p>\n<p>You might wonder why the longest string itself isn't displayed in the output. Keep in mind that the purpose of the <em>width()<\/em> function is to obtain the longest string <em>length<\/em>, which is item number two on the list of variables to calculate in the code. The longest string itself isn't important, but its width is necessary to determine the maximum number of columns in which to present the list on an 80-character-wide terminal screen.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2761\">Next week's Lesson<\/a> presents the final two calculations required, both of which rely upon the value of the <code>width<\/code> variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To figure out which string in an array is the longest, you craft a <em>max()<\/em>-like function, but one that must use the ugly <code>**<\/code> pointer notation. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2749\">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-2749","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\/2749","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=2749"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2749\/revisions"}],"predecessor-version":[{"id":2788,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2749\/revisions\/2788"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}