{"id":7337,"date":"2026-01-08T00:01:38","date_gmt":"2026-01-08T08:01:38","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7337"},"modified":"2026-01-03T13:07:47","modified_gmt":"2026-01-03T21:07:47","slug":"is-it-a-real-triangle-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7337","title":{"rendered":"Is It a &#8220;Real&#8221; Triangle? &#8211; Solution"},"content":{"rendered":"<p>The challenge for this <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7328\">month&#8217;s Exercise<\/a> is to generate three random values and determine if they work as sides of a valid triangle. To make this determination, you must consider three types of triangles and test the generated values for each.<br \/>\n<!--more--><\/p>\n<ul>\n<li>An equilateral triangle has three sides of equal length.<\/li>\n<li>An isosceles triangle has two sides of equal length.<\/li>\n<li>A standard triangle has two sides of greater length than the third.<\/li>\n<\/ul>\n<p>A right triangle fits into the last category, though this exercise doesn&#8217;t require you to test whether the triangle is right.<\/p>\n<p>For my solution, the <em>main()<\/em> function generates the three random values: <code>x<\/code>, <code>y<\/code>, and <code>z<\/code>. Various functions are used to determine the triangle types:<\/p>\n<p>The <em>all_equal()<\/em> function returns TRUE when all values are equal, indicating an equilateral triangle.<\/p>\n<p>The <em>two_equal()<\/em> function returns TRUE when two of the values are equal, which represents an isosceles triangle.<\/p>\n<p>The <em>valid_triangle()<\/em> function returns TRUE when the sum of two of the values is larger than the third. This function requires an additional function, <em>longest()<\/em>, which borrows the weirdo ternary solution from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7274\">last month&#8217;s Exercise<\/a>.<\/p>\n<p>Here is the full code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">2026_01-Exercise.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\n<span class=\"comments\">\/* determine whether all sides are equal *\/<\/span>\r\nint all_equal(int a, int b, int c)\r\n{\r\n    if( a==b==c )\r\n        return 1;\r\n    return 0;\r\n}\r\n\r\n<span class=\"comments\">\/* determine whether two sides are equal *\/<\/span>\r\nint two_equal(int a, int b, int c)\r\n{\r\n    if( a==b || a==c || b==c )\r\n        return 1;\r\n    return 0;\r\n}\r\n\r\n<span class=\"comments\">\/* detect the longest side *\/<\/span>\r\nint longest(int a, int b, int c)\r\n{\r\n    return( b&gt;a ? b&gt;c ? b : c : a&gt;c ? a : c );\r\n}\r\n\r\n<span class=\"comments\">\/* validate three uneven sides as a triangle *\/<\/span>\r\nint valid_triangle(int a, int b, int c)\r\n{\r\n    int h;\r\n\r\n    h = longest(a,b,c);\r\n    if( h==a &amp;&amp; a &lt; b+c )\r\n        return 1;\r\n    if( h==b &amp;&amp; b &lt; a+c )\r\n        return 1;\r\n    if( h==c &amp;&amp; c &lt; a+b )\r\n        return 1;\r\n    return 0;\r\n}\r\n\r\nint main()\r\n{\r\n    int x,y,z;\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    <span class=\"comments\">\/* validate the data for a triangle *\/<\/span>\r\n        <span class=\"comments\">\/* equilateral *\/<\/span>\r\n    if( all_equal(x,y,z) )\r\n        printf(\"%d - %d - %d is an equilateral triangle\\n\",x,y,z);\r\n        <span class=\"comments\">\/* isosceles *\/<\/span>\r\n    else if( two_equal(x,y,z) )\r\n        printf(\"%d - %d - %d is an isosceles triangle\\n\",x,y,z);\r\n        <span class=\"comments\">\/* standard *\/<\/span>\r\n    else if( valid_triangle(x,y,z) )\r\n    {\r\n        printf(\"%d - %d - %d is a valid triangle\\n\",x,y,z);\r\n        printf(\"%d is the longest side\\n\",longest(x,y,z) );\r\n    }\r\n        <span class=\"comments\">\/* invalid *\/<\/span>\r\n    else\r\n        printf(\"%d - %d - %d is not a valid triangle\\n\",x,y,z);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code doesn&#8217;t require all the functions listed; only the <em>longest()<\/em> function is called twice. Even so, adding specific functions makes the <em>main()<\/em> function more readable. Here&#8217;s output from a few runs:<\/p>\n<pre>9 - 10 - 20 is not a valid triangle\r\n\r\n19 - 16 - 4 is a valid triangle\r\n19 is the longest side\r\n\r\n16 - 16 - 12 is an isosceles triangle<\/pre>\n<p>To determine a right triangle you apply the Pythagorean theorem to any valid triangle. For example, if <code>a<\/code> is the longest side (hypotenuse) the following is true: <code>if( a*a==b*b+c*c )<\/code> While this test isn&#8217;t that onery to code, the issue is determining which of the sides is the longest; the <em>longest(<\/em>) function returns the longest side, but it doesn&#8217;t tell you which variable is longest: <code>a<\/code>, <code>b<\/code>, or <code>c<\/code>. Doing so just requires more code, which wasn&#8217;t part of the original challenge.<\/p>\n<p>I hope that your solution met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to generate three random values and determine if they work as sides of a valid triangle. To make this determination, you must consider three types of triangles and test the generated values for &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7337\">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-7337","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\/7337","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=7337"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7337\/revisions"}],"predecessor-version":[{"id":7348,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7337\/revisions\/7348"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}