0

I'm making a program that reads each side of a triangle, calculates its degrees and determines whether it's a right triangle, a scalene triangle or not a triangle.

I'm calculating its cosine then find the degree from the cosine: the cos equation

Here's the code:

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int a,b,c;
double val, cA, cB, cC, angleA, angleB, angleC;

int main()
{
    printf("Input the length of each side!");
    printf("\na: ");
    scanf("%d", &a);
    printf("\nb: ");
    scanf("%d", &b);
    printf("\nc: ");
    scanf("%d", &c);

    //calculating the cosine
    cA = ((b*b)+(c*c)-(a*a))/(2*b*c);
    cB = ((a*a)+(c*c)-(b*b))/(2*a*c);
    cC = ((a*a)+(b*b)-(c*c))/(2*a*b);

    val = 180.0/PI;

    if(cA>=0&&cA<=1){
        angleA = acos(cA)*val;
    } else{
        angleA = 0;
    }

    if(cB>=0&&cB<=1){
        angleB = acos(cB)*val;
    } else{
        angleB = 0;
    }

    if(cC>=0&&cC<=1){
        angleC = acos(cC)*val;
    } else{
        angleC = 0;
    }

    printf("\ncos A= %lf", cA);
    printf("\ncos B= %lf", cB);
    printf("\ncos C= %lf", cC);     

    printf("\nAngle A= %lf", angleA);
    printf("\nAngle B= %lf", angleB);
    printf("\nAngle C= %lf", angleC);
}

I'm still trying to calculate the cosine, but the cA, cB, cC only gives zero whenever I input the side values of a triangle.

cos A= 0.000000
cos B= 0.000000
cos C= 0.000000

How to get the cos equation to return the correct value of the cosine?

  • 3
    so many problems with your code `((b^2)+(c^2)-(a^2))/(2*b*c)`: [`^` is not a power operator](https://stackoverflow.com/q/4843304/995714), and [`(a^2))/(2*b*c)` is not a floating-point division](https://stackoverflow.com/q/8906722/995714). And just use `M_PI` instead of your defined constant, which is far lower than the precise value that double can store – phuclv Mar 25 '18 at 03:29
  • @LuLưu Vĩnh Phúc I initially used `(b*b)+(c*c)-(a*a)` and it's still not working. – W. Mustikarini Mar 25 '18 at 03:31
  • `(b*b)+(c*c)-(a*a)` is obviously not the same as the mathematical expression `((b^2)+(c^2)-(a^2))/(2*b*c)` – phuclv Mar 25 '18 at 03:32
  • Yes it's not. If you don't mind to see the reference of the equation that I put above, the equation for the cosine is `((b*b)+(c*c)-(a*a))/(2*b*c)`. – W. Mustikarini Mar 25 '18 at 03:34
  • use `b*b + c*c - a*a/(2.0*b*c)` – phuclv Mar 25 '18 at 03:35
  • @LưuVĩnhPhúc `(b*b + c*c - a*a)/(2.0*b*c)` still i think, need parentheses – Anders Mar 25 '18 at 03:36
  • Curious, why use `#define PI 3.14159265` and not something more precise like `#define PI 3.1415926535897932384626433832795` or even `#define PI (acos(-1))`? – chux - Reinstate Monica Mar 25 '18 at 04:11
  • @Anders yes. The OP's excessive use of unnecessary parentheses made me confuse – phuclv Mar 25 '18 at 04:47

2 Answers2

0

In lines like:

cA = ((b^2)+(c^2)-(a^2))/(2*b*c);

You do the calculations on ints and save the result to double. This means you could end up with an intermediate result like 8 / 10 which is just 0 rather than a fraction.

But the bigger issue is that ^ is an XOR operation rather than what you think. Either use b*b, or pow(b, 2).

viraptor
  • 33,322
  • 10
  • 107
  • 191
0

So I changed a little bit to solve the division resulting zeros:

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

double a,b,c;
double val, cA, cB, cC, angleA, angleB, angleC;

int main()
{
    printf("Input the length of each side!");
    printf("\na: ");
    scanf("%lf", &a);
    printf("\nb: ");
    scanf("%lf", &b);
    printf("\nc: ");
    scanf("%lf", &c);

    //calculating the cosine
    cA = ((b*b)+(c*c)-(a*a))/(2*b*c);
    cB = ((a*a)+(c*c)-(b*b))/(2*a*c);
    cC = ((a*a)+(b*b)-(c*c))/(2*a*b);

    val = 180.0/PI;

    if(cA>=0&&cA<=1){
        angleA = acos(cA)*val;
    } else{
        angleA = 0;
    }

    if(cB>=0&&cB<=1){
        angleB = acos(cB)*val;
    } else{
        angleB = 0;
    }

    if(cC>=0&&cC<=1){
        angleC = acos(cC)*val;
    } else{
        angleC = 0;
    }

    printf("\ncos A= %lf", cA);
    printf("\ncos B= %lf", cB);
    printf("\ncos C= %lf", cC);     

    printf("\nAngle A= %lf", angleA);
    printf("\nAngle B= %lf", angleB);
    printf("\nAngle C= %lf", angleC);       
}

I guess the integer input doesn't work, so I changed it to double.