Computers in Engineering - 308-208 Lecture 4
Computers in Engineering - 308-208


Lesson 4 - Learning Goals



4.1 More on data types

4.2 Arithmetic operations

4.3 Concept of a computer variable

4.4 Declaration statements

4.5 Implicit declarations in FORTRAN

4.6 Integer division

4.7 Mixed Mode arithmetic

4.8 FORTRAN 90 - PRINT statement

4.9 Design and setup of a complete program



The Four Basic FORTRAN Data Types

1. INTEGERS

... -3 -2 -1 0 1 2 3 ...

          +       add

          -       subtract

          *       multiply

          /       integer divide : 17/5 = 3 (remainder of 2 thrown away)

          **      raise to power = exponentiation

2. REALS

  .16  0.   00014   -0.3   6.02E23 (means 6.02 x l023)

         +       add

         -       subtract

         *       multiply

          /       integer divide : 17./5. = 3.4

          **    raise to power = exponentiation

3. CHARACTER DATA

     'HELLO, WORLD'   'PROF. G. RATZER'   '398-7082'

          '"HOW''S IT GOIN'', EH?" '

4. LOGICAL DATA

          .TRUE.   .FALSE.

          .AND.     logical AND -- result true if both are true

          .OR.        logical OR -- result true if at least one is true

          .NOT.     changes .TRUE. to .FALSE. and vice versa


Data Types

DATA TYPE
MAIN FRAME
MICRO
CHARACTER
8 BITS =
1 BYTE
8 BITS
INTEGER
32 BITS
16 BITS
REAL
32 BITS
32 BITS
LOGICAL
1 BIT
1 BIT

FORTRAN Also Allows

DATA TYPE
SIZE
INTEGER (LEN=2)
2 BYTES
REAL (LEN=4)
4 BYTES
REAL (LEN=8)
8 BYTES
DOUBLE PRECISION
8 BYTES
COMPLEX
8 BYTES

.

.

.

MORE


FORTRAN Variables

  1- A quantity that can vary.

  2- A location in memory.

  3- A name used to refer to.

Rules for Choosing a Name

  A) 1 to 6 characters ( FORTRAN 77 )

          or 1 to 31 characters ( FORTRAN 90 )

  B) First character must be A ... Z, rest A...Z plus 0...9

  EXAMPLES :

          TODAY

          SINGLE

          MAX

          MIN

          X

          ZMAT

          APOLLO13 -> NOT VALID IN FORTRAN 77

          THISISTOOLONGTOBEAGOODVARIABLENAME -> NOT VALID

          7UP -> NOT VALID

          !ALLO -> NOT VALID

          22CATCH -> NOT VALID

Declaration Statements

For each data type and corresponding constant there are what are called declaration statements, which are used to define the type of data to be associated with a chosen FORTRAN variable. Thus

          INTEGER :: K, PTR

          REAL :: SUM, COST, VALUE

          CHARACTER(LEN=20) :: NAME, ADDRSS

          LOGICAL :: SINGLE
The declaration statements should be placed at the beginning of a program, before any executable statements like a PRINT statement. The above declarations define K and PTR to be INTEGER variables and any data assigned to these variables will be stored as a whole number, without any fractional part.

Likewise the variables SUM, COST and VALUE should be used in calculations which may require a numeric value with a fractional part.

The CHARACTER statement usually has a length attribute - namely 20. This specifies that both the variables NAME and ADDRSS may be used to store a string of up to 20 alphanumeric characters.

The LOGICAL variable SINGLE may only be assigned the value .TRUE. or .FALSE.


Implicit Declaration in FORTRAN

You should ALWAYS declare all the variables you will use in your program.

If you forget, FORTRAN will assume that variable names starting with one of these letters (FIRST letter of variable name):

I J K L M N

are INTEGER variable otherwise, they are implicitly declared as REAL VARIABLE.

EXAMPLE :

1) INTEGER :

          INUM

          INDEX

          LSUM

          MONTH

2) REAL :

          TOTAL

SUM

VALUE

TITLE


Common FORTRAN ''Gotcha's''

1) Integer conversions

2) Undeclared variables

3) Priority of operators

Priority
Operations
Order
Highest
( )
  
  
**
Right to left
  
* /
Left to right
Lowest
+ -
Left to right

    2**2**3 = 223 = 28 = 256

    B/C*D = (B*D)/C

4) Watch your punctuation !

   DO J=1.10

   PRINT *,J

   END DO

The rules governing Mixed Mode expressions are as follows.

1. Apply the priority rules for the selection of the arithmetic operations to be performed first.

          ( )     High priority

          **     Medium priority

          * /     Same priority

          + ­     Low priority

Operators with the same priority in an assignment are processed from left to right.

2. If both of the selected variables or constants are INTEGER, do the operation in INTEGER.

3. IF both of the selected variables or constants are REAL, do the operation in REAL.

4. If one is INTEGER and one is REAL, convert the INTEGER data to REAL and do the operation in REAL.

Repeat the process until the expression is reduced to a single value. Note that internally, the computer must do arithmetic between data of the same type, both INTEGER or both REAL. Apart from these rules which apply to the evaluation of expressions on the right of the equal sign in an Assignment statement, there may still be conversion to a different type on the left of the equal sign.


! Arithmetic in print statements
!

PROGRAM ARITHMETIC
!

IMPLICIT NONE

PRINT *, '2+2 = ', 2+2

PRINT *, '2 ** 3 = ', 2**3

PRINT *, 'WITH REALS : ', 2.**3

PRINT *, 'PARENTHESES : (-2+3)*(4+5) = ', (-2+3)*(4+5)

PRINT *, 'WITHOUT : ', -2+3*4+5

PRINT *, 'LOGIC : ', .TRUE., .FALSE.

PRINT *, 'CONTRADICT : (NOT TRUE) = ', .NOT. .TRUE. 

PRINT *, 'AND : (TRUE AND FALSE) = ', .TRUE. .AND. .FALSE.

STOP

END PROGRAM ARITHMETIC

PROGRAM OUTPUT :

2+2 = 4

2 ** 3 = 8

WITH REALS : 8.00000 

PARENTHESES : (-2+3)*(4+5) = 9

WITHOUT : 15

LOGIC : T F

CONTRADICT : (NOT TRUE) = F

AND : (TRUE AND FALSE) = F

Assignment Statement

General form :

          Variable = Expression

Expression can be :

          1) Constant

          2) Variable

          3) Combination of constants and variables

( combined with operators, ref. to data type operators )

EXAMPLE :

PROGRAM EXAMPLE

IMPLICIT NONE

INTEGER :: AGE,PDS

REAL :: TOTAL, COST

CHARACTER (LEN=20) :: NAME

LOGICAL :: LOVED
AGE = 20 AGE = 20 + AGE PDS = 10 TOTAL = (PDS*30.)/3 COST = TOTAL*1.13955 NAME = 'LUCIE' LOVED = .TRUE.
STOP END PROGRAM EXAMPLE

On to the next lecture
Go back to lecture menu

Copyright © 1996 McGill University. All rights reserved.