comparing numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerico
    New Member
    • Sep 2006
    • 39

    comparing numbers

    Hi.How can the greatest of three numbers be determined without using the comparison operator?Thanks for any help.

    Jerico
  • pukur123
    New Member
    • Sep 2006
    • 61

    #2
    Use the ternary operator (?:)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      max(a, max(b,c))

      Of course that just hides the comparision operator in the standard header files

      Comment

      • jerico
        New Member
        • Sep 2006
        • 39

        #4
        Hi Pukur. Could you show me how to use the ternary operator to get desired result?Thanks.

        Jerico

        Comment

        • pukur123
          New Member
          • Sep 2006
          • 61

          #5
          (condition)? e1:e2;

          If the condition is true then e1 will be evaluated otherwise e2 will be evaluated.

          Let us suppose that a,b,c are the three numbers and the statement for finding the maximum of these three is.....

          max=(a>b)?((a>c )?a:c):((b>c)?b :c);

          Comment

          • dush
            New Member
            • Sep 2006
            • 27

            #6
            hi fellows,

            jerico wondered how to get greatest of 3 numbers without using the comparison operator and I actually see three of them in this pukur123's statement:

            max=(a>b)?((a>c )?a:c): ((b>c)?b:c);

            well, here is my solution (works for unsigned integer values):

            Code:
            int getMax(unsigned a, unsigned b, unsigned c)
            {
            	int m=0;
            	while((a?a--:0)+(b?b--:0)+(c?c--:0)) m++;
            	return m;
            }

            Comment

            Working...