{"id":1912,"date":"2016-05-08T00:01:12","date_gmt":"2016-05-08T07:01:12","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1912"},"modified":"2016-05-08T07:54:56","modified_gmt":"2016-05-08T14:54:56","slug":"off-to-the-races-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1912","title":{"rendered":"Off to the Races &#8211; Solution"},"content":{"rendered":"<p>The purpose of the <em>winner()<\/em> function in the horse race game is to determine which value in an integer array is the largest. That would be an easy exercise to write, but it lacks the thrill and excitement of a horse race.<br \/>\n<!--more--><br \/>\nOkay, I agree: Watching a few columns of numbers cascade down the terminal screen is neither thrilling nor exciting.<\/p>\n<p>In the <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/05exercise.c\"\">code I provided<\/a> for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1899\">this month&#8217;s Exercise<\/a>, I omitted the <em>winner()<\/em> function. It&#8217;s called at Line 50 in the sample code, buried within a <em>printf()<\/em> statement:<\/p>\n<pre><code>    printf(\"And the winner is Pony %d!\\n\",\r\n            winner(horse));<\/code><\/pre>\n<p>The variable <code>horse<\/code> is an <em>int<\/em> array. That&#8217;s value (the array&#8217;s address, actually) is passed to the function, and the function returns an <em>int<\/em> value. It&#8217;s important to keep in mind that the value returned is an <strong>element number<\/strong>, not the value of any element within the array. Read the <em>printf()<\/em> statement again: <code>And the winner is Pony %d!\\n<\/code><\/p>\n<p>The <em>winner()<\/em> function stub looks like this:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Determine which horse won *\/<\/span>\r\nint winner(int *array)\r\n{\r\n    return(0);\r\n}<\/pre>\n<p>The <code>horse[]<\/code> array is passed to the function and referenced as <code>array<\/code> within the function. The number of elements is set to the constant <code>HORSES<\/code>. So the function must loop through the array and determine which element holds the largest value. It then returns that element number, plus one. (Because the program doesn&#8217;t show Pony 0.)<\/p>\n<p>Here&#8217;s the solution that I wrote:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Determine which horse won *\/<\/span>\r\nint winner(int *array)\r\n{\r\n    int max,x;\r\n\r\n    max = 0;\r\n    for(x=1; x&lt;HORSES; x++)\r\n    {\r\n        if( array[x] &gt; array[max] )\r\n            max = x;\r\n    }\r\n    return(max+1);\r\n}<\/pre>\n<p>Variable <code>max<\/code> is the array element holding the largest value. It starts at zero with the assumption  that element 0 contains the largest value.<\/p>\n<p>The <em>for<\/em> loop prances through the rest of the array elements, 1 through <code>HORSES<\/code>. Within the loop, the two array elements are compared. If element <code>max<\/code>&#8216;s value is larger that element <code>x<\/code>, the value of <code>max<\/code> is unchanged. When element <code>x<\/code>&#8216;s value is larger, <code>max<\/code> is set equal to element <code>x<\/code>.<\/p>\n<p>After the loop is processed, <code>max<\/code> contains the array element with the largest value &mdash; the winner. That element number is returned, plus 1 so that the human associates it with the proper pony.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/05exercise-solution.c\"\">Click here<\/a> to view the entire code with my solution for the <em>winner()<\/em> function.<\/p>\n<p>The <em>winner()<\/em> function is a variation of the good ol&#8217; <em>max()<\/em> function, which is demonstrated in most beginning programming courses. The <em>max()<\/em> function returns the greatest value between two variables or all elements in an array. The twist here is that the <em>winner()<\/em> function returns the element number, not the value.<\/p>\n<p>Your solution might be different than the one shown here. As long as you return the element number for the winning pony and not the value, you&#8217;re good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The purpose of the winner() function in the horse race game is to determine which value in an integer array is the largest. That would be an easy exercise to write, but it lacks the thrill and excitement of a &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1912\">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":[5],"tags":[],"class_list":["post-1912","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1912","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=1912"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1912\/revisions"}],"predecessor-version":[{"id":1936,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1912\/revisions\/1936"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}