COBOL Test3 Scheme Solutions
COBOL Test3 Scheme Solutions
PART-A Marks
Q.1. what are Indexed Sequential Files? Explain the file-control paragraph for Indexed
files. (5+5)
Q.2. with suitable program example, Explain the concept of subroutine in COBOL. (5+5)
PART-B
Q.3. Explain briefly the various report groups used by a report writer in COBOL. Give an example
(5+5 )
Q.4. Explain the SORT verb and MERGE verb with respect to Sequential files. (5+5)
Q.5. Explain procedure division statements for relative file. (6+4)
PART-C
***********
DEPARTMENT OF MCA, PESIT, BANGALORE – 85
PART-A
Q.1. what are Indexed Sequential Files? Explain the file-control paragraph for Indexed
files. (10)
Ans:-
Indexed sequential files are the files in which records are stored in the key sequence order (usually in
ascending order).in addition , some index tables are also created and maintained with the file.
Thus in order to have access to a recod, first , the group to which a record belongs is identified with the help
of the index tables.
The general format for the SELECT clause for an indexed file is as follows :-
, ORGANIZATION is INDEXED
SEQUENTIAL
; ACCESS MODE IS RANDOM
DYNAMIC
; RECORD KEY IS data-name-1
Description :-
Q.2. with suitable program example, Explain the concept of subroutine in COBOL. (10)
Ans:-
Subroutine is a program which is Compiled, debugged and stored in library to be called when needed
and will perform particular task.
A distinguishing feature that makes a COBOL subroutine different from a main program is
the PROCEDURE DIVISION header. The procedure division header must have USING
phrase.
The genral format is as follows :-
PROCEDURE DIVISION [ USING data-name-1 [ , data-name-2 ] …]
Description :-
operands data-name -1, data name-2 …etc of USING phrase are those data
names which are to be connected with the corresponding data names in the
program calling this subroutine.
The operands of USING phrase must be either 01-level or 77-level data items.
these must be defined in a section of the DATA DIVISION known as the
LINKAGE SECTION.
The linkage section appears in the called program (subroutine) and describes
records or non-contiguous data items in respect of which connection should be
established with the calling program (main program or another sub-routine).
Format of Linkage section :-
LINKAGE SECTION.
77 –level-data-description-entry
Record-description-entry ……
Description :-
The data description entries are simmiller to those of working-storage
section except that the VALUE clause is not allowed to be specied for any
entry in the Linkage section.
In the case of a main program , the logical end of a program is indicated by
a STOP RUN statement .Normally, upon the execution of a subroutine it is
required that the control must be returned to the calling program. For this
purpose the EXIT PROGRAM statement is available in COBOL.
Format is :-
EXIT PROGRAM .
Example :-
IDENTIFICATION DIVISION.
PROGRAM-ID.B.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC S99.
01 B PIC S99.
01 C PIC S99.
01 X1 PIC S999V99.
01 X2 PIC -999V99.
01 X PIC -999V99.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "ENTER THE A VALUE".
ACCEPT A.
DISPLAY "ENTER THE B VALUE".
ACCEPT B.
DISPLAY "ENTER THE C VALUE".
ACCEPT C.
CALL "SUB" USING A B C X1 X2.
DISPLAY "THE X1 VALUE IS " X1.
DISPLAY "THE X2 VALUE IS " X2.
STOP RUN.
SUBROUTINE
IDENTIFICATION DIVISION.
PROGRAM-ID.SUB.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 X PIC S999V99.
01 Y PIC S999V99.
LINKAGE SECTION.
01 A PIC S99.
01 B PIC S99.
01 C PIC S99.
01 X1 PIC -999V99.
01 X2 PIC -999V99.
Q.3. Explain briefly the various report groups used by a report writer in COBOL. Give an example (10 )
Ans:-
The various report groups used by a report are.
1. REPORT HEADING:-
This group represents the title of the report and is printed only once in
the very beginning.
Ex:-
01 TYPE IS RH.
02 LINE NUMBER IS 4.
03 COLUMN NUMBER IS 18 PIC X(11) VALUE "MOBILE-TECH".
02 LINE NUMBER IS PLUS 1.
03 COLUMN NUMBER IS 21 PIC X(5) VALUE "INDIA".
2. REPORT FOOTING:-
This group is also printed only once and at the very end of the report. This
group usually includes a collection of totals.
3. PAGE HEADING:-
This group is printed on the top of each logical page. This group can include
The page number, usual column headings and brought-forward totals from the pervious page.
Ex:-
01 TYPE IS PH.
02 LINE NUMBER IS 4.
03 COLUMN NUMBER IS 1 PIC X(4) VALUE "YEAR".
03 COLUMN NUMBER IS 10 PIC X(5) VALUE "NOKIA".
03 COLUMN NUMBER IS 20 PIC X(8) VALUE "MOTROLLA".
03 COLUMN NUMBER IS 30 PIC X(7) VALUE "SAMSUNG".
03 COLUMN NUMBER IS 40 PIC X(8) VALUE "RELIANCE".
4. PAGE FOOTING:-
This group is printed at the bottom of each page. The group can include
carry-forward totals.
5. CONTROL FOOTING:-
An important function of the report-writer feature is that it takes care
of control breaks in the input file from which report is being generated. Whenever there is
any change in a specified key field, The control break occurs and at that point the control-
footing group is printed. The group can include sub-totals of some fields.
Ex:-
6. CONTROL HEADING:-
This group is also printed on a control break but only before the
next detail group is printed.
For ex:- if a control break occurs on the sales number, then we might like to get the name
of the next salesman printed.
7. DETAIL:-
This group constitutes main group of the report. The records of the input file
can be listed in this group. A record may be printed in one or more lines.
Except for the detail group all other groups are optional. We can use only once the
REPORT HEADING , REPORT FOOTING, PAGE HEADING, PAGE FOOTING groups
for a report. There can be as many as CONTROL FOOTING, CONTROL FOOTING
groups as there are control fields. But, several detail groups can be specified for a report.
Ex:-
01 DETAIL-FILE TYPE IS DETAIL.
02 LINE NUMBER IS PLUS 1.
03 COLUMN NUMBER IS 1 PIC 9(4) SOURCE IS YY.
03 COLUMN NUMBER IS 10 PIC 9(5) SOURCE IS NOKIA.
03 COLUMN NUMBER IS 20 PIC 9(5) SOURCE IS MOTROLLA.
03 COLUMN NUMBER IS 30 PIC 9(5) SOURCE IS SAMSUNG.
03 COLUMN NUMBER IS 40 PIC 9(5) SOURCE IS RELIANCE.
Q.4. Explain the SORT verb and MERGE verb with respect to Sequential files.
Ans :-
SORTING : -
The process of sequencing the records in some desired manner is known as sorting.
Sorting is done upon some key data item in the record.
For eg, payroll file where each record contains all the necessary information of an employee, such as
name, id , deptno, basicpay etc.
The format of the simple SORT verb is,
Description :-
SORT verb requires naming of 3 files namely , unsorted input-file, sorted output-file and work file.
File-name-1 is the name of the work file, where as the name of the input and output files are to be specified
in file-name-2 and file-name-3 respectively.
File-name-2 and file-name-3 are described in the usual manner by means of an FD entry.
The work-file is to be defined by a sort description entry (SD entry).
The format of the SD entry is,
SD file-name
[; RECORD CONTAINS [integer-1 TO] integer-2 CHARACTERS]
[; DATA { RECORD IS } data-name-1 [ , data-name-2]……]
Example :-
Assume that have Student-file with the following description in the DATA division.
FD STD-FILE.
01 STD-REC.
02 STD-ID PIC 999.
02 STD-NAME PIC X(20).
02 SEM PIC 999.
Suppose we wish to sort this Student-file on STD-NAME in ascending sequence. To sort this file we require
one work file and output file be SORT-FILE and OUT_FILE respectively.
The DATA DIVISION entries for these two files are as follows :
SD SORT-FILE.
01 SORT-REC.
02 FILLER PIC X(3).
02 SSTD-NAME PIC X(20).
02 FILLER PIC X(3).
FD OUT-FILE.
01 OUTPUT-REC PIC X(26).
The following statement will sort the input file and will create the sorted output file.
MERGING :-
The process combing two or more files into one according some key field in the records is known as
Merging.
The merging of files can be done in COBOL with the help of the MERGE verb. It is possible to
merge two or more files with one MERGE statement.
The format of the simple MERGE verb is as follows :
MERGE file-name-1
ASCENDING
ON DESCENDING KEY data-name-3 [ , data-name-4 ]….. …….
Example :-
Suppose two separate files of employees are to be combined into one
Both input files and the resulting output file contain 80 characters with an
Emp-No in the first nine positions
DATA DIVISION.
FILE SECTION.
FD EMP-FILE-1.
01 EMP-REC-1 PIC X(80).
FD EMP-FILE-2.
01 EMP-REC-2 PIC X(80).
SD MERGE-FILE.
01 MERGE-REC.
05 MRG-EMP-NO PIC X(9).
05 REST-OF-REC PIC X(71).
FD OUT-EMP-FILE.
01 OUT-EMP-REC PIC X(80).
PROCEDURE DIVISION.
MAIN-MODULE.
MERGE MERGE-FILE ON ASCENDING KEY MRG-EMP-NO
USING EMP-FILE-1, EMP-FILE-2 GIVING OUT-EMP-FILE
STOP RUN.
The general format for the READ statement are as shown below.
Format 1 :
READ file-name RECORD [ INTO identifier ]
[ ; AT END imperative-statement ]
This format is for sequential files.
Format 2 :
READ file-name RECORD [ INTO identifier ]
[ ; INVALID KEY imperative-statement ]
This format is used when access mode is either random or dynamic.
Format 3 :
READ file-name [ NEXT ] RECORD [ INTO identifier ]
[ ; AT END imperative-statement ]
The following statements will read the fiftieth record from the file.
MOVE 50 TO REL-KEY.
READ REL-FILE RECORD INVALID-KEY GO TO PARA-INVALID
WRITE STATEMENT :
MOVE 50 TO REL-KEY.
WRITE REL-OUTPUT INVALID KEY GO TO PARA-INVALID.
PART-C
Q.6. Write a program to create an indexed organized file with
Reg_No PIC X(9)
Name PIC X(15)
DOB PIC 9(6)
With Reg_No as record key and perform (i) Add a new record (ii) Delete a Record
Ans : -
IDENTIFICATION DIVISION.
PROGRAM-ID. INDEX1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MASTFILE ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS REG-NO.
DATA DIVISION.
FILE SECTION.
FD MASTFILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "MAST.DAT"
DATA RECORD IS MASTREC.
01 MASTREC.
02 REG-NO PIC X(9).
02 NAME PIC X(15).
02 DOB PIC 9(6).
WORKING-STORAGE SECTION.
77 ANS PIC X VALUE "Y".
77 CHOICE PIC 9.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM MENU-PARA UNTIL CHOICE = 3.
STOP RUN.
MENU-PARA.
DISPLAY "--------MENU--------".
DISPLAY "1.ADD A NEW RECORD".
DISPLAY "2.DELETE A RECORD".
DISPLAY "3.EXIT".
DISPLAY "SELECT YOUR CHOICE:".
ACCEPT CHOICE.
IF CHOICE = 1
GO TO PERFORM1-PARA.
IF CHOICE = 2
GO TO DELETE-PARA.
PERFORM1-PARA.
OPEN OUTPUT MASTFILE.
CREATE-PARA.
DISPLAY "ENTER THE STUDENT ROLL NO :".
ACCEPT REG-NO.
DISPLAY "ENTER THE STUDENT NAME:".
ACCEPT NAME.
DISPLAY "ENTER THE DATE OF BIRTH:".
ACCEPT DOB.
DELETE-PARA.
OPEN I-O MASTFILE.
DISPLAY "ENTER THE REG-NO TO BE DELETED:".
ACCEPT REG-NO.
DELETE MASTFILE INVALID KEY DISPLAY "RECORD NOT FOUND" GO TO MENU-PARA .
DISPLAY "RECORD SUCCESFULLY DELETED".
CLOSE MASTFILE.
GO TO MENU-PARA.
Q.7. Write a COBOL program to create a Sequential file containing the following fields
Customer Name X(20)
City X(10)
Account Number 9(6)
Balance 9(6)V9(3)
Read the file and count the number of customer having Balance > Rs 50,000
Ans : -
IDENTIFICATION DIVISION.
PROGRAM-ID. P8.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUST-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CUST-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "CUS.DAT".
01 CUS-REC.
02 CUS-NAME PIC X(10).
02 CITY PIC X(10).
02 ACC-NO PIC 9(2).
02 BALANCE PIC 9(7).
WORKING-STORAGE SECTION.
77 CH1 PIC X.
77 CNT PIC 99 VALUE 0.
01 ECNT PIC ZZZ.
PROCEDURE DIVISION.
MAIN-PARA.
OPEN OUTPUT CUST-FILE.
PERFORM ACC-PARA UNTIL CH1 = "N" OR "n".
CLOSE CUST-FILE.
OPEN INPUT CUST-FILE
DISPLAY "CUST-NAME * CITY * ACC-NO * BALANCE".
DISPLAY "---------------------------------------------".
READ CUST-FILE AT END MOVE "Y" TO CH1.
PERFORM DISP-PARA UNTIL CH1 = "Y".
DISPLAY "----------------------------------------------".
DISPLAY "NUMBER OF CUSTOMERS HAVING BALANCE > 50,000 ARE :" CNT.
CLOSE CUST-FILE.
STOP RUN.
PER-PARA.
IF BALANCE > 50000
THEN
ADD 1 TO CNT.
ACC-PARA.
DISPLAY (1,15) "ENTER CUSTOMER DETAILS".
DISPLAY " ".
DISPLAY "ENTER CUSTOMER NAME:".
ACCEPT CUS-NAME.
DISPLAY "ENTER NAME OF THE CITY :".
ACCEPT CITY.
DISPLAY "ENTER ACCOUNT NUMBER:".
ACCEPT ACC-NO.
MOVE ACC-NO TO ECNT.
DISPLAY "ENTER BALANCE AMOUNT :".
ACCEPT BALANCE.
PERFORM PER-PARA.
WRITE CUS-REC.
DISPLAY "DO U WISH TO ADD MORE RECORDS(Y/N):".
ACCEPT CH1.
DISP-PARA.
DISPLAY CUS-REC.
DISPLAY SPACE.
READ CUST-FILE AT END MOVE "Y" TO CH1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT REP-FILE ASSIGN TO "REP1.DAT".
DATA DIVISION.
FILE SECTION.
FD SALES-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "SALES.DAT".
01 SALES-RECORD.
02 YY PIC 9999.
02 NOKIA PIC 9(5).
02 MOTROLLA PIC 9(5).
02 SAMSUNG PIC 9(5).
02 RELIANCE PIC 9(5).
02 TOTAL PIC Z(5).
FD REP-FILE
REPORT IS SALES-REPORT.
WORKING-STORAGE SECTION.
01 FLAG PIC X VALUE "N".
REPORT SECTION.
RD SALES-REPORT CONTROLS ARE FINAL
PAGE LIMIT IS 40 LINES
HEADING 4
FIRST DETAIL 10.
01 TYPE IS RH.
02 LINE NUMBER IS 4.
03 COLUMN NUMBER IS 18 PIC X(11) VALUE "MOBILE-TECH".
02 LINE NUMBER IS PLUS 1.
03 COLUMN NUMBER IS 21 PIC X(5) VALUE "INDIA".
01 TYPE IS PH.
02 LINE NUMBER IS 4.
03 COLUMN NUMBER IS 1 PIC X(4) VALUE "YEAR".
03 COLUMN NUMBER IS 10 PIC X(5) VALUE "NOKIA".
03 COLUMN NUMBER IS 20 PIC X(8) VALUE "MOTROLLA".
03 COLUMN NUMBER IS 30 PIC X(7) VALUE "SAMSUNG".
03 COLUMN NUMBER IS 40 PIC X(8) VALUE "RELIANCE".
PROCEDURE DIVISION.
PARA-1.
OPEN INPUT SALES-FILE.
OPEN OUTPUT REP-FILE.
INITIATE SALES-REPORT.
READ-PARA.
READ SALES-FILE AT END GO TO LAST-PARA.
GENERATE DETAIL-FILE GO TO READ-PARA.
LAST-PARA.
TERMINATE SALES-REPORT.
CLOSE SALES-FILE REP-FILE.