Fog Creek Software
Discussion Board




type of triangle

New Question:
  Write a function to return type of triangle. It takes three parameters as double, length of arms

Mark
Thursday, March 20, 2003

Ok.  I haven't totally proved this to myself, but I think it's right.

If you have three sides, a, b <= c, then you can tell what kind of a triangle it is just by comparing a^2 + b^2 with c^2.  Equality means right triangle, c^2 smaller means acute, larger means obtuse.

To see why this works, imagine a circle with diameter coinciding with side c.  What does the location of the opposite vertex tell you about the kind of triangle it is?

Brian
Friday, March 21, 2003

java code

public string triangleType(double a, double b, double c)
{
   
    string equals;
       
    if(a == b) and (b == c) 
    {
        equals = "equaliteral"
    }
    elseif(a>b) and (a>c)
    {
        if( (a*a) == ((b*b) + (c*c)) )
        {
            equals = "right"
        }
        elseif(b == c)
        {
            equals = "isosoceles"
        }
        else
        {
            equals = "scalene"
        }
    }
    elseif(b>a) and (b>c)
    {
        if( (b*b) == ((a*a) + (c*c)) )
        {
            equals = "right"
        }
        elseif(a == c)
        {
            equals = "isosoceles"
        }
        else
        {
            equals = "scalene"
        }
    }
    elseif(c>a) and (c>b)
    {
        if( (c*c) == ((a*a) + (b*b)) )
        {
            equals = "right"
        }
        elseif(a == b)
        {
            equals = "isosoceles"
        }
        else
        {
            equals = "scalene"
        }
    }

    return equals
}

Mark Hall
Friday, March 28, 2003

Sorry, that won't do it. Try this instead.

--

char *szTriangleType(double dfA, double dfB, double dfC)
{
  boolean fRight;
  assert ((dfA > 0.0) && (dfB > 0.0) && (dfC > 0.0));

  // make dfA the hypoteneuse
  if (dfA < df B)
    {
    dfTemp = dfA;
    dfA = dfB;
    dfB = dfTemp;
    }
  if (dfA < df C)
    {
    dfTemp = dfA;
    dfA = dfC;
    dfC = dfTemp;
    }

  if (fabs((dfA*dfA)- ((dfB*dfB) + (dfC*dfC)) < EPSILON)
    return "right";

  if (fabs(dfB - dfC) < EPSILON)
    {
    if (fabs(dfA - dfB) < EPSILON) 
      return "equaliteral";
    else
      return "isoceles";
    }

  return "scalene";
}

X. J. Scott
Friday, March 28, 2003

errata

Ignore the boolean declaration. won't affect correctness by will generate compiler warning

Arg! I copied the type - it's equilateral.

Anyway the basic thing the interviewer is going to check for on this is if you are testing FLOAT_EXPRESSION_A == FLOAT_EXPRESSION_B.

If we see that, then you probably don't have a lot of experience... or you have written a lot of buggy code!

X. J. Scott
Friday, March 28, 2003

double arg!
not type -- typo; equilateral is correction of a typo

X. J. Scott
Friday, March 28, 2003

I don't think that works fir isoceles triangles where the base is shorter than the sides.

Also, the floating-point equality comparison is off.

What happens if A = B = EPSILON * .0001 and C = EPSILON * .0002?

What if A = B = EPSILON * 10000 and C = EPSILON * 20000?

A better check for floating-point equality removes the scale factor:

if (fabs(a-b) * fabs(a) < EPSILON)

Peter Meilstrup
Thursday, April 17, 2003

1> Check if the lengths can form a triangle or not (sum
of two sides greater than 3rd side!!)
2> Start with saying triangle is scalene, then, if
  any two sides same change it to isoceles and if all three
sides same change it to equilateral.
thats it. I guess we don't need any circle stuff here.
Probably 3-4 if() statements shud do the job.

KN
Sunday, April 20, 2003

*  Recent Topics

*  Fog Creek Home