CHAPTER 7

 

COMPUTING IN COBOL:

THE ARITHMETIC VERBS AND INTRINSIC FUNCTIONS

 

 

CHAPTER OBJECTIVES

 

After completion of this chapter, the student should be able to:

 

1.         Describe the formats and rules for the ADD, SUBTRACT, MULTIPLY, and DIVIDE statements.

 

2.         Explain the purpose of the REMAINDER clause.

 

3.         Explain the purpose of the ROUNDED option.

 

4.         Explain what happens when truncation occurs.

 

5.         Explain what happens when an overflow error occurs.

 

6.         Explain how the ON SIZE ERROR option can be used to detect an overflow condition.

 

7.         Define imperative statement.

 

8.         Define the term scope terminator.

 

9.         Describe the use of the NOT ON SIZE ERROR clause.

 

10.       Explain how the size of the receiving field in an arithmetic operation can be determined.

 

11.       Describe the format of the COMPUTE statement.

 

12.       Explain how to define a numeric field that can contain a negative value.

 

13.       Explain the rules for performing arithmetic with fields containing signed numbers.

 

14.       Explain how to code the PIC clause of a signed input number so that the sign may be entered separately.

 

15.       Describe the use of each of the 42 intrinsic functions.


LECTURE OUTLINE

 

I.          The Basic Arithmetic Verbs

 

            A.        General - Applies to All Four Arithmetic Verbs

 

1.         Fields used in arithmetic operations (ADD, SUBTRACT, MULTIPLY, and DIVIDE) must be defined with numeric PICTURE clauses.

 

2.         Fields must contain numeric data when the arithmetic operation is actually executed.

 

B.         ADD Statement

 

1.         The ADD statement has two instruction formats:

FORMAT 1:

ADD {identifier-1/literal-1} ...

     TO identifier-2 ...

FORMAT 2:

ADD {identifier-1/literal-1} ...

GIVING identifier-2 ...

 

2.         Rules for Addition:

a.         All literals and fields that are part of the addition must be numeric.  The field after the word GIVING may be a report-item.

b.         The resultant field, following the word TO or the word GIVING, must be a data-name and not a literal.  The resultant field is the only field changed as a result of the ADD operation.

c.         When using the TO format, the data-name following the word TO is the receiving field.  The contents of the receiving field are part of the ADD operation and are added along with the other fields.

d.         When using the GIVING format, the data-name following the word GIVING is the receiving field.  It will contain the sum of the other fields, but its original contents will not be part of the ADD operation.  It may be either a numeric field or a report-item.

e.         The words TO and GIVING may be specified in the same statement.

 


3.         It is possible to ADD multiple fields in the same ADD statement.  When doing so, commas followed by at least one space may be used to separate operands, but they are optional.

 

4.         It is possible to name multiple receiving fields in the same ADD operation using the TO format.

 

5.         Use the GIVING format of the ADD statement when the contents of the operands need to be retained.  When the original contents of the operand will no longer be needed after the addition, the TO format may be used.

 

C.        SUBTRACT Statement

 

1.         The SUBTRACT statement has two formats:

 

FORMAT 1:

 

SUBTRACT {identifier-l/literal-l} ...

     FROM identifier-2 ...

 

FORMAT 2:

 

SUBTRACT {identifier-l/literal-l} ...

     FROM {identifier-2/literal-2}

     GIVING identifier-3 ...

 

2.         Rules for Subtraction:

a.         All literals and data-names that are part of the subtraction must be numeric.  The receiving field after the word GIVING may be a report-item.

b.         The receiving field must be a data-name, not a literal.

c.         All fields and literals preceding the word FROM are added together and the sum is subtracted from the field following the word FROM.

d.         When using the GIVING format, the result of the subtraction is placed in the field following the word GIVING.  The initial contents of the receiving field do not take part in the arithmetic operation.

 

3.         When deciding whether to use Format 1 or Format 2 of the SUBTRACT statement, apply the same principles as for the ADD statement.

 


4.         It is possible to SUBTRACT multiple fields in the same SUBTRACT statement using Format 1. When doing so, commas may be used to separate operands, but they are optional.

 

5.         It is possible to perform several SUBTRACT operations with a single statement using Format 1.

 

