By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

comparing numbers

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

Sep 20 '06 #1
Share this Question
Share on Google+
5 Replies

P: 61
Use the ternary operator (?:)
Sep 20 '06 #2

Expert Mod 5K+
P: 8,916
max(a, max(b,c))

Of course that just hides the comparision operator in the standard header files
Sep 20 '06 #3

P: 39
Hi Pukur. Could you show me how to use the ternary operator to get desired result?Thanks.

Sep 20 '06 #4

P: 61
(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.....

Sep 20 '06 #5

P: 27
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):

Expand|Select|Wrap|Line Numbers
  1. int getMax(unsigned a, unsigned b, unsigned c)
  2. {
  3.     int m=0;
  4.     while((a?a--:0)+(b?b--:0)+(c?c--:0)) m++;
  5.     return m;
  6. }
Sep 20 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.