problem with triangle angle in programm language C++

121 views Asked by At

could you help me? I am new in programming and in C++

#include <iostream>
#include <cmath>
#include <math.h>
#include <cstdio>

using namespace std;

double SSS(double a, double b, double c){

    double bot1 = -2*b*c;
    double bot2 = -2*c*a;
    if(bot1==0.0 or bot2==0.0){
        return 0.0;
    }


    double alpha = acos((a*a-b*b-c*c)/bot1);

    const double rad = 0.5729577951308232088;


    double beta = acos((b*b-c*c-a*a)/bot2);
    return alpha*rad;
    return beta*rad;

}

int main(){
    cout << SSS(5, 7, 8)<<endl;
}

I would like to get a triangle angle in my window. I don't know where is an error..:(

2

There are 2 answers

3
ruki On

You need cast int a, b,c to float.

int SSS(int a, int b, int c){
    return (int) acos((float)(a*a-b*b-c*c)/(-2*b*c));
}
1
chux - Reinstate Monica On

2) Use FP math and not integer division, recommend double variables.

3) Avoid division by 0.

double SSS(double a, double b, double c) {
  double bottom = -2 * b * c;
  if (bottom == 0) {
    return 0.0;
  } 
  double alpha = acos((a * a - b * b - c * c) / bottom);
  //return alpha;  // This is in radians
  const double r2d = 57.29577951308232088;
  return alpha * r2d;  // This is degrees
}

Ref Law of cosines


If code needs to provide and return an int

int SSS_int_degrees(int a, int b, int c) {
  int bottom = -2 * b * c;
  if (bottom == 0) {
    return 0;
  } 
  double alpha = acos((1.0 * a * a - 1.0 *b * b - 1.0 *c * c) / bottom);
  const double r2d = 57.29577951308232088;
  return lround(alpha * r2d);  // This is degrees
}