6.         For Format 2, any number of identifiers or literals may follow the word SUBTRACT and any number of identifiers may follow the word GIVING, but only one literal or identifier may follow the word FROM.

 

D.        MULTIPLY and DIVIDE Statements

 

1.         The MULTIPLY statement has two formats:

 

FORMAT 1:

 

MULTIPLY {identifier-1/literal-1}

     BY identifier-2

 

FORMAT 2:

 

MULTIPLY {identifier-l/literal-l}

     BY {identifier-2/literal-2}

     GIVING identifier-3 ...

 

2.         The DIVIDE statement has three formats:

 

FORMAT 1:

 

DIVIDE {identifier-1/literal-1}

     INTO identifier-2 ...

 

FORMAT 2:

 

DIVIDE {identifier-1/literal-1}

     INTO {identifier-2/literal-2}

     GIVING identifier-3 ...

 

FORMAT 3:

 

DIVIDE {identifier-l/literal-l}

     BY {identifier-2/literal-2}

     GIVING identifier-3 ...

 

3.         Either the word INTO or BY may be used with a DIVIDE statement.  The GIVING clause is optional with INTO but required with BY.

 

4.         MULTIPLY and DIVIDE rules:

 

a.         The result field must always be a data-name or identifier and never a literal.

b.         The MULTIPLY and DIVIDE operations can have more than one resultant field.

c.         Always make sure the receiving field is large enough to store the result.

d.         With each MULTIPLY or DIVIDE statement only two operands may be involved in the arithmetic operation.

 

5.         The REMAINDER clause can be used to store the remainder of a division operation.  The following DIVIDE formats using the REMAINDER clause may be used:

 

FORMAT 4:

 

DIVIDE {identifier-l/literal-l}

     INTO {identifier-2/literal-2}

     GIVING identifier-3 REMAINDER identifier-4

 

FORMAT 5:

 

DIVIDE {identifier-l/literal-l}

     BY {identifier-2/literal-2}

     GIVING identifier-3 REMAINDER identifier-4

 

6.         The use of the REMAINDER clause is optional and does not alter the results of the original DIVIDE operation.

 

7.         Review the summary of arithmetic operations in Table 7.1 in this section of the text.

 

II.         Options Available With Arithmetic Verbs

 

A.        ROUNDED Option

 

1.         The ROUNDED option may be specified with any arithmetic operation.  If used, it directly follows the data-name specified as the receiving field.

 

2.         When the resultant field cannot accommodate all the decimal positions in the answer and the ROUNDED option is not specified, truncation of decimal positions will occur.

 


3.         When the ROUNDED option is used, the result will be rounded to the PICTURE specification of the receiving field.

 

4.         If ROUNDED and REMAINDER are used in the same DIVIDE, ROUNDED must appear first.

 

B.         ON SIZE ERROR Option

 

1.         An overflow or size error occurs when the resultant field does not have enough integer positions to store the result of an arithmetic operation.  An overflow condition causes truncation, which will produce erroneous results.

 

2.         The best way to avoid a size error condition is to be absolutely certain that the receiving field is large enough to accommodate any possible result.

 

3.         The ON SIZE ERROR will trap instances in which truncation of high-order digits would otherwise occur.

4.         Format of the ON SIZE ERROR clause:

arithmetic statement

     [ON SIZE ERROR imperative statement ...]

 

5.         Imperative statement means any COBOL statement that gives a direct command and does not perform a test.

 

6.         The computer will ignore the ON SIZE ERROR clause if no on size error condition occurs.  If a size error does occur, then the computer will not perform the arithmetic operation, but will instead execute the statements in the SIZE ERROR clause.

 

7.         Since division by zero is undefined, an attempt to divide by zero will cause a SIZE ERROR condition.

 

8.         An ON SIZE ERROR clause will follow the ROUNDED and REMAINDER options if they are used in the same statement.

 

9.         A clause such as the ON SIZE ERROR must be explicitly terminated.  All statements prior to a terminator will be considered to be a portion of the clause.  Failure to do this correctly may result in a logic error.

 


10.       There are two ways to terminate a clause:

a.         End the last statement included in the clause with a period. (not recommended)

b.         End the statement with a scope terminator.  (recommended)

 

