Exercises — Chapter 15: Sort and Merge

Exercise 15.1: Basic USING/GIVING Sort

Difficulty: Beginner

Write a program that sorts an employee file by last name (ascending), then first name (ascending). Use USING and GIVING phrases — no INPUT or OUTPUT PROCEDUREs.

Employee record layout:

Employee-ID     PIC X(06)   Positions 1-6
Last-Name       PIC X(20)   Positions 7-26
First-Name      PIC X(15)   Positions 27-41
Department      PIC X(04)   Positions 42-45
Salary          PIC 9(07)V99 Positions 46-54
Hire-Date       PIC 9(08)   Positions 55-62
FILLER          PIC X(18)   Positions 63-80

Create at least 15 test records and verify the sorted output.

Exercise 15.2: Multi-Key Sort with Descending Order

Difficulty: Beginner

Using the same employee file from Exercise 15.1, sort by: 1. Department (ascending) 2. Salary (descending) 3. Last name (ascending)

This produces a listing of each department's employees from highest-paid to lowest-paid, with alphabetical ordering within the same salary.

Exercise 15.3: INPUT PROCEDURE — Filtering

Difficulty: Intermediate

Modify your employee sort program to use an INPUT PROCEDURE that: - Includes only employees hired after 20200101 - Excludes employees in department 'TEMP' - Counts and displays the number of records read, included, and excluded - Sorts the included employees by hire date (ascending)

Exercise 15.4: INPUT PROCEDURE — Validation and Transformation

Difficulty: Intermediate

Write a sort program for a product file that: - Validates that the price field is numeric and positive - Converts the category code from old format ('A', 'B', 'C') to new format ('ELEC', 'CLTH', 'FOOD') - Rejects records with blank product names, writing them to an error file - Sorts valid, transformed records by category (ascending), then price (descending)

Product record layout:

Product-ID      PIC X(10)
Product-Name    PIC X(30)
Category        PIC X(04)
Price           PIC 9(05)V99
Quantity        PIC 9(05)
FILLER          PIC X(24)

Exercise 15.5: OUTPUT PROCEDURE — Control Break Report

Difficulty: Intermediate

Sort the employee file by department (ascending), then salary (descending). Use an OUTPUT PROCEDURE to produce a report that shows: - A header for each department - Each employee's name and salary - A subtotal of salaries for each department - A grand total of all salaries - A count of employees per department and overall

Exercise 15.6: MERGE Program

Difficulty: Intermediate

Create three pre-sorted files of student records (sorted by student ID ascending): - File 1: Students from Campus A - File 2: Students from Campus B - File 3: Students from Campus C

Write a MERGE program that combines all three into a single file sorted by student ID. Verify the output manually.

Exercise 15.7: MERGE with OUTPUT PROCEDURE

Difficulty: Advanced

Extend Exercise 15.6 to use an OUTPUT PROCEDURE that: - Detects and reports duplicate student IDs across campuses - Writes only the first occurrence of each student ID to the output - Writes duplicates to a separate exception file - Displays counts of total records merged, unique records written, and duplicates found

Exercise 15.8: Sort Error Handling

Difficulty: Advanced

Write a transaction sort program that implements James Okafor's error threshold pattern: - Validate each record in the INPUT PROCEDURE - Track the error rate (errors / total records) - If the error rate exceeds 2% after at least 100 records have been read, set SORT-RETURN to 16 to abort the sort - Write all error details to an error log file - After the SORT, check SORT-RETURN and set an appropriate RETURN-CODE

Create test data with varying error rates (0.5%, 1.5%, 3%) and verify that the program correctly aborts only when the threshold is exceeded.

Exercise 15.9: Sort-Dedup Pattern

Difficulty: Advanced

Write a program that processes a customer order file: 1. Sorts by customer ID (ascending), then order date (descending, most recent first) 2. In the OUTPUT PROCEDURE, keeps only the most recent order for each customer (deduplicates by customer ID, keeping the first occurrence since most recent sorts first) 3. Writes the deduplicated records to an output file 4. Writes all removed duplicates to an archive file 5. Displays reconciliation counts

Exercise 15.10: Sort-Split Pattern

Difficulty: Advanced

Write a program that sorts a mixed transaction file and splits the output into separate files: - Deposits file (type 'D') - Withdrawals file (type 'W') - Transfers file (type 'T') - Unknown types file (anything else)

Use an INPUT PROCEDURE to validate records and an OUTPUT PROCEDURE to split the sorted output. Ensure all four output files are individually sorted by account number.

Exercise 15.11: In-Memory Table Sort

Difficulty: Challenge

Write a program that: 1. Loads up to 100 student records from a file into a table (OCCURS) 2. Uses SORT with INPUT/OUTPUT PROCEDUREs to sort the table by GPA (descending) 3. Displays the sorted table showing rank, name, and GPA 4. Does NOT write any intermediate files — the sort's SD file is the only file involved besides the original input

Exercise 15.12: Production-Quality Sort Program

Difficulty: Challenge

Build a production-quality sort program following Maria Chen's checklist from Section 15.16. The program should: - Log start/end timestamps using ACCEPT FROM DATE and ACCEPT FROM TIME - Validate all input records with detailed error codes - Implement the error threshold pattern (configurable threshold) - Use both INPUT and OUTPUT PROCEDUREs - Produce a sorted output file - Produce an error/exception file with reason codes - Produce a summary statistics report - Set appropriate RETURN-CODE values - Handle the edge case of an empty input file gracefully