7
Chapter 2 FORTRAN Introduction The first phase in programming a problem to the digital computer is to decide on the approach or method to be used in obtaining the solution. Since the digital computer is capable of performing only arithmetic operations, problems that cannot be solved by arithmetic procedures, when in their usual form, must be put in a form consistent with such procedures. However, the closed-form solution of a differential equation, for example, is not an arithmetic operation. An arithmetic procedure for solving such an equation must be developed. Once a method of solution has been selected, the problem must be programmed to the computer as a defined step-by-step procedure. This is called an algorithm 1 . The second phase of programming is to establish a general outline of the steps involved in the algorithm. One method is to write the steps in a numbered sequence. More frequently, however, the algorithm is stated graphically by means of a flow chart, in which the steps are displayed in the form of a block diagram. A flow chart may show the required steps in considerable detail, or it may merely outline the general procedure necessary to obtain a computer solution to the problem. In general, excessively detailed procedure should be avoided in the flow chart, since its primary purpose is merely to outline the overall process, which will be implemented by the more specific steps appearing in the finished computer program. The computer must be instructed very specifically for it to perform its oper- ations. Each step must be initiated by an instruction. The instructions, which the computer interprets and executes, must be transmitted to it in a coded form, which it can interpret. Thus the third phase of programming is to write the spe- cific program steps in coded form, following the outline provided by the flow chart. 1 An algorithm is a procedure that describes in an unambiguous manner a finite sequence of steps to be performed in a specified order. The object of the algorithm is to implement a numerical procedure to solve a problem or approximate a solution to the problem. 31

Nc 5555555555555555555555

Embed Size (px)

DESCRIPTION

Nc 77777777777777777777

Citation preview

Page 1: Nc 5555555555555555555555

Chapter 2

FORTRAN

Introduction

The first phase in programming a problem to the digital computer is to decide on theapproach or method to be used in obtaining the solution. Since the digital computeris capable of performing only arithmetic operations, problems that cannot be solvedby arithmetic procedures, when in their usual form, must be put in a form consistentwith such procedures. However, the closed-form solution of a differential equation,for example, is not an arithmetic operation. An arithmetic procedure for solvingsuch an equation must be developed.

Once a method of solution has been selected, the problem must be programmedto the computer as a defined step-by-step procedure. This is called an algorithm1.The second phase of programming is to establish a general outline of the stepsinvolved in the algorithm. One method is to write the steps in a numbered sequence.More frequently, however, the algorithm is stated graphically by means of a flowchart, in which the steps are displayed in the form of a block diagram. A flowchart may show the required steps in considerable detail, or it may merely outlinethe general procedure necessary to obtain a computer solution to the problem. Ingeneral, excessively detailed procedure should be avoided in the flow chart, since itsprimary purpose is merely to outline the overall process, which will be implementedby the more specific steps appearing in the finished computer program.

The computer must be instructed very specifically for it to perform its oper-ations. Each step must be initiated by an instruction. The instructions, whichthe computer interprets and executes, must be transmitted to it in a coded form,which it can interpret. Thus the third phase of programming is to write the spe-cific program steps in coded form, following the outline provided by the flow chart.

1An algorithm is a procedure that describes in an unambiguous manner a finite sequence ofsteps to be performed in a specified order. The object of the algorithm is to implement a numericalprocedure to solve a problem or approximate a solution to the problem.

31

Page 2: Nc 5555555555555555555555

This procedure is called coding, and the resulting program is called a machine-language program, but fortunately, it is no longer necessary to code the stepsmanually. The computer itself can be programmed to write the machine-languageprogram. The desired program is first written in one of the less-detailed and muchsimpler languages, normally called procedure-oriented languages, or, as they aresometimes called compiler languages, very similar to ordinary English and al-gebra. A program written in one of these languages must first be translated intoa machine language program by another machine language program. After it hasbeen translated the resulting machine language program (object program) is ex-ecuted. To accomplish this translation, the computer must first have a speciallywritten machine-language program read into its memory. Such a special programis called a translator, or processor, or a compiler. There are many languagesdesigned for different types of requirements. The FORTRAN (formula translatingsystem) processor, or compiler, is an example of such a special program, one of thefirst of the procedure -oriented languages was developed by IBM Corporation forits computers back in 50’s. Engineers and scientists in handling problems involvingconsiderable amounts of numerical computation designed it primarily for use.