C.        NOT ON SIZE ERROR Clause

 

1.         COBOL includes a NOT ON SIZE ERROR clause that may be used with any arithmetic operation.

 

2.         Both ON SIZE ERROR and NOT ON SIZE ERROR can be specified in a single arithmetic statement.

 

D.        Determining the Size of Receiving Fields

 

1.         For ADD operations, determine the largest quantities that can be held in the fields and manually perform an addition.

 

2.         For SUBTRACT operations, subtract the smallest possible number from the largest possible number manually.

 

3.         For MULTIPLY operations, add the number of digits to the left of the decimal points in the fields being multiplied to determine the number of positions needed to the left of the decimal point in the receiving field.  Similarly, add the number of digits to the right of the decimal points in the fields to be multiplied to determine the number of positions needed to the right of the decimal point in the receiving field.

 

4.         For DIVIDE operations, determine the range of possible values based upon the size of the fields involved and establish the size of the receiving field accordingly.

 

III.       The COMPUTE Statement

 

A.        Basic Format

 

1.         The COMPUTE verb allows calculations to be coded in the form of a mathematical equation.

 


2.         The COMPUTE uses arithmetic symbols rather than arithmetic verbs:

 

ADD                                        +

SUBTRACT                            -

MULTIPLY                             *

DIVIDE                                   /

EXPONENTIATION              **

 

3.         The COMPUTE statement has an identifier to the left of the equal sign.  The value computed in the equation to the right of the equal sign is placed in the field to the left of the equal sign.

 

4.         All fields and literals operated on in a COMPUTE statement must be numeric.

 

5.         The COMPUTE statement may include more than one arithmetic operation.

 

6.         The equal sign and arithmetic symbols must be both preceded and followed by a space.

 

7.         When using ROUNDED and ON SIZE ERROR with the COMPUTE statement, the format is as follows:

 

COMPUTE identifier-1 ROUNDED ... =

    {arithmetic expression-1/

        literal-1/identifier-2}

    ON SIZE ERROR imperative statement ...

 

8.         END-COMPUTE and NOT ON SIZE ERROR are permitted.

 

B.         Order of Evaluation

 

1.         The order in which arithmetic operations are performed will affect the results in a COMPUTE statement.

 

2.         The sequence in which operations are performed in a COMPUTE statement are:

            a.         **

            b.         * or /                (equal order, evaluated left to right)

            c.         + or -               (equal order, evaluated left to right)

            d.         (   )                   The use of parentheses overrides rules a-c.  All

                                    operations within parentheses are performed

                                    first.

 

C.        Comparing COMPUTE to Arithmetic Verbs

 

1.         Any calculation can be performed using either the four arithmetic verbs or the COMPUTE. (Note: Exponentiation can be performed as a series of MULTIPLY commands.)

 

2.         A rule of thumb is:  If one arithmetic statement will do the job, use it; if it takes more than one, use a COMPUTE.

 

3.         Different compilers handle decimal rounding in the COMPUTE statement differently when there is more than one operation performed in the statement.  This will be standardized in COBOL 2002+.

 

IV.       Use of Signed Numbers in Arithmetic Operations

 

A.        The Use of S in PIC Clauses for Fields That Can Be Negative

 

1.         If a field may be negative or if a calculation may produce negative results, the letter S must be used in the PICTURE clause of the field.

 

2.         The S, like an implied decimal point, does not use a storage position.

 

B.         Rules for Performing Arithmetic with Signed Numbers

 

1.         Multiplication:

a.         The product is positive if the multiplicand and multiplier have the same sign.

b.         The product is negative if the multiplicand and multiplier have different signs.

 

2.         Division:

a.         The quotient is positive if the dividend and divisor have the same sign.

b.         The quotient is negative if the dividend and divisor have different signs.

3.         Addition:

a.         If the signs of the fields being added are the same, add and use that sign.

b.         If signs of the fields being added are different, add the positive and negative numbers separately.  Then subtract the smaller total from the larger total and use the sign of the larger.

 

4.         Subtraction: Change the sign of the subtrahend and proceed as in addition.


 

C.        Entering Signed Numbers

 

