-1

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..:(

KG. Boys
  • 43
  • 6
  • Is `1.57` the expected output? – zzxyz Nov 05 '18 at 23:57
  • We don't really know what the error is, or what your expected output is. Personally, I would expect the output from a function called SSS to be three angles. – zzxyz Nov 05 '18 at 23:59
  • my output is 0.382132, it is true and it is output for alpha, but I need output for alpha and for beta. I got in output only for alpha... – KG. Boys Nov 06 '18 at 15:50

2 Answers2

1

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));
}
ruki
  • 183
  • 6
1

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
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256