The COBOL-85 Tutorial, Model Answers

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

The COBOL-85 Tutorial, Model

answers.
Model answers.
Copyright 1998-2003
Kim Kjrsulf.

This chapter (TOC)
Model Answer, ex. 1 Chp. 1.
Model Answer, ex. 1 Chp. 2.
Model Answer, ex. 1 Chp. 3.
Model Answer, ex. 1 Chp. 4.
Model Answer, ex. 1 Chp. 5.
Model Answer, ex. 1 Chp. 6.
Model Answer, ex. 1 Chp. 7.
Model Answer, ex. 1 Chp. 8.
Model Answer, ex. 1 Chp. 9.
Model Answer, ex. 2 Chp. 9.
Model Answer, ex. 1 Chp. 10.

Model Answer, ex. 1 Chp. 1.
[This chapter] [TOC] [Tutorial]

It is not possible in a text like this to cover the exact operation of all COBOL-
85 compilers.

You will find a section in the manual (Users Guide) where there is an
explanation of the program development cycle. The section should describe
editing, compiling, linking and execution of a cobol program. One or more of
the steps might be integrated in the environment and therefore not apparent.


Model Answer, ex. 1 Chp. 2.
[This chapter] [TOC] [Tutorial]

There are no exercises in chapter 2.


Model Answer, ex. 1 Chp. 3.
[This chapter] [TOC] [Tutorial]


* Model answer to Exercise 1 of chapter 3.
identification division.
* ------------ program identification.
program-id. calc.
author. kik.
environment division.
* ------------ program environment.
configuration section.
special-names.
console is crt
decimal-point is comma.
data division.
working-storage section.
77 in-1 pic 9999.
77 in-2 pic 9999.

77 summation pic 9(6).
77 difference pic s9(6).
77 quotient pic 9(6).
77 remain pic 9(6).
77 product pic 9(6).

01 Out-field.
03 text1 pic x(15).
03 figure1 pic ---.--9.
03 filler pic xx.
03 remaindertext pic x(15).
03 remainFig pic ---.--9.

procedure division.
* ------------ executable instructions.
main section.
display space.

display "Enter 1st figure :" line 5 column 10.
accept in-1 line 5 column 29.
display "Enter 2nd figure :" line 6 column 10.
accept in-2 line 6 column 29.

add in-1, in-2 giving summation.
subtract in-2 from in-1 giving difference.
divide in-1 into in-2 giving quotient remainder remain.
multiply in-1 by in-2 giving product.

move space to Out-field.
move "Addition" to text1.
move summation to figure1.
display Out-field line 8 column 1.

move space to Out-field.
move "Difference" to text1.
move difference to figure1.
display Out-field line 9 column 1.

move space to Out-field.
move "Product" to text1.
move product to figure1.
display Out-field line 10 column 1.

move space to Out-field.
move "Division" to text1.
move quotient to figure1.
move "remainder" to remaindertext.
move remain to remainFig.
display Out-field line 11 column 1.

exit program.


Model Answer, ex. 1 Chp. 4.
[This chapter] [TOC] [Tutorial]

There are no exercises in chapter 4.


Model Answer, ex. 1 Chp. 5.
[This chapter] [TOC] [Tutorial]

There are no exercises in chapter 5.


Model Answer, ex. 1 Chp. 6.
[This chapter] [TOC] [Tutorial]


* Model answer to Exercise 1 of chapter 6.
identification division.
* ------ program identification.
program-id. exe0601.
author. kik.

environment division.
* ------ program environment.
configuration section.
special-names.
console is crt
decimal-point is comma.
input-output section.
file-control.
select seqfile assign "SEQUDATA"
organization is line sequential
status is file-error.
data division.
* ------ description of the files.
file section.
fd seqfile.
01 seq-record.
03 custno pic 9999.
03 tr-code pic 99.
03 movement pic 9(6)v99.
03 balance pic 9(6)v99.

* ------ variable declarations.
working-storage section.
01 file-error pic 99.

01 eof-code pic 9 value 0.
88 eof value 1.

77 total-1 pic 9(6)v99 value zero.
77 total-2 pic 9(6)v99 value zero.
77 total-3 pic 9(6)v99 value zero.
77 total-4 pic 9(6)v99 value zero.
77 total-99 pic 9(6)v99 value zero.

