Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > greatest of two numbers

Reply
Thread Tools

greatest of two numbers

 
 
James Fang
Guest
Posts: n/a
 
      12-08-2007
On 12月8日, 下午11时17分, CBFalconer <(E-Mail Removed)> wrote:
> James Fang wrote:
>
> ... snip ...
>
> > My code is directly written in the bbs reply, the purpose of my code
> > is to illustrate new idea and algorithm, rather than help sb directly
> > do copy&paste job. The entire execution code is pasted below, it was
> > compiled with gcc and had basic functionality test on win32 system.

>
> > #include <stdio.h>

>
> > int max(int a,int b) {
> > int array[2];
> > array[0]=a;
> > array[1]=b;
> > return array[((a-b)&0x80000000)>>31];
> > }

>
> > int main() {
> > printf("%d\n",max(-1,100));
> > printf("%d\n",max(10000,100));
> > printf("%d\n",max(99,99));
> > getchar();
> > }

>
> And how does it work on sign magnitude systems, or 16 bit integer
> systems, or any non-32 bit integer systems, etc.? What is the
> useless 'getchar' for? Are there prizes for deleting blanks?
>
> --
> Chuck F (cbfalconer at maineline dot net)
> <http://cbfalconer.home.att.net>
> Try the download section.
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com


hi Chuck,
the getchar() is only used to facilitate test in windows command
prompt(as I don't want the command prompt window disapear after
process exits)

This code is absolutely un-portable due to the limitation of the
question itself. It need to be re-considered for each type of
processor(maybe use a macro to control them).

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      12-08-2007
James Fang wrote:
....
> BTW, since "!=" is also in the Relational Operator Set which is also
> disatisfied with the question requirement, a better way may be like


No, the C standard restricts the term "relational operator" to '<', '>',
'<=' and '>=' (6.5.. '!=' is an equality operator, a category that
also includes '==' (6.5.9).
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-08-2007
"Flash Gordon" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)-gordon.me.uk...
> James Fang wrote, On 08/12/07 13:33:
>> My code is directly written in the bbs reply, the purpose of my code

> I have no idea what you mean by bbs.

I presume he means "Bulletin Board System", in the (wrong) assumption that
Usenet is just that.

Bye, Jojo


 
Reply With Quote
 
James Fang
Guest
Posts: n/a
 
      12-09-2007
On 12月9日, 上午12时24分, Flash Gordon <(E-Mail Removed)> wrote:
> James Fang wrote, On 08/12/07 13:33:
>
>
>
> > On 12月8日, 下午7时22分, Flash Gordon <(E-Mail Removed)> wrote:
> >> James Fang wrote, On 08/12/07 04:23:

>
> >>> On Dec 8, 12:20 pm, James Fang <(E-Mail Removed)> wrote:
> >>>> On Dec 7, 1:33 pm, (E-Mail Removed) wrote:
> >>>>> Hi all,
> >>>>> The following question is asked frequently in interviews
> >>>>> How to find the greatest of 2 numbers without using relational
> >>>>> operators ?
> >>>>> the solution i have seen is
> >>>>> ( a+b + abs(a-b) ) /2 ;
> >>>>> is there any better solution than this ....?????
> >>>> In your solution there's an overflow issue.
> >>>> Actually there's a simpler solution: you can use an array, and use the
> >>>> array index istead of the relational operators.
> >>>> int max(int a,int b)
> >>>> {
> >>>> int array[2];
> >>>> array[0]=a;
> >>>> array[1]=b;
> >>>> return array[(a-b)&0x80000000];
> >>>> }
> >> Did you actually test this piece of rubbish? When I try it I always get
> >> the value of a. I'm really not sure how it is giving me that since the
> >> code is so broken. Just to show you how bad I've added a bit of
> >> diagnostic and here it is...

>
> >> markg@brenda:~$ cat t.c
> >> #include<stdio.h>

>
> >> int max(int a,int b)
> >> {
> >> int array[2];
> >> array[0]=a;
> >> array[1]=b;
> >> printf("%d\n",(a-b)&0x80000000);
> >> return array[(a-b)&0x80000000];

>
> >> }

>
> >> int main(void)
> >> {
> >> printf("%d\n",max(1,2));
> >> return 0;}

>
> >> markg@brenda:~$ gcc -ansi -Wall -Wextra -O t.c
> >> markg@brenda:~$ ./a.out
> >> -2147483648
> >> 1
> >> markg@brenda:~$

>
> >> Obviously the index is just a little outside the array.

>
> >>>> also, you can make use of the C system stack
> >> C does not have a system stack. Your implementation might, but equally
> >> well it might not work as you expect.

>
> >>>> to get rid of extra
> >>>> memory space:
> >>>> int max(int a,int b)
> >>>> {
> >>>> // in C standard, the function parameters are pushed from right to
> >>>> left.
> >> Or they are passed in registers (if you take the address then obviously
> >> it has to allocate a memory location), or the stack might not grow in
> >> the direction you expect, or the parameters might be pushed left to right...

>
> >>>> // so integer b is stored in high address.
> >>>> return array *(&a+((a-b)&0x80000000>>31));
> >> int might not be 32 bits. It invokes undefined behaviour if you take a
> >> pointer to a, add 1 to it, and dereference it.

>
> >>>> }
> >>> Sorry, I've made a mistake, the second implementation should be:
> >>> int max(int a,int b)
> >>> {
> >>> // in C standard, the function parameters are pushed from right to
> >>> left.
> >>> // so integer b is stored in high address.
> >>> return *(&a+((a-b)&0x80000000>>31));
> >>> }
> >> No, the second implementation should be erased not corrected, and anyone
> >> who thinks it is valid needs to learn C. The first could be corrected as
> >> an intellectual exercise, but should never be used in real life.
> >> --
> >> Flash Gordon

