Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-17-2005, 09:44 AM   #1 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 3

purfier

Default C Programming

Can someone please help me with this... Is it possible to compare two numbers without using "if", or conditional operators or relational operators or pre-defined functions in C? I thought of using the bitwise operators but i don't exactly know how to implement it...
purfier is offline  
Old 01-17-2005, 09:08 PM   #2 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 16

mohoo

Default

supposed :A is one numberm,and B is the another one.
the result of |A-B|+|B+A| is the larger one

ps: the symbol ' | |' represents the operation of getting the absolute value .
mohoo is offline  
Old 01-18-2005, 03:28 AM   #3 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

There is nothing wrong with using if statements or anything else. Is this for a school project or are you just curious? Do you want to compare these two number for equality or to see which is larger, or both? That equation does not make sense.
let A = 4 and B = 6
|4-6| + |6+4| = 12 how does that answer tell you which letter it corresponds to. I see that the answer divided by 2 would give you the highest number, but how would you be able to tell if it was A or B without using if statements?
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 01-18-2005, 07:54 AM   #4 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 3

purfier

Default

Actually, its just a puzzle like thing read in some book... And, yes that equation unless divided by two doen't give the largest of the two numbers...

Now, suppose i were given a negative number and a positive number... The equation... it won't work...
For instance, take 3 and -5 then (|3+(-5)| + |3-(-5)|)/2 gives me (|-2| + |8|)/2 which finally reduces to 5...but this is wrong.. The answer should be 3 right??
purfier is offline  
Old 01-18-2005, 07:52 PM   #5 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 16

mohoo

Default

Quote:
Originally posted by gab00n
There is nothing wrong with using if statements or anything else. Is this for a school project or are you just curious? Do you want to compare these two number for equality or to see which is larger, or both? That equation does not make sense.
let A = 4 and B = 6
|4-6| + |6+4| = 12 how does that answer tell you which letter it corresponds to. I see that the answer divided by 2 would give you the highest number, but how would you be able to tell if it was A or B without using if statements?
Thank you for finding my careless mistake.

Yes,that should be divided by 2.

(|A-B|+|A+B|)/2
mohoo is offline  
Old 01-18-2005, 07:58 PM   #6 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 16

mohoo

Default

Quote:
Originally posted by purfier
Actually, its just a puzzle like thing read in some book... And, yes that equation unless divided by two doen't give the largest of the two numbers...

Now, suppose i were given a negative number and a positive number... The equation... it won't work...
For instance, take 3 and -5 then (|3+(-5)| + |3-(-5)|)/2 gives me (|-2| + |8|)/2 which finally reduces to 5...but this is wrong.. The answer should be 3 right??
:eek:it's my mistake.you are right.i havenot taken that instance into account. so sorry.

let me think out it again.
mohoo is offline  
Old 01-18-2005, 08:35 PM   #7 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 16

mohoo

Default

Mmmm,

A+B+|A-B|

does this work?
mohoo is offline  
Old 01-23-2005, 05:13 AM   #8 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 2

stanley_robert

Send a message via Yahoo to stanley_robert
Default

A+B+|A-B| also doesnot work i think
If A=5 and B=7 then
A+B+|A-B| gives 5+7+|5-7| = 12 + 2 = 14
But the answer should be 7 ... right ?

So i think , it should be
(A+B+|A-B|) / 2
which gives 7.. the exact answer
Another example
-5 and 2
(-5+2+|-5-2|)/2
(-3 + 7)/2
4/2
2
which is correct answer...!

I think.. this is the correct procedure !
stanley_robert is offline  
Old 01-23-2005, 06:52 AM   #9 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 3

purfier

Default

Yes... stanley_robert... You are indeed right...got the procedure...thank you very much...
purfier is offline  
Old 01-23-2005, 08:06 PM   #10 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 16

mohoo

Default

oh,my god,i forget '/2' again.
mohoo is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On