Chapter 3 Exercises: Data Types, Variables, and the PICTURE Clause

These exercises are organized into five tiers of increasing difficulty. Complete each tier before moving to the next. Solutions for selected exercises are available in code/exercise-solutions.cob.


Tier 1: PICTURE Clause Basics (Exercises 1-5)

Exercise 1: Basic Data Fields

Write COBOL data definitions for the following fields. Choose appropriate PICTURE clauses and sizes.

a) A person's first name (up to 20 characters) b) A person's last name (up to 25 characters) c) A person's age (up to 3 digits, no decimals) d) A person's gender (single letter)

Combine these into a group item called WS-PERSON-RECORD using level 05 for the elementary items.

Exercise 2: Product Inventory

Define a record called WS-PRODUCT-RECORD with the following fields:

a) Product code: up to 10 alphanumeric characters b) Product name: up to 30 characters c) Quantity on hand: up to 5 digits, no decimals d) Unit price: up to 5 integer digits and 2 decimal places e) Product category: up to 15 characters

Exercise 3: Storage Size Calculation

Calculate the storage size in bytes for each of the following data items in DISPLAY usage. Show your work.

a) PIC X(10) b) PIC 9(5) c) PIC S9(7)V99 d) PIC A(20) e) PIC 9(5) COMP

Exercise 4: Data Class Identification

For each PICTURE clause below, identify the data class (alphabetic, alphanumeric, numeric, or numeric edited) and state whether the field can be used in arithmetic operations.

a) PIC X(15) b) PIC 9(5)V99 c) PIC A(10) d) PIC Z(5)9.99 e) PIC S9(7) COMP-3 f) PIC $$$,$$9.99 g) PIC 99/99/9999 h) PIC S9(4) BINARY

Exercise 5: Notation Conversion

Convert between longhand and shorthand PICTURE notation. Write the equivalent form for each.

a) PIC 999V99 (convert to shorthand) b) PIC XXXXX (convert to shorthand) c) PIC S99999V999 (convert to shorthand) d) PIC AAAA (convert to shorthand) e) PIC 9(7)V9(2) (convert to longhand)


Tier 2: Numeric Types and Editing (Exercises 6-10)

Exercise 6: Financial Field Design

Write the PICTURE clause (and appropriate USAGE clause) for each of the following requirements. State the storage size in bytes.

a) A dollar amount up to $999,999.99 that can be negative. Used in arithmetic. b) A quantity counter that will never exceed 50,000. Used as a loop counter. c) An interest rate with up to 6 decimal places (e.g., 4.250000%). Used in calculations. d) An account balance up to $99,999,999,999.99 (11 integer digits). Used in arithmetic. e) A percentage field from 0.00% to 100.00%. Used in arithmetic. f) A transaction amount up to $9,999,999.99 with sign, stored as packed decimal.

Exercise 7: Edited Picture Design

Write edited PICTURE clauses to format output for the following requirements:

a) A dollar amount with commas and a floating dollar sign: e.g., $1,234,567.89 b) A negative amount shown with trailing CR: e.g., $1,234.56CR c) A date in MM/DD/YYYY format from a PIC 9(8) source field stored as MMDDYYYY d) A Social Security Number with dashes: e.g., 123-45-6789 e) A check-protected amount with floating dollar sign: e.g., $***1,234.56

Exercise 8: Zero Suppression Prediction

Given a source field PIC 9(5)V99 with the value 00042.50, predict the output when moved to each of the following edited fields. Show the exact output including spaces.

a) PIC ZZZ,ZZ9.99 b) PIC ***,**9.99 c) PIC $$$,$$9.99 d) PIC Z(5)9.99 e) PIC ------9.99

Then predict the output for a value of 00000.00 with the same edited fields.

Exercise 9: Source and Edited Field Pairs

For each of the following, write BOTH a source field (for arithmetic) and a corresponding edited field (for display). Give them appropriate names and initialize the source with the specified value.

a) Employee salary: $85,000.00 (stored as packed decimal) b) Account balance: -$1,234.56 (must show negative with leading minus and floating dollar sign) c) Interest rate: 8.25% (needs 4 decimal places) d) Check amount: $0.50 (check-protected with asterisks) e) National debt: $33,000,000,000,000.00 (13 integer digits)

