Thread: can you give another logic?

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    50
    if the numbers are non-zero integers we can do this:

    first, convert all the numbers to unsigned integers. This will get us rid of negative numbers.

    then, if floor(x / y) = 0, x < y

    then just do:

    Code:
    int max(int x, int y)
    {
        unsigned signx = (unsigned)x;
        unsigned signy = (unsigned)y;
    
        if ( !(signx / signy) )
            return y;
    
        return x;
    }
    
    int main()
    {
        int x, y, z;
    
        scanf("%d %d %d", &x, &y, &z);
    
        printf("%d", max(max(x, y), z));
        
        return 0;
    }
    If the numbers can be 0, one way would be to check the denominator. If it is 0, return the other number:

    Code:
    int max(int x, int y)
    {
        // 1 = negative, 0 = positive
        unsigned signx = (unsigned)x;
        unsigned signy = (unsigned)y;
    
        if ( signy )
            if ( !(signx / signy) )
                return y;
    
        return x;
    }
    Last edited by Sfel; 01-16-2010 at 03:19 PM.

  2. #17
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe that
    Code:
    unsigned signx = (unsigned)x;
    for signx < 0 will give you a very large number. Which means that it will probably be bigger than any positive number. So you will get actually

    max (abs(x), abs(y), abs(z))

    which might not be the case


    EDIT: I believe there should be a fix like this

    Code:
    int a = -1;
    unsigned fix = (unsigned)a;
    if (a - fix == 0) 
       positive;
    EDIT2: the
    Code:
     if ( signy )
    will sho if it is !=0 not if it is negative
    Last edited by C_ntua; 01-16-2010 at 04:02 PM.

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    50
    True. My method only works if all the numbers have the same sign.

    However, it is easy to check the sign bit and act accordingly: if all the numbers are either positive or negative, my method does work (it's not exactly the maximum of the absolute values, because -1 interpreted as unsigned is bigger than -3 as unsigned for example). If there is a negative number, then the answer is the max of the positives. If there are two negatives, then the answer is the positive.

    Edit to answer your edits:

    Yes, I am checking to see if signy != 0. If it is 0, then signx / signy would crash the program. If it is 0, I just return x. I don't really understand your first edit. Could you explain why you did that please?
    Last edited by Sfel; 01-16-2010 at 04:09 PM.

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Here's one technique that you can use to get you there, which doesn't rely on two's complement representation:
    Code:
    if a mod b equals a, then a is less than b

    Edit: Assuming we're talking about positive numbers here of course.
    Last edited by iMalc; 01-16-2010 at 10:18 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by crocker View Post
    Hi friends,

    i tried to solve the logics for the question " write a c program to find max of 3 numbers with out using ">" and "ternary operators" "
    Why do they not let you use ?: but they allow if/else? That doesn't really make sense.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh my! Someone who still believe that teacher's assignments should make sense!


  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    50
    The easiest way is to just add INT_MAX + 1 to all the numbers, thus making them positive and maintaining their relative order. Then just do if [x / y] = 0 => return y. Otherwise or if y = 0 return x.

    Now what if we're dealing with doubles?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fining the mode in a vector of numbers: Logic errors
    By pantera in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2009, 07:43 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Need help with the logic of the program
    By unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-16-2002, 07:39 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM
  5. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM