A cobol problem



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

A cobol problem

Postby wangy » Fri Nov 23, 2012 6:32 pm

……
01 WS-NUM PIC 9(1) COMP.
01 TABLE-1.
03 TABLE-RECS OCCURS 20 TIMES INDEXED BY WS-IDX.
05 IDNO PIC 9(2).
……
PERFORM VARYING WS-NUM FROM 1 BY 1 UNTIL WS-NUM > 20
MOVE WS-NUM TO IDNO(WS-NUM)
END-PERFORM

MOVE 16 TO WS-NUM
SEARCH TABLE-RECS
AT END
DISPLAY WS-NUM ' NOT FOUND IN TABLE!'
WHEN IDNO(WS-IDX) = WS-NUM
DISPLAY IDNO(WS-IDX)
END-SEARCH
……
what is the answer when this programe be runned?
wangy
 
Posts: 5
Joined: Fri Nov 23, 2012 6:27 pm
Has thanked: 6 times
Been thanked: 0 time

Re: A cobol problem

 

Re: A cobol problem

Postby BillyBoyo » Fri Nov 23, 2012 6:53 pm

Err... what answer did it give you?
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: A cobol problem

Postby wangy » Fri Nov 23, 2012 7:31 pm

BillyBoyo wrote:Err... what answer did it give you?

hi, it says the answer is "06".
wangy
 
Posts: 5
Joined: Fri Nov 23, 2012 6:27 pm
Has thanked: 6 times
Been thanked: 0 time

Re: A cobol problem

Postby Robert Sample » Fri Nov 23, 2012 7:34 pm

And this is a surprise why?

Is this an interview question?
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

Re: A cobol problem

Postby BillyBoyo » Fri Nov 23, 2012 8:04 pm

If you want it to say 16, you have to define it as COMP-5 or use compiler option TRUNC(BIN).

However, I personally think it bad practice to not define enough digits in a field to hold the maximum logical value for that field.

Your maximum is 20. You defined with one digit, which therefore can hold a maximum value of 9. If you define with two digits (99) you'll be able to comfortably hold a maximum value (for your table) of 20.

These users thanked the author BillyBoyo for the post:
wangy (Sat Nov 24, 2012 10:51 am)
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: A cobol problem

Postby wangy » Sat Nov 24, 2012 10:53 am

BillyBoyo wrote:If you want it to say 16, you have to define it as COMP-5 or use compiler option TRUNC(BIN).

However, I personally think it bad practice to not define enough digits in a field to hold the maximum logical value for that field.

Your maximum is 20. You defined with one digit, which therefore can hold a maximum value of 9. If you define with two digits (99) you'll be able to comfortably hold a maximum value (for your table) of 20.

Thank you! I didn't watch the definition carefully. Thank you.
wangy
 
Posts: 5
Joined: Fri Nov 23, 2012 6:27 pm
Has thanked: 6 times
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post