# Solved: reverse triangle inequality

Reverse triangle inequality is an essential concept in both mathematics and computer programming that focuses on the relationship between the lengths of three sides of a triangle. This aspect plays a significant role in mathematical proofs and programming algorithms. In C++, understanding reverse triangle inequality can come in handy in numerous situations, particularly when working with geometrical figures or distances.

The reverse triangle inequality states that for any three sides of a triangle, the length of one side is always less than or equal to the sum of the lengths of the other two sides while being greater than or equal to the absolute difference of the other two sides.

## Implementation in C++

Implementing reverse triangle inequality in C++ is quite straightforward. It involves the application of the mathematical concept into code using logic.

“`c++
#include
#include
using namespace std;

bool checkInequality(int a, int b, int c){
return ((abs(b – c) <= a && a <= (b + c)) && (abs(a - c) <= b && b <= (a + c)) && (abs(a - b) <= c && c <= (a + b))); } int main() { int a, b, c; cout << "Enter three lengths: "; cin >> a >> b >> c;
if(checkInequality(a, b, c))
cout << "The lengths satisfy the reverse triangle inequality"; else cout << "The lengths do not satisfy the reverse triangle inequality"; return 0; } ``` In this code visual C++, `cmath` library is included for the `abs` function, which is used to find the absolute value of the difference of two numbers. The function `checkInequality` is defined to take three integers as arguments (a, b, and c). This function checks if every length is less than or equal to the sum of the other two lengths and greater than or equal to the difference of the other two lengths. The main function gets three lengths from the user and uses the `checkInequality` function to check the conditions.

## Understanding the Algorithm

Understanding the algorithm and logic behind it is vital for its successful implementation. The algorithm is based on the manifestation of the reverse triangle inequality in code.

• Get three lengths, a, b, and c, as input from the user.
• Use the `checkInequality` function to check the conditions for all combinations of the three lengths.
• The `abs` function is used within the `checkInequality` function to find the absolute difference of each pair of lengths.
• If every length is less than or equal to the sum and greater than or equal to the difference of the other two, the three lengths satisfy the reverse triangle inequality.

In the source code demonstrated, we utilized C++ libraries such as `iostream` and `cmath`, which provide functionalities necessary for input/output and mathematical computations, respectively. The keyword `abs` provides the absolute difference, which is indispensable in our calculations. Such libraries and functions help vastly streamline the coding process and incorporate complex functionalities into our program in a comprehendible manner.

Related posts: