0

I created an algorithm to calculate the type of triangle with three given sides a, b and c (given in any order) after an assignment on Codewars Link

My idea was to create a function int triangleType(int a, int b, int c) which takes a, b and c as input and returns: "0" for no triangle, "1" for an acute triangle, "2" for a right triangle and "3" for an obtuse triangle.

The requirements for a triangle are met if (a < (b + c) && b < (a + c) && c < (a + b)){/*...*/}. To evaluate the triangle type one first needs the angles of the triangle so i calculated the angles with the <math.h> function acos(), (however it took me a painstakingly long time to realize that outputs a radiant result... after that i converted it with the constant 57.2958 from rads to degrees)

After calculating the angles i simply tried to assess whether all the angles are smaller than 90 degrees (-> acute triangle), or if one of them is 90 degrees (right triangle) or one of them is bigger than 90 degrees (-> obtuse triangle).

Troubleshooting some syntax it all worked but one test:

printf("%d\n", triangleType(3, 4, 5)); // right triangle, expected output: 2

For troubleshooting, I let the program write me the calculated angles as well:

printf("The triangle is right. The angles are alpha = %.4lf, beta = %.4lf, gamma = %.4lf.\n", alpha, beta, gamma);

Now comes the weird part: The output for triangleType(3,4,5) is: "1 -> The triangle is acute. The angles are alpha = 36.8699, beta = 53.1301, gamma = 90.0000. " So the program doesnt even jump to the if(angle == 90 ...) statement because it is certain that gamma, which was just accurately calculated to be 90.000 is SMALLER than 90?!

I feel like i just oversaw something trivial but i could not find the answer.

Here is the code (I hope it is readable im just starting out...)

#include <stdio.h>
#include <string.h>
#include <math.h>

#define RAD_T_DEG 57.2958
#define TOTAL_DEG 180
#define PI 3.141592653589793238
#define RIGHT_ANGLE 90.0000

int triangleType(int a, int b, int c);

int main(void)
{

    printf("%d\n", triangleType(7, 3, 2));  // not a triangle 0
    printf("%d\n", triangleType(2, 4, 6));  // not a triangle 0
    printf("%d\n", triangleType(8, 5, 7));  // acute 1
    printf("%d\n", triangleType(3, 4, 5));  // right 2
    printf("%d\n", triangleType(7, 12, 8)); // obtuse 3

    return 0;
}
int triangleType(int a, int b, int c)
{

    if (a < (b + c) && b < (a + c) && c < (a + b)) // if the requirements of a triangle are met calculate it
    {
        double alpha, beta, gamma;
        alpha = RAD_T_DEG * acos((double) (b*b+c*c-a*a)/(2*b*c)); //convert alpha in radiants to degrees
        beta = RAD_T_DEG * acos((double) (a*a+c*c-b*b)/(2*a*c));
        gamma = TOTAL_DEG - (alpha+beta);

        if(alpha < RIGHT_ANGLE && beta < RIGHT_ANGLE && gamma < RIGHT_ANGLE){
            printf("The triangle is acute. The angles are alpha = %.4lf, beta = %.4lf, gamma = %.4lf.\n", alpha, beta, gamma);
            return 1;
        }
        else if(alpha == RIGHT_ANGLE || beta == RIGHT_ANGLE || gamma == RIGHT_ANGLE){
            printf("The triangle is right. The angles are alpha = %.4lf, beta = %.4lf, gamma = %.4lf.\n", alpha, beta, gamma);
            return 2;
        }
        else if(alpha > RIGHT_ANGLE || beta > RIGHT_ANGLE || gamma > RIGHT_ANGLE){
            printf("The triangle is obtuse. The angles are alpha = %.4lf, beta = %.4lf, gamma = %.4lf.\n", alpha, beta, gamma);
            return 3;
        }
    }
    else // if it is not a triangle return 0
    {
        printf("No triangle can be formed with the given integer values for the sides.\n");
        return 0;
    }
}

Ortwinius
  • 1
  • 1
  • 1
    Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step ⏯️ carefully ahead and watch what happens leading up to that point? – tadman Aug 10 '23 at 23:31
  • 1
    Are you sure that's actually that value, and not just rounded to *appear to be* that value? Floating point numbers are notoriously fussy and can often be off by infinitesimal amounts that make them seem larger or smaller than they should be. I'd be very wary of asserting any calculation you perform is *exactly equal* to any particular value. It will likely be close to one part in N, where you can pick a *gamma* +/- value to be "close enough" with. (Here "gamma" is a conventional name for such a constant, not to be confused with your angle gamma) – tadman Aug 10 '23 at 23:31

0 Answers0