Exercises: Table Handling and Searching
Exercise 18.1: Basic Table Definition and Access
Define a table to store information about 10 products. Each product has a 5-character product code, a 30-character description, a unit price (up to 9999.99), and a quantity-on-hand (up to 99999). Write a program that:
- Loads the table with at least 10 hardcoded product entries using REDEFINES
- Displays all products in a formatted report
- Accepts a product code from the user and displays the matching product details
- Handles the case where the product code is not found
Requirements: - Use INDEXED BY on your table definition - Use SEARCH for the lookup - Include proper boundary checking
Exercise 18.2: Subscripts vs. Indexes Performance Test
Write a program that creates a 1,000-entry table of random account numbers and performs 100,000 lookups using: - (a) A PERFORM loop with a subscript (PIC 9(4) COMP) - (b) A SEARCH with an index
Use FUNCTION CURRENT-DATE to capture start and end times for each approach. Display the elapsed time for each method. Discuss your findings.
Exercise 18.3: Multi-Dimensional Table
Create a shipping cost lookup table with two dimensions: - Dimension 1: Weight class (5 classes: 0-1 lb, 1-5 lb, 5-20 lb, 20-50 lb, 50+ lb) - Dimension 2: Shipping zone (4 zones: Local, Regional, National, International)
Write a program that: 1. Loads the 5x4 table with hardcoded shipping costs 2. Accepts a package weight and destination zone 3. Determines the correct weight class 4. Looks up and displays the shipping cost 5. Calculates the total for multiple packages in a batch
Exercise 18.4: SEARCH ALL with Compound Keys
Create a fee schedule table for MedClaim with 25 entries, sorted by provider type (2 chars) and procedure code (5 chars). Write a program that:
- Loads the table from hardcoded values (ensure proper sort order)
- Verifies the sort order programmatically
- Uses SEARCH ALL to look up fees by provider type AND procedure code
- Handles not-found conditions with appropriate messages
- Processes a batch of 10 lookup requests and reports the results
Exercise 18.5: Variable-Length Table
Write a program that reads student records from a file and loads them into a variable-length table (OCCURS 1 TO 200 TIMES DEPENDING ON). Each record contains student ID, name, and GPA. The program should:
- Load all records, validating the count does not exceed 200
- Display the total number of students loaded
- Calculate and display the average GPA
- Accept a student ID and look up the student's information
- Display a message if the table is empty after loading
Exercise 18.6: Table Loading from File with Sort Verification
Write a program that: 1. Reads a diagnosis code file (code PIC X(5), description PIC X(50), category PIC X(3)) 2. Loads records into a table with a maximum of 500 entries 3. Verifies the file is sorted by diagnosis code during loading 4. If a sort error is found, displays the offending entries and abends 5. After successful loading, performs SEARCH ALL lookups for 5 test codes 6. Reports found/not-found for each test code
Exercise 18.7: Cross-Reference Table Design
Extend Exercise 18.6 by creating a secondary cross-reference table that indexes diagnosis codes by category. After loading the primary table:
- Build the cross-reference table (sorted by category, containing pointers back to the primary table)
- Accept a category code from the user
- Display all diagnosis codes in that category using the cross-reference
- Count and display the number of codes per category
Exercise 18.8: Defensive Programming Audit
You are given the following code with multiple table-handling bugs. Identify all bugs, explain what could go wrong, and write the corrected version:
01 CUST-TABLE.
05 CUST-ENTRY OCCURS 100 TIMES.
10 CUST-ID PIC 9(6).
10 CUST-NAME PIC X(30).
10 CUST-BAL PIC S9(7)V99.
PROCEDURE DIVISION.
MOVE ZERO TO WS-COUNT
READ CUST-FILE INTO WS-CUST-REC
PERFORM UNTIL WS-EOF
ADD 1 TO WS-COUNT
MOVE WS-REC-ID TO CUST-ID(WS-COUNT)
MOVE WS-REC-NAME TO CUST-NAME(WS-COUNT)
MOVE WS-REC-BAL TO CUST-BAL(WS-COUNT)
READ CUST-FILE INTO WS-CUST-REC
AT END SET WS-EOF TO TRUE
END-READ
END-PERFORM
SEARCH CUST-ENTRY
AT END DISPLAY "NOT FOUND"
WHEN CUST-ID(CUST-IDX) = WS-LOOKUP-ID
DISPLAY CUST-NAME(CUST-IDX)
END-SEARCH.
Exercise 18.9: GlobalBank Account Type System
Build a complete account type management program for GlobalBank that:
- Loads account types from a hardcoded REDEFINES table (at least 8 types)
- Loads branch information from a file into a variable-length table
- For each transaction record (read from file), looks up: - The account type description (binary search) - The branch name (serial search)
- Produces a formatted transaction report with descriptions
- Reports statistics: total lookups, successful lookups, failed lookups, average lookup time
Exercise 18.10: Challenge — Three-Dimensional Fee Table
Build a three-dimensional fee lookup for MedClaim where fees vary by: - Provider type (4 types) - Service category (6 categories) - Geographic region (3 regions)
Load the 4x6x3 = 72-entry table from hardcoded values. Write a program that: 1. Accepts provider type, service category, and region 2. Validates all three inputs 3. Looks up the fee using direct indexing (convert codes to subscripts) 4. If any dimension is invalid, displays a specific error message 5. Processes a batch of claims and totals fees by region