Difficulty: ★ ★ ☆ ☆
Back in October, the C programming Exercise was to code Heron’s Formula. The challenge required the user to input three values, one for each side of the triangle. But, as I learned since, a triangle must have specific side length ratios for it to be a valid triangle.
Call me math-challenged.
Anyhoo, it turns out that for a triangle to be a triangle the length of the longest side must be less than the combined lengths of the other two sides. Further (!) I learned that this rule doesn’t apply to an isosceles triangle or an equilateral triangle, both of which are valid ways to stick three lines together.
(I might also add that the longest side of a triangle is called the hypotenuse only when dealing with a right triangle.)
The challenge for this month’s Exercise is to write code that generates three random numbers in the range of 2 to 25. Examine each of these values to determine whether they could be the lengths of the sides of a valid triangle. Test for an equilateral triangle. Test for an isosceles triangle. Test for an otherwise valid triangle and note the longest side. Test for an invalid triangle.
Fortunately, you don’t need to test whether the triangle is acute, obtuse, or right — though I’d be duly impressed if you take the challenge that far.
Here is output from a few sample runs of my solution:
14 - 7 - 7 is an isosceles triangle 17 - 3 - 16 is a valid triangle 17 is the longest side 2 - 10 - 22 is not a valid triangle
Please try this exercise on your own before you peek at my solution, which I’ll post in a week.