6

I am trying to check if 3 sides form a triangle in C++, but the answer for all possible numbers I tried it says wrong...

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c;

    cin >> a >> b >> c;

    if (pow(a,2) == pow(b,2) * pow(c,2) || pow(b,2) == pow(a,2) * pow(c,2) || pow(c,2) == pow(a,2) * pow(b,2))
        cout << "The sides form a triangle" << endl;
    else
        cout << "The sides do not form a triangle." << endl;
    return 0;
}
user1118321
  • 25,567
  • 4
  • 55
  • 86
user2943407
  • 409
  • 3
  • 5
  • 13
  • For example... ? And, um, I don't recognise this as being a valid "triangle-detection mechanism". Right-angled triangles, maybe, though you mean `+` not `*`. You forgot to explain what you mean by "form", and your code has no error checking. – Lightness Races in Orbit Nov 07 '13 at 11:52
  • 3
    I think the sum of any two sides is greater than third side or difference of any two sides is less than third side? is simple way to check it .. – niko Nov 07 '13 at 11:53
  • 2
    Not all triangles have a right angle. – Pontus Gagge Nov 07 '13 at 11:53
  • 1
    I don't think your formula for right angled triangles is right unless I am being stupid and you're doing something different. Pythagoras is a^2 = b^2 + c^2 not times – T. Kiley Nov 07 '13 at 11:53
  • 2
    What's the mathematical reasoning behind your program? (In case if you're checking for a right triangle, that should be `+` instead of `*`) – bereal Nov 07 '13 at 11:53
  • 2
    You have to remember that [`pow`](http://en.cppreference.com/w/cpp/numeric/math/pow) is working with floating point values, so you might want to read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Some programmer dude Nov 07 '13 at 11:54

6 Answers6

20

Let's say that a, b, c is the sides of the triangle. Therefore, it must be satisfy this criteria :

  1. a + b > c
  2. a + c > b
  3. b + c > a

All the criteria must be true. If one of them are false, then a, b, c will not create the triangle.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    // check whether a, b, c can form a triangle
    if (a+b > c && a+c > b && b+c > a)
        cout << "The sides form a triangle" << endl;
    else
        cout << "The sides do not form a triangle." << endl;
    return 0;
}
david
  • 3,225
  • 9
  • 30
  • 43
3

Triangle conditions to check for,

(a + b > c),
(b + c > a),
(c + a > b)
HVar
  • 120
  • 8
  • 1
    I think just three conditions above is enough – david Nov 07 '13 at 12:02
  • @david - Sorry, It seems to have only 3 conditions. I don't know but I remember having to find out differences as well. Seems I am wrong. Have edited the answer – HVar Nov 07 '13 at 12:12
  • @user2943407 - yes, it can be in any order. You need to check for the absolute value only provided all the values are positive – HVar Nov 07 '13 at 12:13
2

For a normal triangle

1. sum of any two sides is greater than third side (or)
2. difference of any two sides is less than third side

hint :  a+b > c || ...

For a right angled triangle

1) sum of the squares of two sides equals the square of the longest side

Hint:

Find the longest side of three sides, that is find longest number in the three..
square the remaining two nums, add them and equate it to square of longest number
niko
  • 9,285
  • 27
  • 84
  • 131
1

Assuming you are only testing for right angled triangles then the logic to use is z^2 = x^2 + y+2 So there's a mistake in the logic:

 if (pow(a,2) == pow(b,2) * pow(c,2) || pow(b,2) == pow(a,2) * pow(c,2) || pow(c,2) == pow(a,2) * pow(b,2))

This should be:

 if (pow(a,2) == pow(b,2) + pow(c,2) || pow(b,2) == pow(a,2) + pow(c,2) || pow(c,2) == pow(a,2) + pow(b,2))

But even with this change the result might be might wrong due to testing equality on floating point numbers. Make a specific function to test 2 floating point numbers are close enough given some tolerance you decide on then use that for comparisons.

If you do not want to limit your approach to only right angled triangles then you might wish to read up on the triangle inequality. In summary the triangle inequality just states that the length of any edge in a triangle must be smaller than the sum of the other 2 edges.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • This answer is mostly okay except in saying that the general logic is correct; it's not, as he's typo'd the formula. – Lightness Races in Orbit Nov 07 '13 at 11:55
  • @LightnessRacesinOrbit, thanks for pointing that out, I missed that the first time as skimmed over it. – shuttle87 Nov 07 '13 at 12:03
  • His initial values are integers. For a simple case like `pow( x, 2 )`, starting with integral values in range _should_ give an exact value; if it doesn't, the quality of the library is pretty bad. If you prefer results guaranteed to be exact, then replace `pow( x, 2 )` with `x * x`. As long as there's no overflow, the results are guaranteed (since only integer arithmetic is involved---but the usual IEEE format also guarantees exact results for `x * x`, as long as the initial values are actually integers, and the results are less than 2^52. – James Kanze Nov 07 '13 at 14:28
0

An efficient approach will be to sort the given sides. This will be efficient if you are given an entire array and you are asked whether the given array elements form a triangle or not. This can be applied for n number of given sides. However, this can also be applied for 3 sides. Suppose the given array is b. In your case array b is of length h=3.

sort(b,b+h);
for (int j=0;j<(h-2);j++){
    if (b[j]+b[j+1]>b[j+2])
    {
return true;
    }
}
else {
return false;
}
Divyanshi
  • 1
  • 2
-1

Actually, given any three sides, you only need to check one condition: that the longest side, say c, is less than the sum of the two shorter sides, say a and b. That is,

if c < a+b {
   return true;
} else return false;

This is the essence of the triangle inequality theorem. The other conditions will be trivially true when it is a triangle and irrelevant if this one condition is not. The key, of course, will be to sort the three sides to find the longest side by using a simple sorting algorithm. The assumption in the code is that the sides have already been sorted so that c is the longest.