Exercise 10: Editing Symbols Matching

Match each editing symbol with its behavior. For each symbol, provide a PICTURE example and show sample output for the value -1234.56.

Symbol Behavior
Z ?
* ?
$ (floating) ?
- (trailing) ?
+ (trailing) ?
CR ?
DB ?
B ?
/ ?

Tier 3: Group Items and Level Numbers (Exercises 11-15)

Exercise 11: Employee Record Design

Design a complete employee record hierarchy (WS-EMPLOYEE-RECORD) with the following information. Use appropriate level numbers (01, 05, 10, 15), PICTURE clauses, USAGE clauses, and condition names.

Required fields: - Employee ID (6 digits) - Full name (last name 25 chars, first name 20 chars, middle initial 1 char) - Address (street 30 chars, city 20 chars, state 2 chars, ZIP 5 digits, ZIP+4 4 digits) - Phone number (10 digits) - Hire date (YYYYMMDD format with subfields for year, month, day) - Annual salary (up to 7 integer digits + 2 decimal, packed decimal) - Department code (4 characters) - Employment status: A=Active, I=Inactive, T=Terminated, L=On Leave (with 88-level conditions)

Exercise 12: Condition Names for Payment Processing

Design a data item WS-PAYMENT-TYPE (PIC X(2)) with level 88 condition names for the following payment types:

  • CA = Cash
  • CK = Check
  • CC = Credit Card
  • DC = Debit Card
  • WT = Wire Transfer
  • AC = ACH Transfer

Also create 88-level conditions for: - PAY-VALID (any of the above) - PAY-ELECTRONIC (CC, DC, WT, AC) - PAY-PAPER (CA, CK)

Write an EVALUATE statement that uses these condition names to perform different processing for each payment type.

Exercise 13: Group Item Sizes

Calculate the total size in bytes for each of the following group items. Show the size of each elementary item and the running total.

a)

       01  WS-REC-A.
           05  FIELD-1     PIC X(10).
           05  FIELD-2     PIC 9(5).
           05  FIELD-3     PIC S9(7)V99 COMP-3.
           05  FIELD-4     PIC X(20).
           05  FIELD-5     PIC S9(4) COMP.

b)

       01  WS-REC-B.
           05  FIELD-1     PIC 9(8).
           05  FIELD-2.
               10  FIELD-2A PIC X(15).
               10  FIELD-2B PIC S9(5)V99 COMP-3.
           05  FIELD-3     PIC X(30).
           05  FIELD-4     PIC S9(9) COMP.

Exercise 14: RENAMES Exercise

Given the following record:

       01  WS-TRANSACTION-REC.
           05  WS-TRAN-DATE      PIC 9(8).
           05  WS-TRAN-TIME      PIC 9(6).
           05  WS-TRAN-TYPE      PIC X(2).
           05  WS-TRAN-AMOUNT    PIC S9(9)V99 COMP-3.
           05  WS-TRAN-ACCT      PIC 9(10).
           05  WS-TRAN-DESC      PIC X(30).

a) Write a level 66 RENAMES entry that creates WS-TRAN-DATETIME combining the date and time fields. b) Write a level 66 RENAMES entry that creates WS-TRAN-FINANCIAL combining the type, amount, and account fields. c) What are the sizes (in bytes) of each RENAMES field?

Exercise 15: REDEFINES for Multi-Format Input

A data file contains three types of records, identified by a single-character code in position 1:

  • H (Header): Positions 2-9 = batch date (PIC 9(8)), positions 10-15 = batch number (PIC 9(6)), positions 16-80 = description (PIC X(65))
  • D (Detail): Positions 2-11 = account number (PIC 9(10)), positions 12-17 = amount (PIC S9(9)V99 COMP-3), positions 18-19 = transaction code (PIC X(2)), positions 20-80 = filler
  • T (Trailer): Positions 2-8 = record count (PIC 9(7)), positions 9-15 = total amount (PIC S9(11)V99 COMP-3), positions 16-30 = hash total (PIC 9(15)), positions 31-80 = filler

Design the complete record layout using REDEFINES and level 88 condition names for the record type.


Tier 4: USAGE and Storage (Exercises 16-20)

Exercise 16: Storage Size Calculations