1.         By default, a number that has an S in its PICTURE clause contains the sign as part of its rightmost digit.  This convention makes it inconvenient to key in signed numbers because it requires the use of special codes for entering the rightmost digit.

 

2.         To enter the sign as a separate character, use S in the PICTURE clause along with the following clause after the PICTURE clause in the DATA DIVISION:

 

[SIGN IS] {LEADING/TRAILING} SEPARATE CHARACTER

 

 

V.        Intrinsic Functions

 

            A.        General - Applies to All Intrinsic Functions

 

1.         42 intrinsic functions have been added as an extension to COBOL 85, and more will be included in the new standard.

 

2.         When coded, an intrinsic function must be preceded by the word FUNCTION.  Any arguments, which are enclosed in parentheses and separated by commas, follow the intrinsic function name.

 

3.                  The remainder of this section lists each of the 42 functions with brief descriptions.  Refer to the text for detailed explanations and examples.

 

B.         Calendar Functions

 

(Note that all arguments in the date functions must be elementary items.)

 

CURRENT-DATE

Returns 21 characters of day and time information

WHEN-COMPILED

Returns 21 characters of compile date and time information

INTEGER-OF-DATE

Converts a yyyymmdd calendar date to an integer

INTEGER-OF-DAY

Converts a Julian yyyyddd date to an integer

DATE-OF-INTEGER

Converts an integer date to yyyymmdd calendar format

DAY-OF-INTEGER

Converts an integer date to Julian yyyyddd date format

 


C.        Statistical and Numerical Analysis Functions

 

INTEGER

Returns the largest integer that does not exceed the argument value

INTEGER-PART

Returns the integer portion of the argument

MAX

Returns the largest value in a list of arguments

MIN

Returns the smallest value in a list of arguments

MEAN

Returns the average of all values in a list of arguments

NUMVAL

Returns the pure numeric value in a simple edited report item

NUMVAL-C

Returns the pure numeric value in an edited report item that can be more complex

MIDRANGE

Returns the average between the lowest and highest values in a list of arguments

MEDIAN

Returns the middle value from a list of arguments

FACTORIAL

Returns the factorial of a number

REM

Returns the remainder obtained when argument-1 is divided by argument-2

MOD

Returns the greatest integer value of the remainder obtained when argument-1 is divided by argument-2

ORD-MAX

Returns the position of the maximum value in a list

ORD-MIN

Returns the position of the minimum value in a list

RANGE

Returns the difference between the largest value and the smallest value in a list.

SUM

Returns the sum of values in a list

SQRT

Returns the square root of an argument

LOG

Returns the natural logarithm of a number

LOG10

Returns the logarithm to base 10 of a number

STANDARD-DEVIATION

Returns the standard deviation

VARIANCE

Returns the variance

RANDOM

Returns a random number

 

D.        Trigonometric Functions

 

SIN

Returns the sine of an argument

COS

Returns the cosine of an argument

TAN

Returns the tangent of an argument

ASIN

Returns the arc sine of an argument

ACOS

Returns the arc cosine of an argument

ATAN

Returns the arc tangent of an argument

 


E.         Financial Functions

 

ANNUITY

Returns the value of an investment over time

PRESENT-VALUE

Returns the amount to be invested today to obtain some desired future value

 

F.         Character and String Functions

 

LOWER-CASE

Returns in lowercase alphabetic mode the value of an argument

UPPER-CASE

Returns in uppercase alphabetic mode the value of an argument

LENGTH

Returns the length or number of positions in an argument

ORD

Returns the ASCII code for a character

CHAR

Returns the character equivalent of an ASCII code

REVERSE

Returns the value of an argument in reverse order

 

VI.       COBOL 2000+ Changes

 

A.        Spaces around arithmetic operators will be no longer be required.

 

B.         The COMPUTE statement will yield the same results regardless of the compiler used, by making the precision or number of decimal places in each intermediate calculation fixed.

 


SOLUTIONS TO REVIEW QUESTIONS

 

I.          True-False Questions

 

1.         T

 

2.         F          The word AND is not permitted.

 

3.         T

 

4.         F          A literal may not follow the word BY.

 

5.         T

 

6.         F          The * will be performed first.

 

7.         F          There is no arithmetic verb corresponding to the ** used by COMPUTE for

xponentiation.

 

