trying to simplify an algorithm

Anderson Lizardo lizardo at linuxfromscratch.org
Thu Feb 3 08:36:06 PST 2005


On Thursday 03 February 2005 10:23, Archaic wrote:
> The problem to solve is 3 inputs from the user. The output has to be
> nothing more than equilateral, isosceles, scalene, or not a triangle. My
> algorithm feels a bit clunky and I would like to streamline it if
> possible. The input will be sanitized to allow only int and float (both
> positive and negative) to get this far, so my only concern is just with
> this algorithm.
>
> Any suggestions?

Usually, on these kinds of problem, the inputs are edges, not angles (at least 
my first C++ exercise was using edges :-)), specially because you didn't 
specify (and AFAIK, it's not possible to identify this automatically) if the 
input angles are internal or external to the triangle.

IIRC, the sum of external angles on a triangle is 360° and internal ones is 
180°.

Also note that a equilateral triangle is also scalene, but I suppose for the 
purposes of this "problem" it should classify a equilateral triangle as only 
equilateral.

If you plan to have edges as input (supposing you have a choice ;-), the 
following should work:

if a < b + c and b < a + c and c < a + b then
  if a = b and b = c then { Implies a = c }
      equilateral
  else if a = b or b = c or a = c then
      isosceles
  else
      scalene
else
    not a triangle

-- 
Anderson Lizardo
lizardo at linuxfromscratch.org
http://www.linuxfromscratch.org/




More information about the lfs-chat mailing list