473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

comparing numbers

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

Jerico
Sep 20 '06 #1
5 2322
Use the ternary operator (?:)
Sep 20 '06 #2
Banfa
9,065 Expert Mod 8TB
max(a, max(b,c))

Of course that just hides the comparision operator in the standard header files
Sep 20 '06 #3
jerico
39
Hi Pukur. Could you show me how to use the ternary operator to get desired result?Thanks.

Jerico
Sep 20 '06 #4
(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);
Sep 20 '06 #5
dush
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

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

Similar topics

5
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends,...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
14
by: nw | last post by:
Hi, I'd like to compare 2 floating point numbers within a given error. I'd rather not use a absolute error but one related to the number of values that can be represented between the two floats....
0
by: shana07 | last post by:
I need help on comparing lines in my output file... How to delete the second line in text file (FileReader) and print out the first line for the same fileno? I put here sample of my output file:...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
18
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
18
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
5
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.