IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Comparing 3 Numbers


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
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
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
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
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: 2481
Location: Netherlands, Amstelveen

PostPosted: Mon Sep 06, 2010 1:40 pm
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Sep 06, 2010 2:47 pm
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
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Generate random number from range of ... COBOL Programming 3
No new posts Comparing Header and Trailer. DFSORT/ICETOOL 7
No new posts Feild level validation to test first ... JCL & VSAM 10
No new posts Line numbers contains last 6 digits o... TSO/ISPF 4
Search our Forums:

Back to Top