Search code examples
logicinequality

Solving Inequalities In Programming


We are given a variable that have some constraints over its range of value, we have to overall find out a set that denotes it overall range.

For example and conditions are as follows

x< 10 
x> -6 
x>= 0

I can do it on real number line and mark the intersection but how to do it logically in programming.

Note : Only > , >= , < , <= are allowed.

ANSWER=[0, 10)


Solution

  • You have to figure out the logic of your solution, then implement that logic in C++. You say you "can do it", which I assume means you find it "easy" to solve as a humain being. What makes it so easy? Identify the method you're using, then write that method in C++.

    There are two types of inequalities: > and <. Well, there are also <= and >=, but I suggest leaving those aside until you've written a program that handles < and > correctly.

    Imagine you have:

    x > 5
    x > 7
    x > 6
    x < 11
    x < 10
    x < 12.
    

    What is the solution in this case? Try to find the solution without drawing the number line. Then try to describe with words the way you arrived to this solution.

    Then try to write pseudo-code that describes the algorithm more formally.

    Finally, you're ready to write C++ code that performs the same steps. I suggest not trying to write C++ until you have written pseudo-code. When writing C++ you'll encounter a few cumbersome details; for instance, how to parse each expression, such as x < 5, to find out what inequality it is and which number it's comparing x to. These "details" are not uninteresting but they will get in the way of your logic so it's best to keep them for last.