01 Out-field.
03 textout pic x(15).
03 figure pic ---.--9.

procedure division.
* ------ Trap for IO-errors
declaratives.
seq-errors section.
use after error procedure on seqfile.
seq-error.
display "Error when opening input data : " line 24 column 1
with no ADVANCING.
display file-error line 24.
stop run.
seq-error-out.
exit.
*
end declaratives.
*
* ------ executable instructions.
main section.
perform open-file.
perform read-record.
perform process
until eof.
perform write-total.
perform close-file.
stop run.
*
*
open-file.
open input seqfile.
*
read-record.
read seqfile
at end move 1 to eof-code.
*
process.
if tr-code = 1 then
add movement to total-1
else
if tr-code = 2 then
add movement to total-2
else
if tr-code = 3 then
add movement to total-3
else
if tr-code = 4 then
add movement to total-4
else
if tr-code = 99 then
add balance to total-99.
perform read-record.
*
write-total.
move space to Out-field.
move "Code 1" to textout.
move total-1 to figure.
display Out-field line 5 column 1.

move space to Out-field.
move "Code 2" to textout.
move total-2 to figure.
display Out-field line 6 column 1.

move space to Out-field.
move "Code 3" to textout.
move total-3 to figure.
display Out-field line 7 column 1.

move space to Out-field.
move "Code 4" to textout.
move total-4 to figure.
display Out-field line 8 column 1.

move space to Out-field.
move "Code 99" to textout.
move total-99 to figure.
display Out-field line 9 column 1.

*
close-file.
close seqfile.


Model Answer, ex. 1 Chp. 7.
[This chapter] [TOC] [Tutorial]

There are no exercises in chapter 7.


Model Answer, ex. 1 Chp. 8.
[This chapter] [TOC] [Tutorial]

There are no model answer for chapter 8. Please refer to model answers
for chapter 6.


Model Answer, ex. 1 Chp. 9.
[This chapter] [TOC] [Tutorial]



* ------ Model answer to Exercise 1 of chapter 9.
identification division.
* ------ program identification.
program-id. exe0901.
author. kik.

environment division.
* ------ program environment.
configuration section.
special-names.
console is crt
decimal-point is comma.
input-output section.
file-control.
select seqfile assign "SEQUDATA"
organization is line sequential
status is file-error.
select isamfile assign "INDXDATA"
organization is indexed
access is sequential
record key is key in isam-record
with duplicates
status is file-error.

data division.
* ------ description of the files.
fd seqfile.
01 seq-record.
03 custno pic 9999.
03 tr-code pic 99.
03 movement pic 9(6)v99.
03 balance pic 9(6)v99.

fd isamfile.
01 isam-record.
03 key.
05 custno pic 9999.
05 tr-code pic 99.
03 movement pic 9(6)v99.
03 balance pic 9(6)v99.

* ------ variable declarations .
working-storage section.
01 file-error pic 99.

01 eof-code pic 9 value 0.
88 eof value 1.

procedure division.
* ------ Trap for IO-errors
declaratives.
seq-errors section.
use after error procedure on seqfile.
seq-error.
display "Error when opening input data : " line 24 position
1.
display file-error line 24.
stop run.
seq-error-ud.
exit.
*
isam-errors section.
use after error procedure on isamfile.
isam-error.
display "Error when opening isam data : "
line 24 position 1.
display file-error line 24.
stop run.
isam-error-out.
exit.
end declaratives.
*
* ------ executable instructions.
main section.
perform open-file.
perform read-record.
perform process
until eof.
perform close-file.
stop run.
*
*
open-file.
open input seqfile.
open output isamfile.
*
read-record.
read seqfile
at end move 1 to eof-code.
*
process.
move corr seq-record to isam-record.
move custno in seq-record to custno in isam-record.
move tr-code in seq-record to tr-code in isam-record.
write isam-record.
perform read-record.
*
close-file.
close seqfile,
isamfile.



Model Answer, ex. 2 Chp. 9.
[This chapter] [TOC] [Tutorial]


* ------ Model answer to Exercise 4a.
------ This program checks the result of exe4
identification division.
* ------ program identification.
program-id. exe4a.
author. kik.

