Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message
vina2010

New User

Joined: 06 Sep 2010
Posts: 19
Location: Bangalore

 Posted: Mon Sep 06, 2010 1:07 pm    Post subject: Comparing 3 Numbers Hi I have three numbers stored in variables OPTION1,OPTION2,OPTION3.I need to display the greatest or equal of the three variables. Example: 1> If OPTION1 = 20 ,OPTION2 = 10 ,OPTION3 = 30.My output should be say OPTION3 2> If OPTION1 = 30 ,OPTION2 = 10 ,OPTION3 = 30.My output should be say OPTION1,OPTION3 3> If OPTION1 = 10 ,OPTION2 = 10 ,OPTION3 = 10.My output should be say OPTION1,OPTION2,OPTION3 For any given input of 3 numbers I should display as above.Assume no negative values. I have coded a logic in COBOL as below to achieve this but its quite big and confusing. IF WS-OPT(1) = WS-OPT(2) IF WS-OPT(2) = WS-OPT(3) DISPLAY 'OPTION1,OPTION2,OPTION3' ELSE IF WS-OPT(2) > WS-OPT(3) DISPLAY 'OPTION1,OPTION2' ELSE DISPLAY 'OPTION3' END-IF END-IF ELSE IF WS-OPT(1) > WS-OPT(2) IF WS-OPT(1) > WS-OPT(3) DISPLAY 'OPTION1' ELSE IF WS-OPT(2) >= WS-OPT(3) DISPLAY 'OPTION1' ELSE IF WS-OPT(3) > WS-OPT(1) DISPLAY 'OPTION3' ELSE DISPLAY 'OPTION1,OPTION3' END-IF END-IF END-IF ELSE IF WS-OPT(2) > WS-OPT(3) DISPLAY 'OPTION2' ELSE IF WS-OPT(3) = WS-OPT(2) DISPLAY 'OPTION2,OPTION3' ELSE DISPLAY 'OPTION3' END-IF END-IF END-IF. Assume WS-OPT(1) = OPTION1, WS-OPT(2) = OPTION2, WS-OPT(3) = OPTION3 Can we have a simpler code to achive this? I can't use JCL utilities here.I am using ACCEPT and DISPLAY for input and output purpose.

Binop B

Active User

Joined: 18 Jun 2009
Posts: 407
Location: Nashville, TN

Posted: Mon Sep 06, 2010 1:36 pm    Post subject:

Hi Vina,

Am not sure whether this is what you want or whether this will help you... anyways ...

You could probably use the MAX function in COBOL.

To simplify the code - you could follow the below steps
 Code: 1. MAX ( OPTION1, OPTION2, OPTION3 ) = max_value 2. If OPTION1=max_value, then Display "OPTION1" 3. If OPTION2=max_value, then Display "OPTION2" 4. If OPTION3=max_value, then Display "OPTION3"
Bharath Bhat

Active User

Joined: 20 Mar 2008
Posts: 283
Location: chennai

Posted: Mon Sep 06, 2010 1:38 pm    Post subject:

 Code: EVALUATE TRUE     WHEN O1>O2          EVALUATE TRUE              WHEN O1>O3                   DISPLAY 'O1'              WHEN O3>O1                   DISPLAY 'O3'              WHEN O3=O1                   DISPLAY 'O1,O3'         END-EVALUATE          WHEN O2>O1          EVALUATE TRUE              WHEN O2>O3                   DISPLAY 'O2'              WHEN O3>O2                   DISPLAY 'O3'              WHEN O3=O2                   DISPLAY 'O2,O3'         END-EVALUATE          WHEN O1=O2          EVALUATE TRUE              WHEN O1>O3                   DISPLAY 'O1,O2'              WHEN O3>O1                   DISPLAY 'O3'              WHEN O3=O1                   DISPLAY 'O1,O2,O3'         END-EVALUATE      END-EVALUATE
PeterHolland

Global Moderator

