Sorting a Sequential File


READ, RELEASE, SORT, Input Procedure

Introduction

A program is required which will process the Students File (Students.Dat) and will count the number of males and females taking each course. The totals must be displayed in ascending CourseCode order.

 Download Students.Dat and save it to the \WorkArea directory on drive D:
 

Student File Description

The Students File is a sequential file held in ascending StudentId order.
Each record of the students file contains the following items;
Field
Type
Length
Value
Student Id
9
7
0-9999999
Student Name
Group
Surname
X
8
-
Initials
X
2
-
DateOfBirth
Group
Year
9
2
00-99
Month
9
2
01-12
Day
9
2
01-31
Course Code
X
4
-
Grant
9
4
0000-9999
Gender
X
1
M/F
 
 

Suggested Approaches.

The main problem that we face here is that the results must be displayed in CourseCode order but the file is held in StudentId order.

You may think that a solution using tables would be the answer here.  That is, read each record, find its CourseCode and add 1 to the appropriate place in a CourseCode table (where the elements are in CourseCode order).  When all the records have been processed display the results by going through the table element by element.

The problem with this solution is that new courses are added quite frequently.  If we adopted the table approach we would have to change the program each time a new course is added.

So the solution you are going to adopt in this exercise is to create a new file with the records sorted on ascending CourseCode by  using the SORT verb to sort the Students File.  To make the SORT as efficient as possible (by reducing the amount of data that needs to be processed) records of the sorted file should only contain the fields that are needed for this program. An Input Procedure should be used to create the appropriate Sort Records.

When the sorted file has been produced it is a simple matter to process it to calculate the required totals
 

Sorted File Description.

The sorted file is a sequential file held in ascending CourseCode order.
Each record of the  file contains the following items;
Field
Type
Length
Value
Course Code
X
4
-
Gender
X
1
M/F
 
 
  Use edited picture clauses and the zero suppression symbol to produce the output as shown.
 
 

Sample Solution

When you have written your program and have compiled it and have it working correctly you may wish to compare it with this sample solution.
 
                                                         WARNING
As always please do not look at the solution until you have finished your own program.   At the very least you should make a substantial effort to complete your
own attempt at the program before examining the sample solution.
 
 

Back to COBOL Exercises page To COBOL Exercises Please fill out our evaluation form Evaluation Form Search the CSIS Web Site Search CSIS Web Site
Selectable Site Contents


Last updated : April 1998
e-mail : CSISwebeditor@ul.ie