Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Comparing 3 Numbers

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
vina2010

New User


Joined: 06 Sep 2010
Posts: 19
Location: Bangalore

PostPosted: Mon Sep 06, 2010 1:07 pm    Post subject: Comparing 3 Numbers
Reply with quote

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. icon_evil.gif
Back to top
View user's profile Send private message

Binop B

Active User


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

PostPosted: Mon Sep 06, 2010 1:36 pm    Post subject:
Reply with quote

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"
Back to top
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Mon Sep 06, 2010 1:38 pm    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


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

PostPosted: Mon Sep 06, 2010 1:40 pm    Post subject:
Reply with quote

Maybe a combination of the functions ORD-MAX, ORD-MIN could be of use
if you are using an array.
Back to top
View user's profile Send private message
vina2010

New User


Joined: 06 Sep 2010
Posts: 19
Location: Bangalore

PostPosted: Mon Sep 06, 2010 2:33 pm    Post subject:
Reply with quote

Thank you Guys,

Bharath,your code is working,This is a nice replacement for my IF-ELSE logic icon_biggrin.gif ,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.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10232
Location: italy

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

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"
Back to top
View user's profile Send private message
Binop B

Active User


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

PostPosted: Mon Sep 06, 2010 2:48 pm    Post subject:
Reply with quote

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..
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Mon Sep 06, 2010 2:49 pm    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
vina2010

New User


Joined: 06 Sep 2010
Posts: 19
Location: Bangalore

PostPosted: Mon Sep 06, 2010 5:54 pm    Post subject:
Reply with quote

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.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Incorrect output when trying to add n... monica1 PL/I & Assembler 10 Fri Jan 13, 2017 5:02 pm
No new posts Row-Numbers of distinct rows? Auryn DB2 1 Thu Oct 20, 2016 4:38 pm
No new posts Comparing Decimal and CHAR columns rakesh17684 DB2 7 Thu Oct 20, 2016 2:33 am
No new posts comparing comp-3 and unpacked numbers juares castro COBOL Programming 3 Mon May 30, 2016 6:46 pm
No new posts Need help on comparing hexadecimal va... ashok_uddaraju DFSORT/ICETOOL 4 Thu Apr 28, 2016 2:08 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us