Joined: 27 Oct 2009
Posts: 2446
Location: Netherlands, Amstelveen

 Posted: Mon Sep 06, 2010 1:40 pm    Post subject: Maybe a combination of the functions ORD-MAX, ORD-MIN could be of use if you are using an array.
vina2010

New User

Joined: 06 Sep 2010
Posts: 19
Location: Bangalore

 Posted: Mon Sep 06, 2010 2:33 pm    Post subject: Thank you Guys, Bharath,your code is working,This is a nice replacement for my IF-ELSE logic ,I did simplify my code and has good readability. Thanks Binop, I am using COBOL II and my compiler doesn't support Functions so had to take the IF-ELSE route.But anyways thanks. Guys,just a general question, can't we have a method/logic to compare 'n' numbers.When 'n' increases the code becomes really messy.If you have any ideas please share.
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10396
Location: italy

Posted: Mon Sep 06, 2010 2:47 pm    Post subject: Reply to: Comparing 3 Numbers

store the numbers in an array/table and write a subroutine/function to do the sorting/comparison
even worth using it for three numbers

 Code: pseudocode A array holding values K number of entries into the array imin,min imax,max call minmax(k,a,imin,min,imax,max) minmax: imin = 1 min  = a(1) imax = 1 max = a(1) do i = 2 to k     if a(i) < min then do        imin = i        min = a(i)    end    if a(i) > max then do        imax = i        max = a(i)   end if end do return

but naturally the question asked was just homework,
otherwise it would be the silliest business requirement seen since a loooong time

another point which was not clear, You have to display the value or a name

if You had 3789 option and the largest one was the 2693 would you display
the value or the string "OPTION2693"
Binop B

Active User

Joined: 18 Jun 2009
Posts: 407
Location: Nashville, TN

Posted: Mon Sep 06, 2010 2:48 pm    Post subject:

Hi Vina,

 Quote: can't we have a method/logic to compare 'n' numbers.When 'n' increases the code becomes really messy.If you have any ideas please share.

You will need to store all the contents in an arrary - let's say LIST.
 Code: 1. Store LIST(1) to MAX-FIELD 2. Set n to 2 3. Perfom till END OF ARRAY    3.1 If Compare LIST(n) > MAX-FIELD, then MAX-FIELD = Compare LIST(n)    3.2 Increment n by 1 4. Set n to 1 5. Perfom till END OF ARRAY    5.1 If Compare LIST(n) = MAX-FIELD, Display Compare LIST(n)

Please note that this is just a rough idea and not a tested design..
dbzTHEdinosauer

Global Moderator

Joined: 20 Oct 2006
Posts: 6970
Location: porcelain throne

Posted: Mon Sep 06, 2010 2:49 pm    Post subject:

 Quote: Guys,just a general question, can't we have a method/logic to compare 'n' numbers.When 'n' increases the code becomes really messy.If you have any ideas please share.

either
• CALL SORT
• use a COBOL internal table and write your own sort logic
vina2010

New User

Joined: 06 Sep 2010
Posts: 19
Location: Bangalore

 Posted: Mon Sep 06, 2010 5:54 pm    Post subject: Thank you Guys for all the help. enrico ;OPTION1,OPTION2 and OPTION3 are result of some caluculations and I need to compare the three and display the variable name with the greatest or equal values. Thank you.
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics To get Non matching row after compari... vinu78 DB2 7 Fri Jan 05, 2018 5:43 pm Comparing dates in different formats migusd SYNCSORT 4 Sat Nov 18, 2017 3:02 am Comparing current time with the time ... arunsoods SYNCSORT 1 Mon Oct 30, 2017 4:07 pm Comparing 2 Files using Current time arunsoods SYNCSORT 9 Fri Sep 22, 2017 6:00 pm Incorrect output when trying to add n... monica1 PL/I & Assembler 10 Fri Jan 13, 2017 5:02 pm

 © 2003-2017 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us