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

3
  • Is 1.57 the expected output? Commented Nov 5, 2018 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. Commented Nov 5, 2018 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... Commented Nov 6, 2018 at 15:50

2 Answers 2

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));
}
Sign up to request clarification or add additional context in comments.

3 Comments

So (int) 0.59 (0) is the answer the OP is looking for the example triangle?
Thanks for your answer. I tried, I got 0 in output, but I need 34°.
@KG.Boys If you need an answer in degrees, then you should specify that in the question. The C++ math library works with radians.
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
}

1 Comment

I changed my Code and I want to get alpha and beta. but it didn't work :(. Did I something wrong?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.