>
> > Actually, I just missed one shift operation in the max function
> > because the code was written in a hurry in the bbs reply, attached
> > below is the corrected function int max(int,int).

>
> No, you have several other errors. With assume int is 32 bits as I
> already stated, your code will invoke undefined behaviour due to
> overflow with some values. It may well have other problems, your second
> example certainly does.
>
> > My code is directly written in the bbs reply, the purpose of my code

>
> I have no idea what you mean by bbs.
>
> > is to illustrate new idea and algorithm, rather than help sb directly
> > do copy&paste job. The entire execution code is pasted below, it was
> > compiled with gcc and had basic functionality test on win32 system.

>
> Specific implementations are not relevant.
>
> > #include <stdio.h>

>
> > int max(int a,int b)
> > {
> > int array[2];
> > array[0]=a;
> > array[1]=b;
> > return array[((a-b)&0x80000000)>>31];
> > }

>
> > int main()
> > {
> > printf("%d\n",max(-1,100));
> > printf("%d\n",max(10000,100));
> > printf("%d\n",max(99,99));

>
> #include <limits.h>
> printf("%d\n",max(INT_MIN,INT_MAX);
>
> I get the value of INT_MIN printed which is just a little incorrect.
>
> > getchar();
> > }

>
> Basically the only sensible answer is that it is a stupid question.
> --
> Flash Gordon


You are right, detection of arithmetical overflow with portable
solution should be a problem.
The following code can handle the arithmetical overflow, but it still
assumes that the sign bit is the high bit, which might be non-portable
on some CPUs.
In fact,these processor specific differences should be concealed by
the C compiler,right?

int get_max(int a,int b)
{
long long divisor = (long long)a-(long long)b;
int array[2];
array[0]=a;
array[1]=b;
return array[( divisor & (ULLONG_MAX/2+1U) ) >> ((sizeof(long
long)*-1)];
}


Thanks and Regards,
James
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      12-10-2007
James Fang <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > ...
> > Your entire attempt is stupid, and the OP's requirement
> > is stupid. Your whole attempt is pointless,
> > and encourages stupid programming.


Depends how you look at it. The irony is that you need to
be reasonably skilled to actually get this done portably.
Of course, it's highly unlikely that people asking the
question in an interview are actually in a position to
grade the attempts.

> You are right, I omitted the overflow condition in the
> code, and it can be workarounded as follow:
> int get_max(int a,int b)
> {
> long long divisor = (long long)a-(long long)b;


What if the range of int is the same as long long?

> int array[2];
> array[0]=a;
> array[1]=b;
> return array[( divisor & (ULLONG_MAX/2+1U) ) >> ((sizeof(long
> long)*-1)];
>
> }


The version at...

<http://groups.google.com/group/comp.lang.c/msg/5aeee18e55ea156c>

Should be maximally portable, though it's obviously not
maximally readable.

--
Peter
 
Reply With Quote
 
aarklon@gmail.com
Guest
Posts: n/a
 
      12-17-2007
On Dec 7, 7:52 am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> On Dec 7, 6:33 am, (E-Mail Removed) wrote:
>
> > Hi all,

>
> > The following question is asked frequently in interviews

>
> > How to find the greatest of 2 numbers without using relational
> > operators ?

>
> > the solution i have seen is

>
> > ( a+b + abs(a-b) ) /2 ;

>
> > is there any better solution than this ....?????

>
> Stupid question, anyway :
>
> #include <math.h>
> int maximum(int x,int y){return(x+y+sqrt(-2*y*x +x*x + y*y))/2;}


what about this solution

http://cpuzz.blogspot.com/2005/07/ma...elational.html
 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      12-17-2007
On Dec 17, 2:18 pm, (E-Mail Removed) wrote:
> > > How to find the greatest of 2 numbers without using relational
> > > operators ?

>
> what about this solution
>
> http://cpuzz.blogspot.com/2005/07/ma...thout-relation


I've not been following this discussion, but ...

when a has no side-effects (and is unaffected by
evaluating b), and the net expression's value
is discarded or treated as a boolean,
and missing parentheses are not an issue, then
(a && b || !a && c)
is equivalent to
a ? b : c

I don't know if C's "?" is considered a "relational
operator" but if so, to say that the first expression
above "avoids the relational operator" in the second
seems quite frivolous.

James
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-29-2007
>On Dec 6, 9:33 pm, (E-Mail Removed) wrote:
>> The question [of branchless min and max] is asked frequently
>> in interviews ...


In article <(E-Mail Removed)>
user923005 <(E-Mail Removed)> wrote:
>I think it is moronic to ask for gag answers in an interview. I
>imagine that they were looking for the solutions from this web site:
>http://aggregate.org/MAGIC/


[snippage]

>Writing weird crap like that when you mean:
>maxab = a > b ? a : b;
>is just plain stupid. Since 80% of the cost of software is
>maintenance, writing code that is hard to maintain is counter
>productive.


Indeed. However, questions like that might make sense in interviews
for compiler-writing positions.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Greatest of three numbers Amar Kumar Dubedy C Programming 30 07-04-2007 02:20 AM
OT: Greatest headline ever Briscobar MCSE 29 12-22-2005 09:11 PM
DVD Verdict reviews: THE GREATEST AMERICAN HERO: SEASON TWO and more! DVD Verdict DVD Video 0 05-04-2005 08:13 AM
THE GREATEST VITAMIN IN THE WORLD Susan Richmeier Computer Support 3 04-30-2004 05:00 AM
Bill Gates' Greatest Hits Harrison Computer Support 2 02-20-2004 11:56 AM



Advertisments