{"id":7274,"date":"2025-12-08T00:01:20","date_gmt":"2025-12-08T08:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7274"},"modified":"2025-12-06T11:38:46","modified_gmt":"2025-12-06T19:38:46","slug":"which-is-greatest-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7274","title":{"rendered":"Which is Greatest? &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7257\">this month&#8217;s Exercise<\/a> is to write a function, <em>greatest()<\/em>, that returns the largest of three values. While this task could be done easily with an <em>if-else<\/em> construction, the second part of the challenge is to write the entire thing as a single ternary statement. How&#8217;d you do?<br \/>\n<!--more--><br \/>\nStarting with the easy way, here is my first solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_12-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2025_12-Exercise-a.c<\/a><\/h3>\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\nint greatest(int a, int b, int c)\r\n{\r\n    int max;\r\n\r\n    max = a;\r\n    if( b&gt;max )\r\n        max = b;\r\n    if( c&gt;max )\r\n        max = c;\r\n\r\n    return(max);\r\n}\r\n\r\nint main()\r\n{\r\n    int x,y,z,g;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* generate three random values from 2 to 25 *\/<\/span>\r\n    x = rand() % 24 + 2;\r\n    y = rand() % 24 + 2;\r\n    z = rand() % 24 + 2;\r\n\r\n    g = greatest(x,y,z);\r\n    printf(\"The greatest of %d, %d, and %d is %d\\n\",\r\n            x,y,z,g\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>main()<\/em> function generates the three random values, <code>x<\/code>, <code>y<\/code>, and <code>z<\/code>, in the range of 2 to 25. These values are passed to the <em>greatest(<\/em>) function, which returns the largest value. The results are output.<\/p>\n<p>In the <em>greatest()<\/em> function, argument <code>a<\/code> is assigned as the greatest value, stored in variable <code>max<\/code>: <code>max = a<\/code> This assignment is followed by two comparisons: if variable <code>b<\/code> is greater than <code>max<\/code>, <code>max<\/code> is assigned its value. Should variable <code>c<\/code> be greater than <code>max<\/code>, <code>max<\/code> is assigned its value. Finally, the value of <code>max<\/code> is returned.<\/p>\n<p>Here are several sample runs of this solution:<\/p>\n<p><code>The greatest of 11, 6, and 5 is 11<br \/>\nThe greatest of 4, 11, and 25 is 25<br \/>\nThe greatest of 24, 9, and 12 is 24<br \/>\nThe greatest of 3, 17, and 12 is 17<br \/>\nThe greatest of 18, 11, and 24 is 24<\/code><\/p>\n<p>The second solution &mdash; if you dared attempt it &mdash; uses the ternary operator in a single statement to obtain the maximum value of the three arguments passed. This solution requires updating the <em>greatest()<\/em> function to include only the following statement:<\/p>\n<p><code>return( b&gt;a ? b&gt;c ? b : c : a&gt;c ? a : c );<\/code><\/p>\n<p>Marvel at the obfuscation! The code works, and it took me a while to construct it properly. In fact, I admire it even more when I remove the whitespace:<\/p>\n<p><code>return( b&gt;a?b&gt;c?b:c:a&gt;c?a:c );<\/code><\/p>\n<p>To unwind this expression, start at the right end, which I&#8217;m color-coding here:<\/p>\n<p><code>return( b&gt;a ? b&gt;c ? b : c : <span style=\"color:red; text-decoration:underline;\">a&gt;c ? a : c<\/span> );<\/code><\/p>\n<p>The result of this operation is argument <code>a<\/code> or <code>c<\/code>, whichever is greater.<\/p>\n<p>Next, hop to the second part of the expression:<\/p>\n<p><code>return( b&gt;a ? <span style=\"color:red; text-decoration:underline;\">b&gt;c ? b : c<\/span> : a&gt;c ? a : c );<\/code><\/p>\n<p>The result of this expression is either argument <code>b<\/code> or <code>c<\/code>, whichever is greater.<\/p>\n<p>At this point, both arguments <code>a<\/code> and <code>b<\/code> have been compared with argument <code>c<\/code>. What remains is for them to be compared with each other, which is the first part of the expression: <code>b&gt;a<\/code>. So, if <code>b<\/code> is greater than <code>a<\/code> and <code>b<\/code> is greater than <code>c<\/code>, <code>b<\/code> is returned. Otherwise, if <code>a<\/code> is greater than <code>b<\/code> and <code>a<\/code> is greater than <code>c<\/code>, it&#8217;s returned. Otherwise, <code>c<\/code> is returned.<\/p>\n<p>What a mess! Nested ternary statements are insane! But the thing works.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_12-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view my second solution in its entirety on GitHub.<\/p>\n<p>I hope that your solutions met with success!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to write a function, greatest(), that returns the largest of three values. While this task could be done easily with an if-else construction, the second part of the challenge is to write the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7274\">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-7274","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\/7274","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=7274"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7274\/revisions"}],"predecessor-version":[{"id":7306,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7274\/revisions\/7306"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}