Page 1 of 1

A cobol problem

PostPosted: Fri Nov 23, 2012 6:32 pm
by wangy
……
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?

Re: A cobol problem

PostPosted: Fri Nov 23, 2012 6:53 pm
by BillyBoyo
Err... what answer did it give you?

Re: A cobol problem

PostPosted: Fri Nov 23, 2012 7:31 pm
by wangy
BillyBoyo wrote:Err... what answer did it give you?

hi, it says the answer is "06".

Re: A cobol problem

PostPosted: Fri Nov 23, 2012 7:34 pm
by Robert Sample
And this is a surprise why?

Is this an interview question?

Re: A cobol problem

PostPosted: Fri Nov 23, 2012 8:04 pm
by BillyBoyo
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.

Re: A cobol problem

PostPosted: Sat Nov 24, 2012 10:53 am
by wangy
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.