environment division.
* ------ program environment.
configuration section.
special-names.
console is crt
decimal-point is comma.
input-output section.
file-control.
select isamfile assign "INDXDATA"
organization is indexed
access is sequential
record key is key in isam-record
with duplicates
status is file-error.

data division.
* ------ description of the files.
fd isamfile.
01 isam-record.
03 noegle.
05 custno pic 9999.
05 tr-code pic 99.
03 movement pic 9(6)v99.
03 balance pic 9(6)v99.

* ------ variable declarations .
working-storage section.
01 file-error pic 99.

01 eof-code pic 9 value 0.
88 eof value 1.

77 line-count pic 99 value 7.

01 out-line.
03 custno pic 9999.
03 filler pic xx.
03 tr-code pic 99.
03 filler pic xx.
03 movement pic -----9v.99.
03 filler pic xx.
03 balance pic -----9v.99.

procedure division.
* ------ Trap for IO-errors
declaratives.
*
isam-errors section.
use after error procedure on isamfile.
isam-error.
display "Error when opening isam data : "
line 24 position 1.
display file-error line 24.
stop run.
isam-error-out.
exit.
end declaratives.
*
* ------ executable instructions.
main section.
perform open-file.
perform read-record.
display space.
display "Records" line 5 position 1.
perform process
until eof.
perform close-file.
stop run.
*
*
open-file.
open input isamfile.
*
read-record.
read isamfile next
at end move 1 to eof-code.
*
process.
move space to out-line.
move corr isam-record to out-line.
move custno in isam-record to custno in out-line.
move tr-code in isam-record to tr-code in out-line.
display out-line line linie-tel position 5.
add 1 to line-count.
perform read-record.
*
close-file.
close isamfile.



Model Answer, ex. 1 Chp. 10.
[This chapter] [TOC] [Tutorial]


* ------ Model answer to Exercise 5.
identification division.
* ------ program identification.
program-id. exe5.
author. kik.

environment division.
* ------ program environment.
configuration section.
special-names.
console is crt
decimal-point is comma.
input-output section.
file-control.
select isamfile assign "INDXDATA"
organization is indexed
access is sequential
record key is key in isam-record
with duplicates
status is file-error.
select rep-file assign "REPORT"
organization is sequential
access is sequential
status is file-error.

data division.
* ------ description of the files.
file section.
fd isamfile.
01 isam-record.
03 key.
05 custno pic 9999.
05 tr-code pic 99.
03 movement pic 9(6)v99.
03 balance pic 9(6)v99.

fd rep-file
report is report.

* ------ variable declarations .
working-storage section.
01 file-error pic 99.

01 eof-code pic 9 value 0.
88 eof value 1.

report section.
rd report
control is custno in isam-record
page limit is 66 lines
first detail 5
last detail 60.

01 page header
line number is 2
type is page heading.

05 Header pic x(20) value "Header"
column number is 5.

01 customer-header
line number is plus 2
next group is plus 2
type is control heading custno in isam-record.

05 custno pic 9999 column 1
source is custno in isam-record.

01 customer-footer
line number is plus 1
next group is plus 1
type is control footing custno in isam-record.


05 foot-string pic x(20) value all "-"
column 11.
05 foot-text pic x(10) column 1
line number is plus 1
value "Total".
05 customer-move pic -----9v.99 column 11
sum movement upon detail-line.
05 customer-bal pic -----9v.99 column 22
sum balance upon detail-line.


01 detail-line
line number is plus 1
type is detail.

05 tr-code pic 99 column 7
source is tr-code in isam-record.
05 movement pic -----9v.99 column 11
source is movement in isam-record.
05 balance pic -----9v.99 column 22
source is balance in isam-record.

procedure division.
* ------ Trap for IO-errors
declaratives.
*
isam-errors section.
use after error procedure on isamfile.
isam-error.
display "Error when opening isam data : "
line 24 position 1.
display file-error line 24.
stop run.
isam-error-out.
exit.
end declaratives.
*
* ------ executable instructions.
main section.
perform open-file.
perform read-record.
perform process
until eof.
perform close-file.
stop run.
*
*
open-file.
open input isamfile,
output rep-file.
initiate report.
*
read-record.
read isamfile next
at end move 1 to eof-code.
*
process.
generate detail-linie.
perform read-record.
*
close-file.
terminate report.
close isamfile, rep-file.

You might also like