8.         T

 

9.         T

 

10.       T

 

II.      General Questions

 

 

A = 3, B = 2, X = 5

(PIC for each is 99V99)

 

COBOL Statement

Result In

Contents

1.

ADD A B GIVING X

X

05Ù00

2.

ADD A B TO X

    ON SIZE ERROR

    MOVE ZERO TO X

X

10Ù00

3.

DIVIDE A INTO B ROUNDED

B

00Ù67

 

 

4.         DIVIDE GAS INTO MILES GIVING MPG

          or

            DIVIDE MILES BY GAS GIVING MPG

 

5.         COMPUTE Y = (A + B) ** 2 / X

 

6.         Correct.

 

7.         The receiving field cannot be a literal.


8.         AMT cannot be followed by a period.  Only with a COBOL 85 compiler can both TO and GIVING be used in the same ADD statement.

 

9.         DIVIDE and MULTIPLY operations cannot be performed in the same statement.  It should be:

 

DIVIDE A BY B

MULTIPLY B BY C

 

10.       ROUNDED must appear to the left of the equal sign in a COMPUTE statement.

 

11.         COMPUTE EOQ = (2 * R * S / I) ** .5

 

12.       COMPUTE A = A + 1

 

13.       Assuming fractional portions of a pound of grass seed can be purchased, the following solution would be correct:

 

MULTIPLY LENGTH BY WIDTH GIVING SQUARE-FEET

COMPUTE COST = SQUARE-FEET / 1000 * 2.50

 

If grass seed could only be purchased by the pound, the following solution would be correct:

 

POUNDS    PIC S9(3).

 

MULTIPLY LENGTH BY WIDTH GIVING SQUARE-FEET

COMPUTE POUNDS = SQUARE-FEET / 1000 + .99999

MULTIPLY POUNDS BY 2.50 GIVING COST

 

14.       No.  Only one identifier may follow the word FROM in this format.

 

15.       a.         COMPUTE HOURS = OVERTIME-HOURS + HOURS

 

b.            ADD OVERTIME-HOURS TO HOURS

 

16.       a.         Cannot code COMPUTE with a REMAINDER.

b.            DIVIDE X BY 12 GIVING FEET REMAINDER INCHES

 

17.       a.             COMPUTE WEEK-END = FRI + SAT + SUN

b.            ADD FRI, SAT, SUN GIVING WEEK-END

 

18.       a.             COMPUTE TOTAL = TOTAL + AMT1 + AMT2 + AMT3

b.         ADD AMT1, AMT2, AMT3 TO TOTAL

 


19.       a.         COMPUTE AMT-X = AMT-X - 47.5

b.         SUBTRACT 47.5 FROM AMT-X

 

20.       a.             COMPUTE MONTHLY-TOTAL = YEARLY-TOTAL / 12

b.            DIVIDE 12 INTO YEARLY-TOTAL

    GIVING MONTHLY TOTAL

or

DIVIDE YEARLY-TOTAL BY 12 GIVING MONTHLY-TOTAL

 

21.              WORKING-STORAGE SECTION field definitions: 

 

01  CHARACTER-FIELDS.

         05  UPPER-CASE-WORD PIC X(7)   VALUE 'GOODBYE'.

         05  LOWER-CASE-WORD  PIC X(7).

 

PROCEDURE DIVISION code:

 

MOVE FUNCTION LOWER-CASE (UPPER-CASE-WORD)

         TO LOWER-CASE-WORD

     DISPLAY 'UPPER CASE WORD = ' UPPER-CASE-WORD

     DISPLAY 'LOWER CASE WORD = ' LOWER-CASE-WORD.

 

22.       The value of RESULT will be 3 (3 is the remainder obtained when 15 is divided by 4).

 


23.       WORKING-STORAGE SECTION field definitions: 

 

01  LEAP-YEAR-TEST-FIELDS.

         05  TEST-YEAR           PIC 9(4).

         05  REMAINDER-4         PIC 9(1).

         05  REMAINDER-400       PIC 9(3).

 

PROCEDURE DIVISION code:

 

