mardi 31 octobre 2017

Determine if three side lengths form an obtuse triangle c++

I have a program that determines whether or not a triangle is an obtuse triangle. The program instructs the user to input 3 side length values (s1,s2,s3). If the squared value of the smaller two side length values is less than the squared value of the largest side value then it is an obtuse triangle.

For Example:

s1 = 3, s2 = 5, s3 = 9

3^2 + 5^2 < 9^2

34 < 81

This is an obtuse triangle.

I have two versions of my program shown below that both give the same output. My question is which one is more efficient? or is there another version that is more efficient than my two versions?

Version 1:

double s1,s2,s3;
cout << "Enter three numbers -> ";
cin >> s1 >> s2 >> s3;
if (max(max(s1,s2),s3) == s1){
    if(pow(s2,2)+pow(s3,2) < pow(s1,2)){
        cout << "This is an obtuse triangle";
    } else {
        cout << "Not an obtuse triangle";
    }
} else if (max(max(s1,s2),s3) == s2){
    if(pow(s1,2)+pow(s3,2) < pow(s2,2)){
        cout << "This is an obtuse triangle";
    } else {
        cout << "Not an obtuse triangle";
    }
} else {
    if(pow(s1,2)+pow(s2,2) < pow(s3,2)){
        cout << "This is an obtuse triangle";
    } else {
        cout << "Not an obtuse triangle";
    }
}

Version 2:

double s1,s2,s3;
cout << "Enter three numbers -> ";
cin >> s1 >> s2 >> s3;
if( max(s1,s2) < s3){
    if(pow(s1,2) + pow(s2,2) < pow(s3,2)){
        cout << "This is an obtuse triangle";
    } else {
        cout << "Not an obtuse triangle";
    }
} else {
    if(pow(min(s1,s2),2) + pow(s3,2) < pow(max(s1,s2),2)){
        cout << "This is an obtuse triangle";
    } else {
        cout << "Not an obtuse triangle";
    }
}

Aucun commentaire:

Enregistrer un commentaire