9 Replies Latest reply: Nov 19, 2009 9:54 AM by 801456 RSS

    Help with Boolean

    801456
      Hey guys , hope you all are good , i am in trouble , i want to write a program that reads three edges for a triangle and determines whether the input is valid. The input is valid if the sum of any two edges is greater than the third edge , i compared 2 values before but now how can i compare three values , for example i suggested that if we have variable and assigned 3 values for them like that ....
      int a = 3;
      int b = 4;
      int c = 5;

      and i suggested to write expressions like that to be compared , (a + b > c) , (a + c > b) , (b + c > a) how to write a boolea expression , and if the first expression true so the second one is false and the third one should be what ?

      plesae help

      thanks in advance
        • 1. Re: Help with Boolean
          3004
          To check if both X and Y are true, use
          if (X && Y)
          Edited by: jverd on Nov 19, 2009 7:38 AM
          • 2. Re: Help with Boolean
            843789
            boolean okay(int a,int b,int c) {
                return 0<a && 0<b && 0<c && a<(b+c) && c<(a+b) && b<(a+c);
            }
            • 3. Re: Help with Boolean
              3004
              .
              • 4. Re: Help with Boolean
                843789
                alaaraulo wrote:
                and i suggested to write expressions like that to be compared , (a + b > c) , (a + c > b) , (b + c > a) how to write a boolea expression , and if the first expression true so the second one is false and the third one should be what ?
                Use the AND operator:
                if ( (condition1) && (condition2) && (condition3) ) {
                //...
                }
                Each of your comparisons will be a boolean subexpression that will occupy condition1, 2 or 3.
                • 5. Re: Help with Boolean
                  843789
                  jverd wrote:
                  alaaraulo wrote:
                  and if the first expression true so the second one is false and the third one should be what ?
                  If any one of them is true, you don't care about the other ones.
                  So this is a valid triangle: (a=2,b=4,c=10000000)?
                  After all, a is less than 10000004!
                  • 6. Re: Help with Boolean
                    3004
                    endasil wrote:
                    jverd wrote:
                    alaaraulo wrote:
                    and if the first expression true so the second one is false and the third one should be what ?
                    If any one of them is true, you don't care about the other ones.
                    So this is a valid triangle: (a=2,b=4,c=10000000)?
                    After all, a is less than 10000004!
                    Yeah, sorry, not awake yet. I edited that reply out of existence, but not quickly enough.

                    :-)
                    • 7. Re: Help with Boolean
                      843789
                      jverd wrote:
                      Yeah, sorry, not awake yet. I edited that reply out of existence, but not quickly enough.

                      :-)
                      Way to sneak the correct reply back into position 1 though :-P
                      • 8. Re: Help with Boolean
                        801456
                        many thanks
                        • 9. Re: Help with Boolean
                          843789
                          If 0<a,b,c, at least one can be bigger than the sum of the others: the biggest one.

                          So we can check the signum of (a+b-c).(a+c-b).(-a+b+c) or

                          signum (a+b-c).signum (a+c-b).signum (-a+b+c)

                          Their being positive is equivalent to a,b,c are possible sides for a triangle.

                          Indeed, Heron's formula for the area of the triangle is

                          Area=SQRT(s(s-a)(s-b)(s-c)),

                          where s=(a+b+c)/2 or perimeter/2.

                          http://mste.illinois.edu/dildine/heron/triarea.html