{"id":252,"date":"2013-09-01T00:01:26","date_gmt":"2013-09-01T08:01:26","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=252"},"modified":"2013-08-30T08:38:34","modified_gmt":"2013-08-30T16:38:34","slug":"min-and-max","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=252","title":{"rendered":"Min and Max"},"content":{"rendered":"<p>The C language is rather weak when it comes to array functions. In fact, as far as I know, the standard library doesn&#8217;t contain a single array function.<br \/>\n<!--more--><br \/>\nWell, yeah: You can count the <code>strings.h<\/code> functions as array functions, but they operate only on <em>char<\/em> arrays. Missing from the library are a host of other array functions, some of which you&#8217;d never know existed unless you&#8217;ve dabbled in some higher-level programming languages.<\/p>\n<p>As a sample, here are some of my favorite array functions from other languages. (I&#8217;m using generic names in this examples.)<\/p>\n<p><em>array_sort()<\/em> This function gobbles a numeric array, sorts the contents low-high or high-low (however you specify) and returns the sorted array. Most languages have a slew of functions that sort arrays in every possible way.<\/p>\n<p><em>array_average()<\/em> This function returns the average value stored in the array. Similarly, other array functions let you mathematically manipulate the array&#8217;s contents in a variety of obscure ways.<\/p>\n<p><em>array_chop()<\/em> This function (actually a family of functions) slices, dices, splits and spits up an array in various ways.<\/p>\n<p><em>array_max()<\/em> This function returns the highest (maximum) value of all the values stored in the array.<\/p>\n<p><em>array_min()<\/em> This function returns the lowest (minimum) value stored in the array.<\/p>\n<p>Any grizzled veteran of the C language will promptly explain to you that having such functions in the C library is utterly useless. After all, you can always code your own. And, in fact, that&#8217;s the assignment for this month.<\/p>\n<p>Don&#8217;t fret! I&#8217;m not going to be cruel. In fact, the <em>array_max()<\/em> and <em>array_min()<\/em> functions are frequently found in most C programming tomes as basic function examples. Such examples are missing in my C programming books, but that doesn&#8217;t mean you miss out on the fun.<\/p>\n<p>Below you see sample code that carries out the basic operations for the exercise: An array is created and filled with random values. Your job is to craft the <em>array_max()<\/em> and <em>array_min()<\/em> functions, stubbed out in the code.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define SIZE 20\r\n\r\nint array_max(int *a,int s);\r\nint array_min(int *a,int s);\r\n\r\nint main()\r\n{\r\n    int array[SIZE];\r\n    int x;\r\n\r\n\/* Fill the array with random values *\/\r\n    srand((unsigned)time(NULL));        \/* seed randomizer *\/\r\n    for(x=0;x&lt;SIZE;x++)\r\n        array[x] = rand() % 100 + 1;\r\n\r\n\/* Display the array *\/\r\n    puts(\"The Array:\");\r\n    for(x=0;x&lt;SIZE;x++)\r\n        printf(\"%3d \",array[x]);\r\n    putchar('\\n');\r\n\r\n    printf(\"Highest value: %d\\n\",\r\n        array_max(array,SIZE));\r\n    printf(\"Lowest value: %d\\n\",\r\n        array_min(array,SIZE));\r\n\r\n    return(0);\r\n}\r\n\r\nint array_max(int *a,int s)\r\n{\r\n}\r\n\r\nint array_min(int *a,int s)\r\n{\r\n}<\/pre>\n<p>One of the limitations of C is that the array size must be passed to the function in addition to the array&#8217;s base address; unlike some higher-level languages, the array&#8217;s size isn&#8217;t information known to the program. (You could use <em>sizeof<\/em> to fetch the array&#8217;s size and then divide it by <code>sizeof(int)<\/code>, but this code simply passes the <code>SIZE<\/code> constant to the array functions, which works.)<\/p>\n<p>As I urge every month, please attempt to solve the puzzle on your own before you click the link below to view my solution. Also, keep in mind that my code is one of many potential solutions to the exercise.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/09\/09exercise.c\">Exercise Solution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you (a human, I&#8217;m assuming) look at a long series of values, it takes a while and a bit of brain muscle to see the largest and smallest values. On the other hand, for a computer, it takes only a glance. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=252\">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":[3],"tags":[],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/252","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=252"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions\/276"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}