440,213 Members | 2,108 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

# Greatest of three numbers

 P: n/a How to find the greatest of three numbers without using any comparison operator or ternary operator?? Jul 1 '07 #1
30 Replies

 P: n/a "Amar Kumar Dubedy"

 P: n/a Amar Kumar Dubedy wrote, On 01/07/07 07:36: How to find the greatest of three numbers without using any comparison operator or ternary operator?? Put them all in a ring, throw weapons in, and wait to see which one comes out alive. If it is homework, do it yourself, if you are doing some stupid quiz, do it yourself. People *might* help if you post an attempt here, but why should we do it for you? -- Flash Gordon Jul 1 '07 #3

 P: n/a Amar Kumar Dubedy said: How to find the greatest of three numbers without using any comparison operator or ternary operator?? Ensure that one of the numbers is LDBL_MAX, and simply return LDBL_MAX from your solving function. That way, you don't need any operators of any kind at all. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 1 '07 #4

 P: n/a "Amar Kumar Dubedy" .... if (a - b == abs(a - b))... (but beware of overflows...) HTH. Jul 1 '07 #5

 P: n/a Army1987 wrote: "Amar Kumar Dubedy" How to find the greatest of three numbers without using any comparisonoperator or ternary operator?? #include ... if (a - b == abs(a - b))... In what way is `==` not a comparison operator? -- Archetype Hedgehog "No-one here is exactly what he appears." G'kar, /Babylon 5/ Jul 1 '07 #6

 P: n/a Army1987 said: > "Amar Kumar Dubedy" How to find the greatest of three numbers without using anycomparison operator or ternary operator?? #include ... if (a - b == abs(a - b))... == can be regarded as a comparison operator. (but beware of overflows...) That's another problem. The Right Thing here is to use comparison operators - specifically the 'greater-than' operator: max = a; if(b max) { max = b; } if(c max) { max = c; } Therefore, the question is mistaken. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 1 '07 #7

 P: n/a In article , Chris Dollin Army1987 wrote: >>How to find the greatest of three numbers without using any comparisonoperator or ternary operator?? >#include ...if (a - b == abs(a - b))... >In what way is `==` not a comparison operator? Perhaps Army was thinking of "relational operator", which C defines as not including the equality operators. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963. Jul 1 '07 #8

 P: n/a In article <11**********************@e9g2000prf.googlegroups. com>, Amar Kumar Dubedy How to find the greatest of three numbers without using any comparisonoperator or ternary operator?? Do you really need to determine which of them it is, or would it be sufficient to return a number equal to the greatest one? -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963. Jul 1 '07 #9

 P: n/a "Richard Tobin" , Chris Dollin >Army1987 wrote: >>>How to find the greatest of three numbers without using any comparisonoperator or ternary operator?? >>#include ...if (a - b == abs(a - b))... >>In what way is `==` not a comparison operator? Perhaps Army was thinking of "relational operator", which C defines as not including the equality operators. if (!(a - b - abs(a - b)) (But beware of disasters...) Jul 1 '07 #10

 P: n/a "Amar Kumar Dubedy"

 P: n/a cr88192 said: int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); } Fails when b is 0. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 1 '07 #12

 P: n/a Richard Heathfield wrote: cr88192 said: [un-snip] >just a guess really (assumes 3 integers 0). >int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); } Fails when b is 0. If b is 0, then you don't have three integers 0. Jul 1 '07 #13

 P: n/a Harald van D?k said: Richard Heathfield wrote: >cr88192 said: [un-snip] >>just a guess really (assumes 3 integers 0). >>int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); } Fails when b is 0. If b is 0, then you don't have three integers 0. In which case we have demonstrated that the assumption is false. :-) Note that this assumption was not present in the OP. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 1 '07 #14

 P: n/a "Richard Heathfield" Richard Heathfield wrote: >>cr88192 said: [un-snip] >>>just a guess really (assumes 3 integers 0).int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); }Fails when b is 0. If b is 0, then you don't have three integers 0. In which case we have demonstrated that the assumption is false. :-) Note that this assumption was not present in the OP. If we allow that the number must be positive integers then it turns into a trival problem of C syntax, which I don't mind helping the OP with even if it is homework. Make them arbitrary floating point numbers and it is a lot more difficult. Specify that the operation must not overflow machine precision for any legal input and I think it might be impossible. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm Jul 1 '07 #15

 P: n/a Malcolm McLean wrote, On 01/07/07 17:38: > "Richard Heathfield" Harald van D?k said: >>Richard Heathfield wrote:cr88192 said:[un-snip]just a guess really (assumes 3 integers 0).int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); }Fails when b is 0.If b is 0, then you don't have three integers 0. In which case we have demonstrated that the assumption is false. :-)Note that this assumption was not present in the OP. If we allow that the number must be positive integers then it turns into a trival problem of C syntax, which I don't mind helping the OP with even if it is homework. Make them arbitrary floating point numbers and it is a lot more difficult. Specify that the operation must not overflow machine precision for any legal input and I think it might be impossible. Since the OP said "numbers" there is no reason to assume only +ve or an integer type. There was also no license given to loose precision (so no floating point arithmetic) or have it fail on some inputs. All of which shows why it was a stupid question. However, there is an easy answer, it just relies on the user of the program answering the questions the program asks correctly. -- Flash Gordon Jul 1 '07 #16

 P: n/a Amar Kumar Dubedy wrote: How to find the greatest of three numbers without using any comparison operator or ternary operator?? if all three are unsigned... #define N (CHAR_BIT * sizeof (unsigned) - 1) int /* returns the carry flag of the sum a+b */ carry_flag (unsigned a, unsigned b) { int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}}; unsigned s= a + b; return cf [a >N] [b >N] [s >N]; } int /* returns 1 iff a < b */ below (unsigned a, unsigned b) { return carry_flag (a + ~b, 1) | carry_flag (a , ~b) } unsigned /* returns the greatest of 3 arguments */ greatest (unsigned a, unsigned b, unsigned c) { unsigned res [2][2][2]; res [0][0][0]= a; /* or any, all equal */ res [0][0][1]= res [0][1][1]= a; res [1][0][0]= res [1][0][1]= b; res [0][1][0]= res [1][1][0]= c; res [1][1][1]= 0; /* should not happen */ return res [below (a,b)] [below (b,c)] [below (c,a)]; } -- regis Jul 1 '07 #17

 P: n/a "Richard Heathfield" int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); } Fails when b is 0. yes, but note, I said, 3 integers 0. that it fails when one of them is 0, is pretty damn obvious, which is why I wrote this as an assumption... now, by adding and subtracting 1 in the right places, it is possible to make this valid for 3 integers >= 0... oh well, the main reason I was here was seing if this was a good place to talk about compiler writing and design (namely, I wrote a C compiler), ..., but it does not look like it, which is why I have not posted on this subject here... -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 3 '07 #18

 P: n/a cr88192 wrote: oh well, the main reason I was here was seing if this was a good place to talk about compiler writing and design (namely, I wrote a C compiler), ..., but it does not look like it, which is why I have not posted on this subject here... You're right, this isn't the best group for that, but you might like the comp.compilers newsgroup for it. Jul 3 '07 #19

 P: n/a Amar Kumar Dubedy

 P: n/a cr88192 said: > "Richard Heathfield" cr88192 said: >>int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); } Fails when b is 0. yes, but note, I said, 3 integers 0. Note that your "solution" thus solved a different problem to the one actually asked. oh well, the main reason I was here was seing if this was a good place to talk about compiler writing and design (namely, I wrote a C compiler), Presumably you were asked to write a Fortran compiler, and wrote at the top of the design spec "assuming Fortran means C..." -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 3 '07 #21

 P: n/a cr88192 said: here seems to be mostly a place for newbs to ask questions... Mostly, alas, that is true. In fact, the quality of the answers usually far exceeds the quality of the questions. OT, basic description of effort: now, my parser, well, it is a big mass of hand-written C code, not much to say about that (I suspect most people with any interest in compiler or interpreter writing, maybe run into the problem of syntax, or get some crude interpreter or translator working, and die off after this point). parser is uniteresting, hard-coded recusive-descent... now, IMO, the parser is the easy part... Well, the lexer is the easy part, actually. The parser is a bit harder than the lexer. Sounds like you're having lots of fun. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 3 '07 #23

 P: n/a cr88192 wrote: > "Harald van D?k" You're right, this isn't the best group for that, but you might like thecomp.compilers newsgroup for it. sadly, it is moderated and filled with people obsessing on parsers... Topics move around. If you want c.c to talk about something else, /post/ about something else. -- Interpreting Hedgehog Notmuchhere: http://www.electrichedgehog.net/ Jul 3 '07 #24

 P: n/a "Richard Heathfield" >here seems to be mostly a place for newbs to ask questions... Mostly, alas, that is true. In fact, the quality of the answers usually far exceeds the quality of the questions. yeah, sad really... >OT, basic description of effort:now, my parser, well, it is a big mass of hand-written C code, notmuch to say about that (I suspect most people with any interest incompiler or interpreter writing, maybe run into the problem of syntax,or get some crude interpreter or translator working, and die off afterthis point).parser is uniteresting, hard-coded recusive-descent...now, IMO, the parser is the easy part... Well, the lexer is the easy part, actually. The parser is a bit harder than the lexer. true, but usually the lexer is easy enough, and done in the same pass as the parser. as a result, I don't usually distinguish them. so, what is the lexer? maybe a few hundred loc?... (source file, 514 loc, 130 going to line-number handling, 95 whitespace and comments, 290 tokenizer). parser? about 3 kloc total... preprocessor, 741 loc; expressions, 764 loc; statements, 429 loc; misc (definitions, args lists, ...), 733 loc. very little actual "mechanics" are implemented by the parser (beyond generating the ASTs). the compiler is split into several stages: upper (still needs a lot of work), 2.4 kloc, converts ASTs to a kind of IL (I call RPNIL or RIL); lower, 6.5 kloc (converts RPNIL to assembler, rather horrible code); assembler, 10.25 kloc (obvious enough what this does...). so, yeah, technically it is fairly small, but a lot of this code can be a lot of effort to write and debug... Sounds like you're having lots of fun. if I am lucky it could actually be useful... -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 3 '07 #25

 P: n/a "Richard Heathfield" >"Richard Heathfield" >cr88192 said:int norz(int a, int b) { return(a*ceil((a/b)*((float)b/a))); }Fails when b is 0. yes, but note, I said, 3 integers 0. Note that your "solution" thus solved a different problem to the one actually asked. ok. yes, 'number' and 'counting-number' are not necissarily the same. >oh well, the main reason I was here was seing if this was a good placeto talk about compiler writing and design (namely, I wrote a Ccompiler), Presumably you were asked to write a Fortran compiler, and wrote at the top of the design spec "assuming Fortran means C..." hmm... actually, this project was done for my own reasons: it could be useful for my larger-scale project... but, it has consumed more time and effort than I had originally expected (I suspect my frequent depressive runs slow me down...). -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 3 '07 #26

 P: n/a Richard Heathfield

 P: n/a On Jul 1, 2:36 am, Amar Kumar Dubedy #include int max(int a, int b, int c) { return fmaxl(fmaxl(a,b),c); } int main(void) { int a= 5; int b= 10; int c= -3; printf("%d\n", max(a,b,c)); return 0; } Is it in theory possible that a long double can't exactly represent the largest possible integer and this will fail? If so, I'm sure someone will say so. I don't use floats much, and lost interest deciding whether DECIMAL_DIG or any of the other set of defines in float.h is relevant here. -David Jul 3 '07 #28

 P: n/a Richard Heathfield >here seems to be mostly a place for newbs to ask questions... Mostly, alas, that is true. In fact, the quality of the answers usually far exceeds the quality of the questions. [...] Well, the opposite would be worse. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jul 3 '07 #29

 P: n/a regis wrote: Amar Kumar Dubedy wrote: >How to find the greatest of three numbers without using any comparisonoperator or ternary operator?? #define N (CHAR_BIT * sizeof (unsigned) - 1) int /* returns the carry flag of the sum a+b */ carry_flag (unsigned a, unsigned b) { int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}}; unsigned s= a + b; return cf [a >N] [b >N] [s >N]; } int /* returns 1 iff a < b */ below (unsigned a, unsigned b) { return carry_flag (a + ~b, 1) | carry_flag (a , ~b) } Hmm, the function below() shown above is in fact above_or_equal(). The function shown below is indeed below() with a clearer logic: int carry_flag (unsigned a, unsigned b, unsigned carry_in) { int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}}; unsigned s= a + b + carry_in; return cf [a >N] [b >N] [s >N]; } int below (unsigned a, unsigned b) { return 1 - carry_flag (a, ~ b, 1); } Jul 4 '07 #30

 P: n/a regis N] [b >N] [s >N]; } return ( (a >1) + (b >1) + (((a & 1) + (b & 1)) >1) ) / (-1u/2+1); -- Peter Jul 4 '07 #31

### This discussion thread is closed

Replies have been disabled for this discussion.