How to find the greatest of two numbers without using the comparison operators? - Programming Languages

This is a discussion on How to find the greatest of two numbers without using the comparison operators? - Programming Languages ; On Fri, 31 Aug 2007 06:41:03 -0000, Aparajita <aparajita.mohanty> wrote: [color=blue] >Hi, > >I want to find the greatest of two given numbers say 'A' and 'B. >The condition is that I should use the IF clause but not comparison ...

+ Reply to Thread
Page 2 of 19 FirstFirst 1 2 3 4 12 ... LastLast
Results 11 to 20 of 184

How to find the greatest of two numbers without using the comparison operators?

  1. Default Re: How to find the greatest of two numbers without using the comparison operators?

    On Fri, 31 Aug 2007 06:41:03 -0000, Aparajita
    <aparajita.mohanty> wrote:
    [color=blue]
    >Hi,
    >
    >I want to find the greatest of two given numbers say 'A' and 'B.
    >The condition is that I should use the IF clause but not comparison
    >operators like '<', '>','=' etc.
    >Is there any other operator in COBOL by which we can compare two
    >numbers.
    >
    >Thanks!
    >Aparajita[/color]

    Can't use operators? How about words? You know like:

    IF A IS GREATER THAN B
    Then do something.

    Regards,
    ////
    (o o)
    -oOO--(_)--OOo-

    "I'm not afraid of dying. I just don't want to
    be there when it happens."
    --Woody Allen
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Remove nospam to email me.

    Steve

  2. Default Re: How to find the greatest of two numbers without using the comparison operators?

    On Fri, 31 Aug 2007 13:09:34 GMT, "Michael Mattias"
    <mmattias@talsystems.com> wrote:
    [color=blue]
    >
    >
    >"Aparajita" <aparajita.mohanty> wrote in message
    >news:1188561799.566239.84930@22g2000hsm.googlegroups.com...[color=green][color=darkred]
    >>> > Hi,
    >>>
    >>> > I want to find the greatest of two given numbers say 'A' and 'B.
    >>>[/color][/color]
    >or your response.[color=green]
    >> I got an alternate solution,
    >> like this
    >> IF A-B IS POSITIVE
    >> DISPLAY "A IS GREATER"
    >> ELSE
    >> DISPLAY "B IS GREATER"
    >> END-IF.
    >>
    >> What is your opinion on the above solution? Or if you find any
    >> limitations or constraints with this code, please let me know.[/color]
    >
    >I use three (3) tests for code:
    >
    >1. Works correctly?
    >2. Relatively efficient?
    >3. Maintainable?
    >
    >If 'yes' to all, it's a keeper.
    >
    >
    >MCM
    >[/color]

    You need a 4th in this case Michael.

    4. Is this what the teacher wanted?

    Regards,
    ////
    (o o)
    -oOO--(_)--OOo-

    "I'm not afraid of dying. I just don't want to
    be there when it happens."
    --Woody Allen
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Remove nospam to email me.

    Steve

  3. Default Re: How to find the greatest of two numbers without using the comparison operators?

    In article <0qXBi.233753$Bo7.185140@fe07.news.easynews.com>,
    William M. Klein <wmklein@nospam.netcom.com> wrote:[color=blue]
    >Pete,
    > Usually I am "amused" by DD's "do your own homework" and don't follow up on
    >it. However, when I first saw this question last night, it had ME stumped.[/color]

    Ahhhhh... might this be similar to 'the wisdom of Lao Tsu, confounded by
    the question of a dolt'?

    DD

  4. Default Re: How to find the greatest of two numbers without using thecomparison operators?

    If you didn't need to use the IF statement, I can think of a number of
    (ridiculously expensive) ways to do it. (I was thinking of these when I wasn't
    looking at the original assignment where we knew the items were numeric)

    - Create a temp file and SORT it
    - Create an indexed file (and see what entry you get first)
    - Put them into a table and use SEARCH (I am not even certain this could work)

    And then there is my favorite (that actually meets the original assignment)

    - use Object Orientation and create a method for comparing two items, e.g.

    If whatever::"compare" (Field-A Field-B) Numeric
    Then Display "Field-A > Field-B)


    .... where the compare method returns zero if the first operand is > than the
    second
    it returns spaces if the 2nd operand is > than the first
    - it returns high-values if they are equal (so you would need to do a 2nd test)

    ***

    Don't you think THAT would impress the teacher <G>

    --
    Bill Klein
    wmklein <at> ix.netcom.com
    "Rick Smith" <ricksmith@mfi.net> wrote in message
    news:13dggs41m0sc4c@corp.supernews.com...[color=blue]
    >
    > "Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> wrote in message
    > news:46D7E17F.6F0F.0085.0@efirstbank.com...
    > [snip][color=green]
    >> Not sure why you need this, but perhaps FUNCTION MAX could give you what[/color]
    > you[color=green]
    >> want?
    >>
    >> COMPUTE MAX = FUNCTION MAX(A B)[/color]
    >
    > Another intereting approach might be:
    > -----
    > evaluate
    > function ord-max(A B) - function ord-min(A B)
    > when 1
    > display "A is less than B"
    > when 0
    > display "A is equal to B"
    > when -1
    > display "A is greater than B"
    > end-evaluate
    > -----
    >
    > A and B may be either numeric or alphanumeric, as long as
    > they are the same.
    >
    >
    >[/color]



  5. Default Re: How to find the greatest of two numbers without using the comparison operators?

    On Fri, 31 Aug 2007 16:34:04 GMT, "William M. Klein"
    <wmklein@nospam.netcom.com> wrote:
    [color=blue]
    >Therefore, I could think of ways to do it with
    >words (not symbols) and without an IF (using intrinsic function MAX), or with
    >only = (not > or <);[/color]

    I once had a requirement to use words, because our print train was
    missing one of the above symbols. Fortunately that need is long
    past.

  6. Default Re: How to find the greatest of two numbers without usingthecomparison operators?

    >>> On 8/31/2007 at 10:32 AM, in message
    <13dggs41m0sc4c@corp.supernews.com>,
    Rick Smith<ricksmith@mfi.net> wrote:
    [color=blue]
    > "Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> wrote in message
    > news:46D7E17F.6F0F.0085.0@efirstbank.com...
    > [snip][color=green]
    >> Not sure why you need this, but perhaps FUNCTION MAX could give you what[/color]
    > you[color=green]
    >> want?
    >>
    >> COMPUTE MAX = FUNCTION MAX(A B)[/color]
    >
    > Another intereting approach might be:
    > -----
    > evaluate
    > function ord-max(A B) - function ord-min(A B)
    > when 1
    > display "A is less than B"
    > when 0
    > display "A is equal to B"
    > when -1
    > display "A is greater than B"
    > end-evaluate
    > -----
    >
    > A and B may be either numeric or alphanumeric, as long as
    > they are the same.[/color]

    This whole discussion makes me wonder if the OP is trying to win an
    "Obfuscated Cobol" contest!

    :-)


  7. Default Re: How to find the greatest of two numbers without using the comparison operators?



    "William M. Klein" <wmklein@nospam.netcom.com> wrote in message
    news:0qXBi.233753$Bo7.185140@fe07.news.easynews.com...[color=blue]
    > Pete,
    > Usually I am "amused" by DD's "do your own homework" and don't follow up
    > on it. However, when I first saw this question last night, it had ME
    > stumped. Certainly, it HAD TO BE homework (because I can't think of any
    > "good" reason to make this restriction in a business environment). Also,
    > when I was thinking about it, I was NOT looking at the original question,
    > so I didn't remember that the two fields were numeric. Therefore, I could
    > think of ways to do it with words (not symbols) and without an IF (using
    > intrinsic function MAX), or with only = (not > or <); but I couldn't come
    > up with a solution that actually met the requirement. I am glad you gave
    > the hint and others more on the solution.[/color]

    Normally, I endorse the policy of not doing homework for people.

    But sometimes, even if it IS homework, a helping hand, or a pointer in the
    right direction does not go amiss.

    In this case, I remembered a 19 year old trying to get to grips with the
    assembler on a certain computer and to understand the whole architecture and
    how it worked, at a time when computers were not part of the scenery and
    embedded in the lives of all of us.
    It had instructions like "CFM" (Compare for magnitude...), "CFE" (Compare
    for equality).

    The clues came when I looked at the Multiply and Divide instructions
    (Macros) and realised they were repetitive addition and subtraction.

    Then in a flash of cognition I realised that that was ALL this "wonderful"
    machine could do... arithmetic. Nothing more. Everything else was derived
    from that. (Later I found out more, and got to grips with the reality of
    binary and Boolean operations, which at the lowest level are all that is
    required. I once wrote a program (for a bet) which read cards and printed,
    using ONLY Logical instructions AND, OR, NOT, and XOR...it was a good
    learning experience.)

    Nowadays, we've come a long way and both hardware, software, and bioware are
    "smarter" than in those far off misty days... but the excitement I got from
    figuring how it did it, has never been forgotten and I thought someone else
    might benefit from that too.

    Sadly, as is often the case today, the OP simply picked up a solution from
    someone who was happy to provide it, without really bothering to think about
    it.

    It seems to me that thinking is no longer "fashionable" and solutions via
    the line of least resistance are more the order of the day. I'm not
    suggesting we should make our lives more difficult, but thought can often
    lead to understanding, and understanding can often lead to wisdom. (Of
    course, if you have no place for wisdom in your life then there is no real
    requirement for thinking, either... For myself, I find the acquisition of
    wisdom by personal growth to be a very useful survival tool... I've never
    been conned in a pyramid scam, for example :-))

    Around four thousand years ago, before the distractions and pressures of
    modern life, TV, movies, travel, entertainment... people used to think.
    (Human brains are quite well adapted for this...) They worked out the
    distance from the Earth to the Sun just by sticking sticks in the sand on a
    beach and observing the shadows. This calculation was correct to within 5%.
    No reason for it; they weren't planning on going there, just an
    "interesting" intellectual exercise.

    Unfortunately, this knowledge and, much more importantly, the attitude that
    nurtured it, was lost in the Dark ages and the advent of Religion, which
    taught that the Universe behaved the way the Church said it did, and there
    was therefore no need for people to think about it. The wise teachings of a
    few enlightened people were submerged into commercializing the "Faith" and
    the resulting parodies of the original teaching set us back thousands of
    years in our development, and are still affecting our lives today. Even in
    this "enlightened" age, when we can perform technical miracles, there are
    still people prepared to kill themselves and others for the sake of their
    imaginary friend, who is more powerful that anyone else's imaginary friend.

    As you may have guessed, it is a quiet Saturday here.No one has driven a car
    loaded with explosives into the local market, there are monarch butterflies
    re-enacting the Battle of Britain around the newly arrived pink blossoms on
    my nectarine tree, I can go to the shops with the certainty that my chances
    of being shot are less than those of my being hit by a meteor, and the fact
    that there is a Salvation Army church on the corner of my street does not
    offend my Atheist sensibilities one jot or tittle. (I admire the work they
    do.)

    It IS possible for us to "get along".

    But we need to think about it.

    Pete
    --
    "I used to write COBOL...now I can do anything."



  8. Default Re: How to find the greatest of two numbers without using thecomparison operators?



    "Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> wrote in message
    news:46D7E17F.6F0F.0085.0@efirstbank.com...[color=blue][color=green][color=darkred]
    >>>> On 8/31/2007 at 12:41 AM, in message[/color][/color]
    > <1188542463.524263.317700@i38g2000prf.googlegroups.com>,
    > Aparajita<aparajita.mohanty> wrote:[color=green]
    >> Hi,
    >>
    >> I want to find the greatest of two given numbers say 'A' and 'B.
    >> The condition is that I should use the IF clause but not comparison
    >> operators like '<', '>','=' etc.
    >> Is there any other operator in COBOL by which we can compare two
    >> numbers.[/color]
    >
    > Not sure why you need this, but perhaps FUNCTION MAX could give you what
    > you
    > want?
    >
    > COMPUTE MAX = FUNCTION MAX(A B)
    >
    > Well, I guess there's no IF in that.
    >
    > I guess the POSITIVE usage others have given is the answer.
    >[/color]

    No, the SIGN test is the answer. (POSITIVE is only one aspect of it...)

    Pete.
    --
    "I used to write COBOL...now I can do anything."



  9. Default Re: How to find the greatest of two numbers without using thecomparison operators?



    "Rick Smith" <ricksmith@mfi.net> wrote in message
    news:13dggs41m0sc4c@corp.supernews.com...[color=blue]
    >
    > "Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> wrote in message
    > news:46D7E17F.6F0F.0085.0@efirstbank.com...
    > [snip][color=green]
    >> Not sure why you need this, but perhaps FUNCTION MAX could give you what[/color]
    > you[color=green]
    >> want?
    >>
    >> COMPUTE MAX = FUNCTION MAX(A B)[/color]
    >
    > Another intereting approach might be:
    > -----
    > evaluate
    > function ord-max(A B) - function ord-min(A B)
    > when 1
    > display "A is less than B"
    > when 0
    > display "A is equal to B"
    > when -1
    > display "A is greater than B"
    > end-evaluate
    > -----
    >
    > A and B may be either numeric or alphanumeric, as long as
    > they are the same.
    >[/color]
    A nice solution Rick. Good job.

    (At least you thought about it ... :-))

    Pete
    --
    "I used to write COBOL...now I can do anything."



  10. Default Re: How to find the greatest of two numbers without using thecomparison operators?



    "William M. Klein" <wmklein@nospam.netcom.com> wrote in message
    news:_7ZBi.297833$rk4.155800@fe09.news.easynews.com...[color=blue]
    > If you didn't need to use the IF statement, I can think of a number of
    > (ridiculously expensive) ways to do it. (I was thinking of these when I
    > wasn't looking at the original assignment where we knew the items were
    > numeric)
    >[/color]
    Bill, it doesn't matter if the operands are numeric or not. It is the
    principle of doing "arithemetic" (in the sense that the ALU does
    "arithmetic") on them, and testing the resulting sign that matters.
    (Although I really liked Rick's Evaluate approach as well...) It is not for
    nothing that the the heart of the CPU is an "Arithmetic Logic Unit"... :-)
    [color=blue]
    > - Create a temp file and SORT it
    > - Create an indexed file (and see what entry you get first)
    > - Put them into a table and use SEARCH (I am not even certain this could
    > work)
    >
    > And then there is my favorite (that actually meets the original
    > assignment)
    >
    > - use Object Orientation and create a method for comparing two items, e.g.
    >
    > If whatever::"compare" (Field-A Field-B) Numeric
    > Then Display "Field-A > Field-B)[/color]

    C# has almost exactly such a method... as a kind of "blanket" for cases
    where strings need comparison and the usual innate methods are unsuitable
    :-) (Fortunately, you only need to use it rarely...:-))[color=blue]
    >
    >
    > ... where the compare method returns zero if the first operand is > than
    > the second
    > it returns spaces if the 2nd operand is > than the first
    > - it returns high-values if they are equal (so you would need to do a 2nd
    > test)
    >
    > ***
    >
    > Don't you think THAT would impress the teacher <G>
    >[/color]

    I think that, in the unlikely event the teacher reads this thread, he/she
    will run screaming from the room... :-)

    Pete
    --
    "I used to write COBOL...now I can do anything."



+ Reply to Thread
Page 2 of 19 FirstFirst 1 2 3 4 12 ... LastLast

Similar Threads

  1. greatest of two numbers
    By Application Development in forum C
    Replies: 37
    Last Post: 12-29-2007, 06:29 PM
  2. Combinations of numbers/operators?
    By Application Development in forum RUBY
    Replies: 6
    Last Post: 08-25-2007, 03:35 PM
  3. automatic type conversion for comparison operators
    By Application Development in forum Python
    Replies: 3
    Last Post: 07-26-2007, 11:13 PM
  4. type conversions for comparison operators
    By Application Development in forum Python
    Replies: 1
    Last Post: 07-18-2007, 09:38 PM
  5. Style or speed with comparison operators sequences?
    By Application Development in forum c++
    Replies: 10
    Last Post: 02-21-2007, 05:16 AM