# Programming Example: Three Programming Traps ### Problem Statement

The purpose of this program is to show you three common programming traps:

• A**B**C is not equal to (A**B)**C.
• Dividing an integer with another integer always yields an integer result.
• In PARAMETER, assignment statement and READ, strings may be truncated if the length of the variable at the receiving end is not long enough. ### Solution

```! ------------------------------------------------------------
!     This program illustrates the following points:
!     (1)  The exponential trap:
!          That is, A**B**C is equal to A**(B**C) rather
!          than (A**B)**C.
!     (2)  The integer division trap:
!          That is, 4/6 is ZERO in Fortran rather than
!          a real number 0.666666
!          Function REAL() is used to illustrate the
!          differences.
!     (3)  The string truncation trap:
!          What if the length assigned to a CHARACTER
!          is shorter than the length of the string you
!          expect the identifier to have?  The third part
!          shows you the effect.
! ------------------------------------------------------------

PROGRAM      Fortran_Traps
IMPLICIT     NONE

INTEGER, PARAMETER          :: A = 2, B = 2, H = 3
INTEGER, PARAMETER          :: O = 4, P = 6
CHARACTER(LEN=5), PARAMETER :: M = 'Smith', N = 'TEXAS'
CHARACTER(LEN=4), PARAMETER :: X = 'Smith'
CHARACTER(LEN=6), PARAMETER :: Y = 'TEXAS'

!  The exponential trap

WRITE(*,*)   "First, the exponential trap:"
WRITE(*,*)   A, ' ** ', B, ' ** ', H, ' = ', A**B**H
WRITE(*,*)   '( ', A, ' ** ', B, ' ) **', H, ' = ', (A**B)**H
WRITE(*,*)   A, ' ** ( ', B, ' ** ', H, ' ) = ', A**(B**H)
WRITE(*,*)

!  The integer division trap.  Intrinsic function REAL() converts
!  an integer to a real number

WRITE(*,*)   "Second, the integer division trap:"
WRITE(*,*)
WRITE(*,*)   O, ' / ', P, ' = ', O/P
WRITE(*,*)   'REAL( ', O, ' ) / ', P, ' = ', REAL(O)/P
WRITE(*,*)   O, ' / REAL( ', P, ' ) = ', O/REAL(P)
WRITE(*,*)

!  The string truncation trap

WRITE(*,*)   "Third, the string truncation trap:"
WRITE(*,*)   'IS ', M, ' STILL IN ', N, '?'
WRITE(*,*)   'IS ', X, ' STILL IN ', Y, '?'

END PROGRAM  Fortran_Traps

```
Click here to download this program. ### Program Output

``` First, the exponential trap:
2 ** 2 ** 3 = 256
( 2 ** 2 ) **3 = 64
2 ** ( 2 ** 3 ) = 256

Second, the integer division trap:

4 / 6 = 0
REAL( 4 ) / 6 = 0.666666687
4 / REAL( 6 ) = 0.666666687

Third, the string truncation trap:
IS Smith STILL IN TEXAS?
IS Smit STILL IN TEXAS ?
``` ### Discussion

• All names in this program are aliases of constants.
• Consider the first group. Variables A, B and H are aliases of 2, 2 and 3. The first WRITE computes A**B**H, which is equivalent to A**(B**H), and the result is 2**(2**3)=256. The second WRITE computes (A**B)**C and the result is (2**2)**3=64. The third WRITE computes A**(B**H) and the result is 2**(2**3)=256. Thus, it is clear that A**B**H equal to A**(B**H).
• The second group illustrates the problem unique to integer division. Two integer aliases are involved, namely O and P with values 4 and 6, respectively. The first WRITE displays O/P and the result is 4/6=0 since it is an integer division. The second WRITE converts O to real with intrinsic function REAL(). Thus, in computing REAL(O)/P, the expression is REAL(4)/6, which becomes 4.0/6 and then 4.0/6.0. Thus, the result is 0.6666667. The third WRITE should give the same result.
• Go back to the top of this program. Alias M and N should have no problem since the length of the names and the length of the strings agree. Since the length of X is 4 and is shorter than the length of string 'Smith', X only receives the left-most 4 characters. Now take a look at Y. Since the length of Y is longer than the length of string 'TEXAS', spaces will be appended to the end to fill up to 6 characters. Thus, Y actually becomes 'TEXAS '. The output should look like the following:
```IS Smith STILL IN TEXAS?
IS Smit STILL IN TEXAS ?
```
On the second line, it is easily seen that the original Smith becomes Smit and the original TEXAS becomes TEXAS_, where _ indicates a space.