Calculate the exact storage size in bytes for each field. Show your calculation.

a) PIC S9(7)V99 COMP-3 b) PIC S9(4) COMP c) PIC 9(5) DISPLAY d) PIC S9(12)V99 COMP-3 e) PIC S9(10) COMP f) PIC S9(9)V99 COMP-3 g) PIC S9(15) COMP-3 h) PIC S9(18) COMP i) PIC S9(3) COMP-3 j) PIC S9(5) SIGN IS TRAILING SEPARATE CHARACTER

Exercise 17: Optimal USAGE Selection

For each of the following use cases, recommend the optimal USAGE clause and write the complete data definition. Justify your choice.

a) Account balance used in financial calculations b) Transaction counter used as a loop index c) Interest rate used in interest calculations d) Account number displayed on reports but not used in arithmetic e) Array subscript for a 500-element table

Exercise 18: COMP-3 Hex Representation

Write the hexadecimal representation (byte by byte) for the following values stored in COMP-3. Show the nibble layout.

a) PIC S9(5) COMP-3, value = +12345 b) PIC S9(5) COMP-3, value = -12345 c) PIC S9(7)V99 COMP-3, value = +1234567.89 d) PIC S9(3) COMP-3, value = +042 e) PIC 9(5) COMP-3, value = 00100 (unsigned)

Exercise 19: Mixed USAGE Arithmetic

Consider the following fields and arithmetic statement:

       01  WS-PRICE     PIC S9(5)V99 COMP-3 VALUE 129.99.
       01  WS-QUANTITY   PIC 9(4) COMP VALUE 25.
       01  WS-TAX-RATE   PIC V9(4) DISPLAY VALUE .0825.
       01  WS-SUBTOTAL   PIC S9(7)V99 COMP-3.
       01  WS-TAX-AMT    PIC S9(5)V99 COMP-3.
       01  WS-TOTAL      PIC S9(7)V99 COMP-3.

a) What conversions does the compiler need to generate for: MULTIPLY WS-PRICE BY WS-QUANTITY GIVING WS-SUBTOTAL? b) Calculate the expected value of WS-SUBTOTAL. c) What conversions are needed for: MULTIPLY WS-SUBTOTAL BY WS-TAX-RATE GIVING WS-TAX-AMT? d) Calculate WS-TAX-AMT (with truncation to 2 decimal places). e) How could you redesign these fields to minimize conversions?

Exercise 20: SIGN Clause Comparison

For the value -12345 stored in PIC S9(5), describe the storage content for each SIGN clause option. Indicate the total bytes used and what each byte contains.

a) SIGN IS TRAILING (default) b) SIGN IS LEADING c) SIGN IS TRAILING SEPARATE CHARACTER d) SIGN IS LEADING SEPARATE CHARACTER


Tier 5: Comprehensive Exercises (Exercises 21-30)

Exercise 21: Complete Invoice Record

Design a complete COBOL invoice record structure that includes:

  1. Invoice header: Invoice number (8 digits), invoice date (YYYYMMDD with subfields), status with 88-levels (Open, Paid, Overdue, Void)
  2. Customer information: Customer ID, name, address (multi-level hierarchy)
  3. Line items: An occurs clause for up to 10 line items, each with item code, description, quantity, unit price (COMP-3), and extended price (COMP-3)
  4. Totals: Subtotal, tax rate, tax amount, invoice total (all COMP-3)
  5. Corresponding edited output fields for printing the invoice

Exercise 22: Data Validation Program Design

Design the DATA DIVISION for a program that validates customer input records. Include:

a) An input record with fields for: name, address, city, state, ZIP, phone, email, SSN, date of birth, account type b) Level 88 conditions for all code fields c) An error record structure that captures: field name, error code, error message d) Counters for: records read, records valid, records with errors, total errors e) Use appropriate USAGEs for all fields

Write the WORKING-STORAGE SECTION with all necessary data definitions.

Exercise 23: REDEFINES for Variable-Format Record

A legacy payroll file contains records where the same positions hold different data depending on the employee type. Design a record layout using REDEFINES:

  • Salaried employees (type "S"): Annual salary (PIC S9(7)V99 COMP-3), bonus percentage (PIC 9V99), exempt status (PIC X with 88-levels)
  • Hourly employees (type "H"): Hourly rate (PIC S9(3)V99 COMP-3), regular hours (PIC 9(2)V9), overtime hours (PIC 9(2)V9)
  • Commissioned employees (type "C"): Base salary (PIC S9(7)V99 COMP-3), commission rate (PIC 9V99), territory code (PIC X(4))

