# 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 ; 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 '&lt;', '&gt;','=' etc. Is there any other operator in COBOL ...

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

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

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

Aparajita <aparajita.mohanty> wrote:[color=blue]
>Hi,
>
>I want to find the greatest of two given numbers say 'A' and 'B.[/color]

DD

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

"Aparajita" <aparajita.mohanty> wrote in message
> Hi,
>
> I want to find the greatest of two given numbers say 'A' and 'B.[/color]

That would be the "greater" of two numbers; the "greatest" implies at least
three...
[color=blue]
> 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]
No there isn't.

But what you want CAN be done.

a clue: Check out the COBOL SIGN test.

Then think about how a computer is able to make comparisons. How would a
"compare" instruction (on any platform) "work"? If you had to build a
computer, how would you build a "compare" instruction? Given that all you
can do is arithmetic and sign checking, how would you implement a "compare"?

Post your thoughts here, and we'll see how you go.

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

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

On Aug 31, 3:28 pm, "Pete Dashwood"
<dashw...@removethis.enternet.co.nz> wrote:[color=blue]
> "Aparajita" <aparajita.moha...> wrote in message
>
>[color=green]
> > Hi,[/color]
>[color=green]
> > I want to find the greatest of two given numbers say 'A' and 'B.[/color]
>
> That would be the "greater" of two numbers; the "greatest" implies at least
> three...
>[color=green]
> > 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]
>
> No there isn't.
>
> But what you want CAN be done.
>
> a clue: Check out the COBOL SIGN test.
>
> Then think about how a computer is able to make comparisons. How would a
> "compare" instruction (on any platform) "work"? If you had to build a
> computer, how would you build a "compare" instruction? Given that all you
> can do is arithmetic and sign checking, how would you implement a "compare"?
>
> Post your thoughts here, and we'll see how you go.
>
> Pete.
> --
> "I used to write COBOL...now I can do anything."[/color]

Hi Pete,
Thanks for your response.
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.

Thanks!
Aparajita

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

"Aparajita" <aparajita.mohanty> wrote in message
>> > Hi,[/color]
>>[color=darkred]
>> > I want to find the greatest of two given numbers say 'A' and 'B.[/color]
>>[/color][/color]
> 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

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

"Aparajita" <aparajita.mohanty> wrote in message
> On Aug 31, 3:28 pm, "Pete Dashwood"
> <dashw...@removethis.enternet.co.nz> wrote:[color=green]
>> "Aparajita" <aparajita.moha...> wrote in message
>>
>>[color=darkred]
>> > Hi,[/color]
>>[color=darkred]
>> > I want to find the greatest of two given numbers say 'A' and 'B.[/color]
>>
>> That would be the "greater" of two numbers; the "greatest" implies at
>> least
>> three...
>>[color=darkred]
>> > 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]
>>
>> No there isn't.
>>
>> But what you want CAN be done.
>>
>> a clue: Check out the COBOL SIGN test.
>>
>> Then think about how a computer is able to make comparisons. How would a
>> "compare" instruction (on any platform) "work"? If you had to build a
>> computer, how would you build a "compare" instruction? Given that all you
>> can do is arithmetic and sign checking, how would you implement a
>> "compare"?
>>
>> Post your thoughts here, and we'll see how you go.
>>
>> Pete.
>> --
>> "I used to write COBOL...now I can do anything."[/color]
>
> Hi Pete,
> Thanks for your response.
> 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]

Yes, you are on the right track. But the code is flawed.

What happens if A = B? Your code will incorrectly display that "B IS
GREATER".

You should seek to understand the code yourself, then you won't need my
opinion (or anyone else's...) as to constraints or limitations on it. Had
you taken my advice and checked out the COBOL SIGN test you could have
written a better solution. HINT: It's not too late... :-)

Read about the COBOL SIGN Test, then see if you can fix it yourself.

Picking up a solution from someone else is only one way to solve a problem,
although it is sometimes a valid approach. Always ****yse whatever you pick
up, until you understand it thoroughly yourself. If you don't understand it,
don't use it... :-)

Cheers,

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

7. ## 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]
>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.[/color]

Why? If it is because you don't like the symbols, use the words.
[color=blue]
>Is there any other operator in COBOL by which we can compare two
>numbers.[/color]

There are ways to solve your problem in CoBOL. I don't know what
kind of operator you want for this excluding the operators designed
for it.

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

>>> On 8/31/2007 at 12:41 AM, in message
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.[/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.

Frank

9. ## 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...
[snip][color=blue]
> Not sure why you need this, but perhaps FUNCTION MAX could give you what[/color]
you[color=blue]
> 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.

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

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.

P.S. To OP who gave

IF A-B ....

This may just be a typo (or my misreading a post),but Standard COBOL requires
spaces around the subtraction sign.
"A-B" is a single data item name, like ADDRESS-OF-HOUSE
while
"A - B" is an arithmetic expression

--
Bill Klein
wmklein <at> ix.netcom.com
"Pete Dashwood" <dashwood@removethis.enternet.co.nz> wrote in message
news:5jq8r6Fqh1sU1@mid.individual.net...[color=blue]
>
>
> "Aparajita" <aparajita.mohanty> wrote in message
>> Hi,
>>
>> I want to find the greatest of two given numbers say 'A' and 'B.[/color]
>
> That would be the "greater" of two numbers; the "greatest" implies at least
> three...
>[color=green]
>> 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]
> No there isn't.
>
> But what you want CAN be done.
>
> a clue: Check out the COBOL SIGN test.
>
> Then think about how a computer is able to make comparisons. How would a
> "compare" instruction (on any platform) "work"? If you had to build a
> computer, how would you build a "compare" instruction? Given that all you can
> do is arithmetic and sign checking, how would you implement a "compare"?
>
> Post your thoughts here, and we'll see how you go.
>
> Pete.
> --
> "I used to write COBOL...now I can do anything."
>[/color]