2.1 GENERAL STRUCTURE OF COMPILER

LEVEL LANGUAGE

2.1.1 The Elements of FORTRAN

The sentences of FORTRAN are called statements. The elements used to formthese statements include constants, variables, subscripted variables, operation sym-bols, expressions, and mathematical functions.

2.1.2 Constants

A constant is a fixed, unchanged quantity. There are three principal types of con-stants: (1) numerical, (2) logical, and (3) literal. Numerical constants may befurther subdivided into integers constants, real constants, and complex constants.Both real and complex constants may be single precision or double precision.

2.1.2.1 Integer Constants

An integer constant is a number written without decimal point and can thus be onlya whole number. For example,

0 216 1 − 4321

32

Page 3: Nc 5555555555555555555555

2.1.2.2 Real Constants

Real constants are numbers written with a decimal point. There are two basic forms,ordinary decimal form such as -983.245, and exponent form, which consist of theuse of a real constant followed by an E and either a positive or negative power of10. For example,

1.57463E + 13 − 2.3567E − 11 32E + 4

2.1.2.3 Complex Constants

An ordered pair of real constants represents complex constants. The two real con-stants are separated by a comma and enclosed in parentheses. The first real constantis the real part of the complex constant, and the second is the imaginary part.For example,

(5.71,−2.86) represents 5.71 − 2.86i

Both parts should be of the same precision.

2.1.2.4 Logical Constants

Logical constants specify the logical values “ true” and “false”. When appearing ina sourse program, they are written as

.TRUE. and .FALSE.

2.1.3 Variables

A FORTRAN variable is a symbolic name for a quantity that is stored in computermemory. It consists of from one to six alphabetic and numeric (alphanumeric)characters, the first of which must be alphabetic. Real and complex variables maybe either single-precision or double precision.

2.1.3.1 Integer Variables

The first character of an integer variable name is I, J, K, L, M, or N. A differentcharacter than those listed may be used if the name appears in an INTEGER explicittype specification statement, or if the variable is specified as integer by an IMPLICITtype specification statement. If the variable is specified as integer by virtue of its firstcharacter being I, J, L, M, or N, its type is said to be specified by the predefinedspecification. For example, JOBNO is a valid integer variable name.

33

Page 4: Nc 5555555555555555555555

2.1.3.2 Real Variables

If a variable has its type specified by the predefined specification, its name will beginwith an alphabetic character other than I, J, K, L, M, or N. An example would beVELOC

2.1.4 Arrays

A FORTRAN array is a set of variables identified by a single name. The differentelements are differentiated from one another by using subscripts with the arrayname. Thus the array elements are subscripted variables. They may also be thesame modes (integer, real, and so forth) as nonsubscripted variables. The numberof elements in an array must be specified by giving the number of subscripts and themaximum value of each subscript in a DIMENSION, COMMON, or explicit typespecification statement. For example,

DIMENSION A(50), B(30,30)indicates that there is array named A having subscript of maximum value 50, andanother array named B with two subscripts, each having a maximum value of 30.A(10) or B(10,20) would be particular elements of the respected arrays.

2.1.5 Arithmetic Expressions

The simplest arithmetic expression is a single constant, variable, or function. Com-bining constants, variables, and functions with arithmetic operators forms morecomplicated expressions. These operators are

Operator Operation Hierarchy Level∗∗ Exponentiation 1st

∗ and / Multiplication 2nd

+ and - Addition and subtraction 3rd

The hierarchy determines which of two consecutive operations is performed first.

2.1.6 Logical Expressions

A logical expression always has the value “true” or “false” and are used in LogicalIF statements. The simplest form of logical expression is a single logical constant, asingle logical variable, or a single logical function. More complicated logical expres-sions state conditions, which are either true or false. One such logical expressionis called a relational expression, which simply makes a declaration regarding therelative magnitudes of two arithmetic expressions. These are: -