DISPLAY 'ENTER A 4-DIGIT YEAR:  '

     ACCEPT TEST-YEAR

     MOVE FUNCTION REM (TEST-YEAR, 4) TO REMAINDER-4

     IF REMAINDER-4 = 0

         IF TEST-YEAR (3:2) = 0

            MOVE FUNCTION REM (TEST-YEAR, 400) TO REMAINDER-400

            IF REMAINDER-400 = 0

                DISPLAY TEST-YEAR ' IS A LEAP YEAR'

            ELSE

                DISPLAY TEST-YEAR ' IS NOT A LEAP YEAR'

            END-IF

         ELSE

            DISPLAY TEST-YEAR ' IS A LEAP YEAR'

         END-IF

     ELSE

         DISPLAY TEST-YEAR ' IS NOT A LEAP YEAR'

     END-IF.

 


24.       WORKING-STORAGE SECTION field definitions: 

 

01  DATE-CALCULATION-FIELDS.

         05  DATE-1              PIC  9(8).

         05  DATE-2              PIC  9(8).

         05  DATE-1-INTEGER      PIC  9(8).

         05  DATE-2-INTEGER      PIC  9(8).

         05  DAYS-BETWEEN-DATES  PIC S9(8) SIGN LEADING

                                           SEPARATE.

 

PROCEDURE DIVISION code:

 

DISPLAY 'ENTER THE FIRST DATE:   '

     ACCEPT DATE-1

     DISPLAY 'ENTER THE SECOND DATE:  '

     ACCEPT DATE-2

     MOVE FUNCTION INTEGER-OF-DATE (DATE-1) TO DATE-1-INTEGER

     MOVE FUNCTION INTEGER-OF-DATE (DATE-2) TO DATE-2-INTEGER

     SUBTRACT DATE-1-INTEGER FROM DATE-2-INTEGER

         GIVING DAYS-BETWEEN-DATES

     DISPLAY 'THERE ARE '

              DAYS-BETWEEN-DATES

             ' DAYS BETWEEN THE TWO DATES'.

 

25.       WORKING-STORAGE SECTION field definitions: 

 

01  INPUT-FIELDS.

         05  NUMBER-1        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-2        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-3        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-4        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-5        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-6        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-7        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-8        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-9        PIC S9(3)  SIGN LEADING SEPARATE.

         05  NUMBER-10       PIC S9(3)  SIGN LEADING SEPARATE.

01  STATISTICAL-FIELDS.

         05  WS-MEAN         PIC +999.99.

         05  WS-MEDIAN       PIC +999.9.

         05  WS-SUM          PIC +9999.

         05  WS-MAX          PIC +999.

         05  WS-MIN          PIC +999.

 


PROCEDURE DIVISION code:

 

DISPLAY 'ENTER 10 SIGNED, 3-DIGIT INTEGERS (SAMPLE: -123)'

     DISPLAY 'PRESS ENTER AFTER EACH NUMBER'

     ACCEPT NUMBER-1

     ACCEPT NUMBER-2

     ACCEPT NUMBER-3

     ACCEPT NUMBER-4

     ACCEPT NUMBER-5

     ACCEPT NUMBER-6

     ACCEPT NUMBER-7

     ACCEPT NUMBER-8

     ACCEPT NUMBER-9

     ACCEPT NUMBER-10

     MOVE FUNCTION MEAN (NUMBER-1, NUMBER-2, NUMBER-3,

                         NUMBER-4, NUMBER-5, NUMBER-6,

                         NUMBER-7, NUMBER-8, NUMBER-9,

                         NUMBER-10)

          TO WS-MEAN

     MOVE FUNCTION MEDIAN (NUMBER-1, NUMBER-2, NUMBER-3,

                           NUMBER-4, NUMBER-5, NUMBER-6,

                           NUMBER-7, NUMBER-8, NUMBER-9,

                           NUMBER-10)

          TO WS-MEDIAN

     MOVE FUNCTION SUM (NUMBER-1, NUMBER-2, NUMBER-3,

                        NUMBER-4, NUMBER-5, NUMBER-6,

                        NUMBER-7, NUMBER-8, NUMBER-9,

                        NUMBER-10)

          TO WS-SUM

     MOVE FUNCTION MAX (NUMBER-1, NUMBER-2, NUMBER-3,

                        NUMBER-4, NUMBER-5, NUMBER-6,

                        NUMBER-7, NUMBER-8, NUMBER-9,

                        NUMBER-10)

          TO WS-MAX

     MOVE FUNCTION MIN (NUMBER-1, NUMBER-2, NUMBER-3,

                        NUMBER-4, NUMBER-5, NUMBER-6,

                        NUMBER-7, NUMBER-8, NUMBER-9,

                        NUMBER-10)

          TO WS-MIN

     DISPLAY 'MEAN   = ' WS-MEAN

     DISPLAY 'MEDIAN = ' WS-MEDIAN

     DISPLAY 'SUM    = ' WS-SUM

     DISPLAY 'MAX    = ' WS-MAX

     DISPLAY 'MIN    = ' WS-MIN.


