Thread: can you give another logic?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    can you give another logic?

    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" " . For this i got one logic as

    1. first i converted given numbers to strings.

    2. i find the max of two numbers by compaing the strcmp() function library.( strcmp(s1,s2) returns 0 if s1 equal to s1, -1 s2 greater than s1 , 1 s1 is greater than s2)

    3. i used switch case after strcm() library

    from above steps i can find max of 3 numbers


    can any one suggest me another logics .


    i look for your further replys

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by crocker View Post
    strcmp(s1,s2) returns 0 if s1 equal to s1, -1 s2 greater than s1 , 1 s1 is greater than s2)
    can any one suggest me another logics .
    strcmp() DOES NOT work that way.

    You say you can't use > greater than. Can you use < less than?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He has a typo, but yes, strcmp() does *sometimes* work that way. The 1 and -1 is not defined by the standards of C, but are sometimes used by compilers as the returned values. (turbo C does, for instance).

    if your teacher's compiler does NOT use 1 and -1 for these strcmp() returned values, then you're sunk, of course.

    Are you sure that your teacher's compiler's strcmp() will return 1 and -1?
    Last edited by Adak; 01-16-2010 at 10:13 AM.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    hi i think,

    incase of -1 its return any negative value and incase of 1 it return any positive value may correct ?


    i want ur suggestions

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    max(a,b)?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    He has a typo, but yes, strcmp() does *sometimes* work that way.
    Okay, my mistake. In fact it is part of the standard:

    Quote Originally Posted by c99
    7.21.4
    The sign of a nonzero value returned by the comparison functions memcmp, strcmp,
    and strncmp is determined by the sign of the difference between the values of the first
    pair of characters (both interpreted as unsigned char) that differ in the objects being
    compared.
    So that's all good.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by crocker View Post
    hi i think,

    incase of -1 its return any negative value and incase of 1 it return any positive value may correct ?


    i want ur suggestions
    Yes, it's not always just a 1 or a -1.

    So, moving along, I haven't looked into it, but Jeff's idea of using max(), sounded MUCH better, still.

    Let's go with that puppy.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But max() or strcmp() will both involve < > or ?:

    So are you really meeting the objective, by hiding your implied use of these operators?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Salem View Post
    But max() or strcmp() will both involve < > or ?:

    So are you really meeting the objective, by hiding your implied use of these operators?
    Its hard to say; sometimes an instructor will try to get a student to think outside the box and sometimes they are trying to lead them to the obvious answer....only the OP knows for sure...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Whatever you use to sort values, it will imply at some level, a > or <, imo.

    So of course, we're hiding it. I thought that was the name of this game, no?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How about this:

    Use an infinite loop. Add one to each number until one or more of them == INT_MAX. That number is the highest. If two+ numbers equal zero in the same iteration, they are the same number.

    Real inefficient, but there's no < or >.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by MK27 View Post
    How about this:

    Use an infinite loop. Add one to each number until one or more of them == INT_MAX. That number is the highest. If two+ numbers equal zero in the same iteration, they are the same number.

    Real inefficient, but there's no < or >.
    This assumes they are integers, which might not be the case.

    You can simply use bitwise operators for integers. Start searching who has "1" at the highest position.
    You can do soething similar for floats.

    You could also substract and check the one from another. Then do
    result & 10000...
    if the result is 1000.... it is negative, thus the second is bigger. Otherwise the first. You should check also the
    result | 00000...
    and/or
    result | 10000...
    to check for the result being zero. Thus both numbers are equal

    Bitwise operators don't involve the < symbol at all. Neither the >

    Of course, the simple answer to the OP, as already mentioned is to use < :P

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by C_ntua View Post
    You can simply use bitwise operators for integers. Start searching who has "1" at the highest position.
    I thought about that, but won't work very well because it will be too easy for two numbers to tie.

    However, I guess you could keep iterating and the first positive XOR indicates a tie breaker. Maybe that was really the point of the assignment.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by MK27 View Post
    I thought about that, but won't work very well because it will be too easy for two numbers to tie.

    However, I guess you could keep iterating and the first positive XOR indicates a tie breaker. Maybe that was really the point of the assignment.
    True. And you will have to take under consideration how negatives are represented. Usually the first digit shows signs (1 for negative).

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That caffeine must be working on ya'll. Very creative!

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