34

Page 5: Nc 5555555555555555555555

Relational Operator Definition

.GT. Greater than

.GE. Greater than or equal to

.EQ. Equal to

.NE. Not equal to

.LT. Less than

.LE. Less than or equal to

Relational operators may combine only single and double precision real and integerarithmetic expressions. Combining relational expressions, logical constants, logicalfunctions, and logical variables with the logical operators .AND., .OR., and .NOT.can form more complicated logical expressions. For example,

.NOT. Xis true if X is false, or false if X is true. The logical expression

X .AND. Yis true if and only if both X and Y are true. The logical expression

X .OR. Yis true if either X or Y or both are true,and false if both X and Y are false.

Still more complicated logical expressions involving several logical operators arepossible. Parentheses may be used to control the order in which operations areperformed in logical expressions. When parentheses are not used, a hierarchy deter-mines the order of performing operations which is as follows:

Operation HierarchyEvaluation of functions 1st

∗∗ 2nd

∗ and / 3rd

+ and - 4th

Application of relational operators.GT., .LT., etc 5th

Application of .NOT. 6th

Application of .AND. 7th

Application of .OR. 8th

2.1.7 FORTRAN supplied Mathematical Functions

Certain mathematical functions are automatically available to the FORTRAN pro-cessor. There are two kinds of these supplied function subprograms. The first are

35

Page 6: Nc 5555555555555555555555

called intrinsic, built-in, or in-line functions. These requires only a few machine-language instructions, so these instructions are placed in the object program eachtime the function name is used in the source functions. The second kind are calledexternal, library, or out-of-line functions. The preprogrammed procedures forthe functions are in a library, and only one set of machine-language instructionswould be placed in the object program, even though the function is used severaltimes.

Some commonly used out-of-line mathematical function subprograms are:

36

Page 7: Nc 5555555555555555555555

Function No. of Function Argument DefinitionName arguments Type Type (a is argument)EXP 1 Real(4) Real(4)DEXP 1 Real(8) Real(8) ea

CEXP 1 Complex(8) Complex(8)CDEXP 1 Complex(16) Complex(16)ALOG 1 Real(4) Real(4)DLOG 1 Real(8) Real(8) loge(a)CLOG 1 Complex(8) Complex(8)CDLOG 1 Complex(16) Complex(16)ALOG10 1 Real(4) Real(4) log10(a)DLOG10 1 Real(8) Real(8)ARSIN 1 Real(4) Real(4) arc sin(a)DARSIN 1 Real(8) Real(8) (in radians)ARCOS 1 Real(4) Real(4) arc cos(a)DARCOS 1 Real(8) Real(8) (in radians)ATAN 1 Real(4) Real(4) arc tan(a)ATAN2 2 Real(4) Real(4) arc tan(a1

a2)

DTAN 1 Real(8) Real(8) arc tan(a)DTAN2 2 Real(8) Real(8) arc tan(a1

a2)

SIN 1 Real(4) Real(4)DSIN 1 Real(8) Real(8) sin(a)CSIN 1 Complex(8) Complex(8) ( a is in radians)CDSIN 1 Complex(16) Complex(16)COS 1 Real(4) Real(4)DCOS 1 Real(8) Real(8) cos(a)CCOS 1 Complex(8) Complex(8) ( a is in radians)CDCOS 1 Complex(16) Complex(16)TAN 1 Real(4) Real(4) tan(a)DTAN 1 Real(8) Real(8) tan(a)COTAN 1 Real(4) Real(4) cot(a)DCOTAN 1 Real(8) Real(8) ( a is in radians)SQRT 1 Real(4) Real(4)DSQRT 1 Real(8) Real(8)

√a

CSQRT 1 Complex(8) Complex(8)CDSQRT 1 Complex(16) Complex(16)TANH 1 Real(4) Real(4) tanh(a)DTANh 1 Real(8) Real(8)SINH 1 Real(4) Real(4)DSINH 1 Real(8) Real(8) sinh(a)COSH 1 Real(4) Real(4)DCOSH 1 Real(8) Real(8) cosh(a)

37