All share: Employee ID (PIC 9(6)), name (PIC X(30)), type code (PIC X, position 37), and the type-specific data occupies positions 38-55.

Exercise 24: Report Layout Design

Design the DATA DIVISION entries for a monthly account statement report. Include:

a) Report title line with bank name and date b) Account header line with account number and customer name c) Column header line for transaction detail d) Detail line with: date (MM/DD/YYYY), description (30 chars), debit amount ($$$,$$9.99), credit amount ($$$,$$9.99), running balance (-$$$,$$$,MATH5$,$$9.99. ADD WS-TAX TO WS-DISPLAY-AMT.


**Bug C:**
```cobol
       01  WS-YEAR     PIC 9(2) VALUE 26.
       IF WS-YEAR > 99
           DISPLAY "INVALID YEAR"
       END-IF.

Bug D:

       01  WS-RESULT   PIC S9(3)V99 COMP-3.
       MULTIPLY 999.99 BY 999.99
           GIVING WS-RESULT.

Bug E:

       01  WS-NAME     PIC X(10).
       MOVE "CHRISTOPHER" TO WS-NAME.
       IF WS-NAME = "CHRISTOPHER"
           DISPLAY "MATCH"
       END-IF.

Exercise 28: Cross-Platform Data Design

You are designing a COBOL data record that will be: 1. Created on an IBM mainframe (EBCDIC, big-endian) 2. Transferred via FTP to a Linux server (ASCII, little-endian) 3. Processed by a COBOL program on the Linux server

Discuss the data representation issues for each of the following field types and recommend the best USAGE for portability:

a) A 10-character account number b) A dollar amount PIC S9(9)V99 c) A signed integer counter PIC S9(4) d) A date field PIC 9(8)

Exercise 29: Performance Analysis

A financial processing program performs 10 million arithmetic operations per run. Each operation involves adding a transaction amount to an account balance. Analyze the performance implications of the following three approaches:

Approach A: Both fields use COMP-3

       01  WS-TRAN-AMT     PIC S9(9)V99 COMP-3.
       01  WS-ACCT-BAL     PIC S9(11)V99 COMP-3.

Approach B: Both fields use DISPLAY

       01  WS-TRAN-AMT     PIC S9(9)V99.
       01  WS-ACCT-BAL     PIC S9(11)V99.

Approach C: Mixed USAGEs

       01  WS-TRAN-AMT     PIC S9(9)V99 COMP-3.
       01  WS-ACCT-BAL     PIC S9(11)V99 DISPLAY.

Discuss: storage efficiency, arithmetic performance on mainframe, conversion overhead, and your recommendation.

Exercise 30: Complete System Data Model

Design the complete DATA DIVISION (WORKING-STORAGE SECTION only) for a payroll processing program. The program must handle:

  1. Employee master record with personal info, tax filing status (88-levels for Single, Married, Head of Household), department, salary/rate
  2. Pay period record with period dates, regular hours, overtime hours, sick hours, vacation hours
  3. Earnings record with gross pay, federal tax, state tax, FICA, Medicare, 401k deduction, health insurance deduction, net pay
  4. Year-to-date accumulators for all earnings and deductions
  5. Tax table (simplified) with federal tax brackets using OCCURS and 88-levels
  6. Printed pay stub output record with all edited fields
  7. Processing counters and flags

Use appropriate USAGE clauses throughout. Include level 88 conditions for all status codes. Use COMP-3 for all monetary amounts and COMP for all counters.


Answer Key Notes

  • Solutions for exercises 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 14, 16, 17, 21, and 23 are provided in code/exercise-solutions.cob.
  • Remaining exercises are designed for open-ended responses; discuss with your instructor or study group.
  • For exercises involving storage calculations, verify your answers against the formulas in Chapter 3, Section 3.6.
  • For exercises involving output formatting, compile and run the programs using GnuCOBOL to verify your predictions: cobc -x -free exercise-solutions.cob && ./exercise-solutions