comare tow int

L

Lew Pitcher

All solutions I've seen on this newsgroup (at least all trivial
solutions) have something wrong. Many of these solutions were
discussed in non-homework threads.

Here is a solution that doesn't suffer from overflow issues. It is neither
very efficient, nor very elegant, but does solve the OP's problem without
encountering chance overflows, depending on specific values for sizeof(int)
or a specific format for negative integers, or invoking additional
functions (either user-coded or C standard library). And, of course, it
does not use any of the OP's "forbidden" operations.

Pick away, pedants ;-)


/*
** count downward from INT_MAX until
** the count matches either a or b
** the count will be the maximum of a or b
*/
int MyMax(int a, int b)
{
int c;

for (c = INT_MAX; (c-a) && (c-b); --c) ;
return c;
}


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
C

CBFalconer

Lew said:
.... snip ...

Here is a solution that doesn't suffer from overflow issues. It
is neither very efficient, nor very elegant, but does solve the
OP's problem without encountering chance overflows, depending on
specific values for sizeof(int) or a specific format for
negative integers, or invoking additional functions (either
user-coded or C standard library). And, of course, it does not
use any of the OP's "forbidden" operations.

Pick away, pedants ;-)

/*
** count downward from INT_MAX until
** the count matches either a or b
** the count will be the maximum of a or b
*/
int MyMax(int a, int b) {
int c;

for (c = INT_MAX; (c-a) && (c-b); --c) ;
return c;
}

Hey, ingenious. Congratulations.
 
P

Phil Carmody

Lew Pitcher said:
Here is a solution that doesn't suffer from overflow issues. It is neither
very efficient, nor very elegant, but does solve the OP's problem without
encountering chance overflows, depending on specific values for sizeof(int)
or a specific format for negative integers, or invoking additional
functions (either user-coded or C standard library). And, of course, it
does not use any of the OP's "forbidden" operations.

Pick away, pedants ;-)


/*
** count downward from INT_MAX until
** the count matches either a or b
** the count will be the maximum of a or b
*/
int MyMax(int a, int b)
{
int c;

for (c = INT_MAX; (c-a) && (c-b); --c) ;
return c;
}

Can this be adapted for sign?

unsigned int yamax(unsigned int a, unsigned int b)
{
unsigned int ma=~(UINT_MAX>>1), mb=ma;
unsigned int r=0;
while(ma|mb)
{
r |= (ma&a)| (mb&b);
ma&= (ma&a)|~(mb&b);
mb&=~(ma&a)| (mb&b);
ma>>=1; mb>>=1;
}
return r;
}

Log-time, and fairly simple due to all the repeated
subexpressions.

Phil
 
K

Kenny McCormack

James Kuyper said:
When you ask such questions, you should expect to receive:

ITYM, when post you to CLC, you should expect to receive:
a) answers that don't do you any good, other than to let you know that
the question was inappropriate.

b) answers which don't actually work, written the deliberate intent of
making it clear to your teacher that you didn't do the work yourself,
but copied an answer you did not actually understand, given to you by
someone who knows a great deal more about C than you do, and who wanted
you to get caught.

c) answers which do work. However, the method they use is so twisted and
complicated that they will still have the same effect as the answers
which don't work: letting your teacher know that you relied on someone
else to do the work.

d) criticism for having asked other people to do your homework, rather
than doing it yourself.

e) other possibilities which would do you even more harm, in the long
run, than any of the above. I will not describe them, so that you may be
properly worried about them.

The rest is spot on - I wouldn't change a thing.
 
G

Guest

Malcolm McLean wrote:

I'm lost.  What is a "tpy employee payroll program"?  Is this some
form of Briticism or Scottishism?

no.
(BTW. the scottish *are* british)
 
G

Guest

It would be more of a help if you sent the answers directly to their
instructors rather than giving them complete answers to plagiarize.

it's a waste of time arguing with him on this one
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top