Difficulty: ★ ★ ★ ☆
One of the first functions many C programmers code is max(). It returns the largest of two values. Though this operation can also take place by using a simple if-else comparison, it’s a great way to teach how functions work and test various comparison operators. But this task isn’t the challenge for this month’s exercise.
A few months back, the Exercise was to code Heron’s Formula. This formula uses the lengths of a triangle’s three sides to calculate its area.
In the post, I noted that inputting the three side lengths was something that my solution didn’t validate. A comment from Chris Webb stated that you can validate a triangle’s proportions by ensuring that the sum of the two shorter sides is greater than the longest side. This statement got me thinking about measuring the three side values input and determining which is the largest. The process works like the old max() problem, but with three values instead of two.
Your challenge for this month’s Exercise is to write a function, greatest(). This function is fed three random values in the range of 2 to 25. It returns the largest of these three values.
Here’s a sample run from one of my solutions:
The greatest of 7, 21, and 16 is 21
Yes, I wrote two solutions, which is part of this challenge as well: Craft two versions of the greatest() function. The easy version uses if tests. The more difficult version uses the ternary operator. If you can, try to craft a single statement that generates and returns the proper result. Have fun!
I’ll post my solution in a week.
The Mohammed Ali Algorithm.
The return string “Mohammed Ali” would be a valid solution. 🙂