III.    Internet/Critical Thinking Questions

 

1.

 

Search Engine:  yahoo.com>Computers&Internet

Keywords:                   +COBOL + standard

URL:                            http://www.cobolstandards.com/

Contents:                      J4: COBOL Standardization status.  Click on “Description of new features” and then look up “Intrinsic functions.”

 

Search Engine:  altavista.com

Keywords:                   +COBOL + “intrinsic functions”

URL:                            http://www.cs.ntu.edu.au/homepages/alexk/sit231/cobol/ref7.html

Contents:                      Definition of intrinsic function and a list of the intrinsic functions

 

Search Engine:  excite.com>

Keywords:                   COBOL + "intrinsic functions”

URL:                            http://www.adtools.com/info/whitepaper/coby2k.pdf

Contents:                      Paper on COBOL 2002+, includes material on COBOL 2002+

 


2.

 

Search Engine:  yahoo.com>Computers & Internet

Keywords:                   “Julian date”

URL:                            http://whatis.techtarget.com/definition/0,,sid9_gci212429,00.html

Contents:                      What Is a Julian date (a definition).  Includes links to other web sites.

 

Search Engine:  yahoo.com>Computers & Internet

Keywords:                   “Julian date”

URL:                            http://www.capecod.net/~pbaum/date/date0.htm

Contents:                      Date Algorithms

 

Search Engine:  yahoo.com>Computers & Internet

Keywords:                   “Julian date”

URL:                            http://pacific.commerce.ubc.ca/xr/julian.html

Contents:                      Julian date definition and converter

 

Search Engine:  hotbot.com

Keywords:                   COBOL + “intrinsic functions”

URL:                            http://www.cis.ohio-state.edu/~sgomori/314/intrinsic.html

Contents:                      Discussion of Date Functions and others.  Has link for Julian dates.

 


SOLUTIONS TO DEBUGGING EXERCISES

 

1.         1)         Correct.

 

2)         Correct.

 

ADD AMT1, AMT2 GIVING AMT3

 

3)         Correct.  Although the syntax is correct, however, this is probably not what the programmer had intended.  See Debugging Exercise 2 for a corrected statement.

 

4)         ROUNDED must be to the left of the equal sign. 

 

COMPUTE AMT4 ROUNDED = AMT1 + AMT2

 

5)         Correct.

 

6)         The receiving field cannot be a literal.

 

DIVIDE 2 INTO AMT1

 

or

 

DIVIDE AMT1 BY 2 GIVING AMT1

 

7)         The word TIMES is not permitted.

 

MULTIPLY AMT4 BY AMT3

 

2.         No.  COMPUTE AVERAGE = (AMT1 + AMT2) / 2.

 

3.         If the result of the MULTIPLY operation is greater than 9,999.99, high-order truncation will occur.  To avoid this potential problem:

 

a.         Use the GIVING option and specify a receiving field AMT3 with a PICTURE clause of 9(6)V9(4).

b.         Keep the statement as coded and use the ON SIZE ERROR clause to trap the possibility of obtaining a result with more than 4 digits to the left of the decimal place.  If truncation of digits to the right of the decimal place is an issue, ROUNDED might be used if appropriate to the application.

 

4.         AMT1 = the original value of AMT1 divided by 2

            AMT2 = the original value of AMT2 times the original value of AMT1

            AMT3 = the square of the sum (AMT1 + AMT2)

            AMT4 = the sum of the original values of AMT1 and AMT2

            AVERAGE = the average of the original values of AMT1 and AMT2