CHAPTER 5

 

 ASSIGNMENT STATEMENTS

 

In Chapter 3, we learned that Fortran has six intrinsic data types: integer, real, double precision, complex, logical, and character. We used these data types to declare variable names in declaration statements. 

This chapter describes how to fill in values (constants) for each one of these variable names. Also described in this chapter are: (1) assignment statements, (2) arithmetic, logical, and character operations, and (3) a few commonly used intrinsic functions. 

Definitions 

  • A constant is a value that can be stored in the computer's memory and referred to by its variable name. 

  • An assignment statement defines the value of an arithmetic, logical, or character variable.

  • An intrinsic function is a subprogram that is supplied as part of the Fortran library. 

The arithmetic variable can be of integer, real, complex, or double precision type (see Chapter 3). Operations with arithmetic and logical variables are based on rules of rank of data types, and order of precedence of operators (Section 5.3). 

 
5.1  CONSTANTS 

A constant is a value that can be stored in the computer's memory. The value is stored in a specific location, the location is referred to by its address, and the address is referred to by its variable name. The programmer declares the variable name and assigns a value to it; the computer keeps track of the location and address. 

The relationship between value, location, address, and variable name is analogous to that of a street mailbox. A letter is stored in the mailbox at 123 Springfield Drive. The mail box is labeled Jones. The letter is the value, the mailbox is the location, 123 Springfield Drive is the address, and Jones is the (declared) variable name. The value may change (another letter may be placed in the mailbox), and the variable name may also change eventually (a change in occupant at the address). However, unlike a mailbox, the computer can only store one value per address. 

Integer Constants 

An integer constant is a whole number with no decimal point. It has the form:

si

where s is the sign (+ or -) and i is the integer number. For example, -135 is an integer constant.

 The following rules apply for integer constants: 

  • An integer constant can be negative, zero, or positive. 
  • The minus sign (-) is required preceding a negative integer constant. 
  • The plus sign (+) is optional in the case of a positive integer constant.
  • Typically, the range of an integer constant is from -2n-1 to 2n-1- 1, in which n is the number of bits per word. 
  • For a 4-byte integer constant, where 1 byte = 8 bits: n = 32. Therefore, the range (minimum negative to maximum positive value allowed) of a 4-byte integer constant is -2147483647 to 2147483646. 
  • Some processors allow a 2-byte integer constant. In this case: n= 16. Therefore, the range of a 2-byte integer constant is -32768 to 32767. 
The following are examples of valid integer constants:

0

92

-345

The following are examples of invalid integer constants:

3.1416

31,860    

9753845210

Periods or commas are not allowed, and the last example (9753845210) is out-of-range (it is too large, even for a 4-byte integer constant). 

In unformatted input, integer constants should have no decimal point. If the decimal point is present, with or without a fractional part, the decimal point is ignored and the fractional part, if any, is disregarded. Unformatted integer output appears with no decimal point. 

 
Real Constants 

A real constant is a number that is written with a decimal point and may have a fractional part following the decimal point. A real constant has one of the following forms:

s.r

sr.r

sr.

where s is the sign (+ or -) and r is a string of one or more decimal digits; for example, -132.764 is a real constant. The decimal point precedes or follows a string r, or is embedded between two r's.

As with an integer constant, a real constant can be negative, zero, or positive. Although the plus sign (+) is optional, the minus sign (-) is required preceding a negative real constant.

Real constants may also be written in exponential notation. Exponential notation uses the letter E, followed by a sign (+ or -) and an integer exponent, to represent "... times 10 to the power ..." A real constant can contain only the numerals 0 through 9, algebraic signs, a decimal point, and the letter E. In exponential notation, a real constant can take any of the following forms:

s.rEsk

sr.rEsk

sr.Esk

iEsk

where k is the integer exponent and all other symbols have been previously defined. An example is:

-132.764E-2

The following rules apply for exponential notation: 

  • A plus sign (+) is optional between the letter E and a positive integer exponent. 
  • A minus sign (-) must appear between the letter E and a negative integer exponent.
  • The exponent k, which has at most two digits (0 through 9), is required even if it happens to be zero.
  • There is no limit to the number of digits of the string r, but only the leftmost m digits (the value of m is processor dependent) are significant. This is referred to as "a precision of m digits". For a 4-byte real wordsize, m= 7. 
  • Leading zeros are ignored in counting the leftmost m significant digits. 
  • The range of a real constant (i.e., maximum to minimum values allowed) is not unlimited, but rather, is processor dependent.

 The following are examples of valid real constants: 

3.14159 

-0.56 

-3.56E-3 

-123.456E0 

56E4 

The following are examples of invalid real constants: 

3,1416 

$33560 

123._67 

Commas or special characters such as $ or _ are not allowed as part of real constants.

In unformatted input, a real constant should have a decimal point and, optionally, a fractional part. If a real constant has no decimal point and therefore, no fractional part, a decimal point is added to the right of the constant and the fractional part is taken to be a string of zeros. Unformatted real output has a decimal point and a fractional part, which may be a string of zeros.

 
Double-Precision Constants

A double-precision constant is written in exponential notation by using the letter D instead of E. A double-precision constant has one of the following forms:

s.rDsk

sr.rDsk

sr.Dsk

iDsk

where D denotes "... times 10 to the power ..." and all other symbols have been previously defined. For example:

-13257.3619D-3

As with real exponential notation, plus signs are optional and minus signs are required. The exponent k is required even if it happens to be zero. There are two types of double-precision constants: D-type and G-type. In D-type, the exponent k can have at most two digits. In G-type, the exponent k can have at most three digits. The type of double-precision constant is determined during compilation time in a processor-dependent manner.

    There is no limit to the number of digits of the string r, but only the leftmost n digits (the value of n is processor dependent) are significant. For an 8-byte double-precision wordsize, n= 16 and k= 2 for a D-type constant; and n= 15 and k= 3 for a G-type. It can be seen that a G-type constant has a slightly lesser accuracy than D-type, but it allows for a larger integer exponent. 

  The following are examples of valid double-precision constants: 

-.123D-3

0.31D25

-35071.D+3 

56D299

The last example (56D299) is valid only as G-type.

In unformatted input, a double-precision constant should be specified using an exponential notation with the letter D. Unformatted double-precision output is written in exponential notation using the letter E. 

 
Complex Constants 

In Fortran, a complex constant represents the complex number a + bi.

A complex constant consists of a pair of real or integer constants, separated by a comma and enclosed in parentheses. The first constant represents the real part of the complex number (a) and the second represents the imaginary part (b). It takes the form: 

(a,b)

where a and b are real or integer constants, as shown in the following examples:

 (2,3) 

(3.5,-5.6)

 (123.45E3,5.43E-1) 

In unformatted input, a complex constant should be enclosed in parentheses, with its real and imaginary parts separated by a comma. Unformatted complex output appears in the same way.

 
Logical Constants 

A logical constant defines a logical value as either true or false. It takes one of the following forms:

 .TRUE.

 .FALSE. 

The surrounding (delimiting) periods are required.

In unformatted input, a logical constant can be read as either .TRUE. or .FALSE., or any other character string beginning with the letters T (true) or F (false). Either of the surrounding periods is optional. Unformatted logical output appears as a single T (true) or a single F (false). 

 
Character Constants 

A character constant is a string of valid characters en closed within apostrophes. It takes the following form:

  'abc&xyz' 

The character string abc&xyz enclosed within apostrophes stands for any string of characters of the Fortran character set (Section 1.1). The value of the character constant is the string of characters contained between the required apostrophes. This value does not include the apostrophes, but it does include all blank spaces and tabs, if any, within them. 

The length of a character constant must be in the range of 1 to 2000 characters, including blank spaces. Within a character constant, the apostrophe character is represented by two consecutive apostrophes, with no blank spaces between them, as shown below:

 'MCDONALD''S HAMBURGERS' 

This character constant would be stored and printed as follows: 

MCDONALD'S HAMBURGERS

In unformatted input, a character constant should be surrounded by apostrophes. In unformatted output, a character constant appears as a character string without apostrophes. 

 
5.2 ASSIGNMENT STATEMENTS

 The three types of assignment statements are: 

  1. Arithmetic assignment statements 
  2. Logical assignment statements 
  3. Character assignment statements 
 
Arithmetic Assignment Statement 

The arithmetic assignment statement is perhaps the most common Fortran statement. It defines the value of a variable, be it integer, real, double precision, or complex. It has an equal (=) symbol preceded by a variable name and followed by a constant or arithmetic expression whose value is determined at execution time.

The arithmetic assignment statement takes the form: 

v= e 

where v is a variable name and e is a constant or arithmetic expression. 

The symbol = does not mean "equal to" as in conventional mathematics. Rather, it means "is replaced by". For example, in the following statement:

  KCOUNT= KCOUNT + 1

the integer KCOUNT is incremented by one at execution time. The old value is replaced by the new value; the old value is lost and the new value remains in storage.

The following statement defines the value of Z: 

Z= X + Y

The values of X and Y should be defined prior to the execution of this statement. It is important to realize that some processors may assign initial values of 0. to all real variables. Then, the absence of a definition of X or Y will not stop execution. In the above case, if neither X nor Y were defined prior to this statement, the processor would add X = 0. to Y = 0. to give Z = 0. 

In the assigment statement v = e: 

  • If e is of the same data type as v (say, there are both real), the assignment is direct, requiring no conversion. 

  • If e is of a different type as v, the data type of e is converted to that of v prior to the completion of the assignment. 

   In certain cases, the conversion may result in the loss of a portion of the data, as shown in the following statements: 

X= 3.5 

J= X + 1 

in which X is real and J is integer. Upon execution of these two statements, the value of X + 1 is 4.5; however, only its integer portion can be stored in J. Therefore, J= 4.

Examples of valid arithmetic assignment statements are:

 I= J/K*M 

PI= 3.1416

B= 1.3D-2**3.5 

ALPHA= (3.2*X*Y)/(2.5*Z) 

ZT= Z1 + (Z2*Z3) - (Z4/Z5) 

COM= (12.5,34.7) + (9.6,-3.5) 

Examples of invalid arithmetic assignment statements:

25.= A 

ALPHA= (3.2*X*Y)/(2.5*Z 

PI= 3.1416.

ZT= Z1 + Z2*/Z3 

Regarding these examples:

  •  The first statement is reversed.
  • The second statement contains an unmatched parenthesis.
  • The third statement has an extraneous period at the end. 
  • The fourth statement contains an invalid sequence of arithmetic operators (*/).
 
Logical Assignment Statement 

The logical assignment statement defines the value of a logical variable. It consists of an equal (=) symbol preceded by a logical variable name and followed by a logical constant (or logical expression) whose value is determined at execution time. 

The logical assignment statement takes the form:

 v = e 

where v is a logical variable name and e is a logical constant (or logical expression). 

The following are examples of valid logical assignment statements: 

A= .TRUE. 

BL= CL.AND.DL

P= X.LT.3.5.AND..NOT.Q 

BIGV= V.GT.X.AND.V.GT.Y.AND.V.GT.Z 

Examples of invalid logical assignment statements:

A= TRUE 

BL= CL.AND..DL

P= X.LT.3.5.NOT..AND.Q 

BIGV= V.GT.X.AND.V.GT.Y.AND.GT.Z

In these examples:

  • The first statement is missing the surrounding periods. 
  • The second statement has an extra period before DL. 
  • In the third statement, the operator .NOT. should not precede the operator .AND. as shown; rather, it should follow it.
  • In the fourth statement, the third relational operator is missing a variable name (V).

 
Character Assignment Statement 

The character assignment statement defines the value of a character variable. It consists of an equal (=) symbol preceded by a character variable name and followed by a character constant (or character expression) whose value is determined at execution time.

     The character assignment statement takes the form: 

v = e 

where v is a character variable name and e is a character constant (or character expression). 

 The following rules apply:

  • If the length of e is greater than the length of v, the character expression e is cut off (the extra characters to the right are lost). 

  • If the length of e is less than the length of v, the character expression e is filled to the right with blank spaces.

For example: 

CHARACTER*15 PROJECT,DATE 

PROJECT= 'SOLAR DEVELOPMENT NO. 2'

DATE= 'MAY 10, 1994' 

The first statement declares PROJECT and DATE as character variables, each of maximum length equal to 15 spaces. The second statement assigns the character constant SOLAR DEVELOPMENT NO. 2 to PROJECT. The third statement assigns the character constant MAY 10, 1994 to DATE. Upon execution, the variable PROJECT is completely filled with the first 15 characters of SOLAR DEVELOPMENT NO. 2 The remaining characters are lost. The value of PROJECT is then 

SOLAR DEVELOPME 

Likewise, the variable DATE is partially filled, starting from the left, with all the characters of MAY 10, 1994. The remaining three spaces to the right are filled with blank spaces denoted by b. The value of CHAR2 is then 

MAY 10, 1994bbb 

Character constants are not initialized at execution time, remaining undefined until an assignment is made by a DATA or character assignment statement, or read from an input file. 

Following are valid character assignment statements: 

FRUIT= 'ORANGE' 

LABEL= 'THE VALUES ARE: ' 

ERROR_35= 'PLEASE EXAMINE VALUE OF Y' 

Following are invalid character assignment statements:

FRUIT= 'APPLE 

NAME= 'MCDONALD'S' 

ERROR_45= IMPROPER VALUE OF Y

Regarding these examples, note: 

  • The first statement is missing the end apostrophe.
  • The second statement has three apostrophes, instead of a pair.
  • The third statement is missing the surrounding apostrophes altogether.
Things to remember in regard to assignment statements:
  • Always place the new variable to the left of the equal sign. 
  • Always place arithmetic, logical, and character operators to the right of the equal sign. 
  • If you place integer variables to the left of the equal sign and real values to the right, replacement with convert the real values into integer values, with loss of the fractional part. 

 
5.3 OPERATIONS 

Arithmetic Operations

Table 5.1 lists arithmetic operators. Table 5.2 shows examples of the order of precedence of arithmetic operations. 

Within one arithmetic expression, execution follows a preestablished order of precedence: 

  1. First (highest): exponentiation (**); starting with the rightmost exponentiation and proceeding from right to left. 
  2. Second (intermediate): multiplication (*) and division (/); starting with the leftmost multiplication or division, and proceeding from left to right. 
  3. Third (lowest): summation (+) and subtraction (-), starting with the leftmost summation or subtraction, and proceeding from left to right.
 
TABLE 5.1 ARITHMETIC OPERATORS 

Precedence Operator Function Direction of Evaluation
1. Highest ** Exponentiation Right to Left

2. Intermediate * Multiplication Left to Right

/ Division Left to Right

3. Lowest + Addition Left to Right

- Subtraction Left to Right


TABLE 5.2 ORDER OF PRECEDENCE OF OPERATIONS

Fortran expression Algebraic equivalent Numeric example

A + B -C a + b - c 1 + 2 - 3 = 0

A + B*C a + bc 1 + 2 * 3 = 7

(A + B)*C (a + b) c (1 + 2)*3 = 9

A*B**C a bc 1*2**3 = 8

(A*B)**D (a b)d (1*2)**4 = 16

A + B/D - C a + (b / d) - c 1 + 2/4 - 3 = -1.5

(A + B)/(D - C) (a + b) / (d - c) (1 + 2)/(4 - 3) = 3

A**B**C a(bc) 1**2**3 = 8

(A**B)**C (ab)c (1**2)**3 = 1

A*B + C/D**E ab + c / (d e) 1*2 + 3/4**5 =2.003

(A*B) + (C/D**(E)) ab + c / (d e) 1*2 +3/4**(5) =2.003

(A*B) + ((C/D)**E) ab + (c / d)e 1*2 +(3/4)**5 = 2.24

You may use parentheses to force an order of evaluation different from the preestablished order of precedence. Expressions within parentheses are evaluated first, working outwards to the expression within the outermost set. The expression contained within each set of parentheses is subject to the preestablished order of precedence. Nonessential parentheses do not affect the evaluation of the expression.


Rank of Arithmetic Data Types

Arithmetic data types rank in this order, from highest to lowest:

  1. COMPLEX
  2. DOUBLE PRECISION
  3. REAL
  4. INTEGER
  5. LOGICAL

When evaluating an arithmetic expression that contains two elements of different data type, the result is a variable of data type equal to that of the element of highest rank. For example, if two variables, one real and one integer, appear in one arithmetic expression, the result of the operation is of real type, as in

A = 1.6

I = 2

C = A * I

In this case, C is equal to 3.2.

When an operation involves two integers, the result is also an integer. Therefore, any fraction resulting from the division of two integers is automatically truncated (lost). For example, in the following sequence of statements:

I= 7

J= 2

K= I/J

the value of K is stored as 3, not 3.5. Disregarding the possibility of this truncation is a common source of error in programming.

In the following sequence of statements:

I= 5

J= 2

X= 4.

Y= I/J*X

the expression for Y is evaluated from left to right. First, I/J= 2; second, Y is equal to 2 times 4, resulting in Y= 8.

Parentheses may be used to override the established order of precedence. For instance:

Y= I/(J*X)

This results in first multiplying J times X, to yield 8., and then Y= 5/8. = 0.625. Note that two similar expressions (one with and another without parentheses) yield completely different answers.


Logical Operations

Table 5.3 lists the preestablished order of precedence of combined relational and logical expressions. Practical examples are shown in the box immediately following the table. As stated previously, when operators are of equal precedence, they are evaluated from left to right, except for exponentiation, which is evaluated from right to left. As with arithmetic operations, parentheses can be used to force an order of evaluation different from the preestablished order of precedence.

TABLE 5.3 ORDER OF PRECEDENCE OF COMBINED


RELATIONAL AND LOGICAL OPERATIONS

Operator Order of Precedence

** First (highest)

* , / Second

+ , - Third

Relational Operators Fourth

.NOT. Fifth

.AND. Sixth

.OR. Seventh

.EQV. and .NEQV. Eighth (lowest)

Examples: Combined Relational and Logical Operations

Example 1

A+B.LT.5..AND.C

First, real variables A and B are summed; then the relational expression A+B.LT.5. is tested to see if it is true. Then, the conjuntion of A+B.LT.5. and logical C is tested to see if it is true. If both A+B.LT.5. and C are true, the combined logical expression is true.

Example 2

A.LT.3..AND.B.LT.5..OR..NOT.C

First, real A is tested to see if it is less than 3. Then real B is tested to see if it is less than 5. Then .NOT.C is evaluated; it is true if logical C is false; it is false if C is true. Then the conjunction of the two relational expressions A.LT.3. and B.LT.5. is evaluated; if both are true, the conjunction is true; otherwise, the conjunction is false. Finally, the disjunction of the expressions A.LT.3..AND.B.LT.5. or .NOT.C is evaluated; if either is true, the combined logical expression is true.

Character Linking

Character strings can be linked together (i.e., concatenated) using the double slash operator (//), as shown in the following example:

ALPHA='ABC'//'DEFGHI'//'JKLMNOPQRS'//'TUVWYZ'

The character variable ALPHA is stored in memory as:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Embedded blank spaces, if any, are incorporated as part of the character string.


5.4 INTRINSIC FUNCTIONS

The Fortran library contains a set of intrinsic (built-in) functions. These functions perform common mathematical computations such as the square root, the logarithm, or a trig function.

An example of the intrinsic function that calculates an absolute value is shown in the following sequence of statements:

X= -3.2

Y= 10.5

Z= ABS(X) + Y

In this example, the value of Z is evaluated as 13.7, i.e., the absolute value of X (3.2) plus the value of Y (10.5).

A few commonly used intrinsic functions are described in Table 5.4.


TABLE 5.4 COMMONLY USED INTRINSIC FUNCTIONS

Function Meaning Argument Example
ABS(X) | x | Integer variable ABS(J)

Real variable ABS(A)

Integer expression ABS(J+K)

Real expression ABS(A+J)

ALOG(X)1 ln (x) Real constant ALOG(3.5)

Real variable ALOG(A)

Real expression ALOG(A+3.5)

1 Note: The value of the argument must be greater than zero.

ALOG10(X)2 log10 (x) Real constant ALOG10(3.5)

Real variable ALOG10(A)

Real expression ALOG10(A+3.5)

2 Note: The value of the argument must be greater than zero.

EXP(X) e x Real constant EXP(3.5)

Real variable EXP(A)

Real expression EXP(A+J)

SIN(X)3 sin(x) Real constant SIN(1.7)

Real variable SIN(A)

Real expression SIN(A+1.7)

3 Note: For all trig functions, the value of the argument must be given in radians.

COS(X) cos(x) Real constant COS(1.7)

Real variable COS(A)

Real expression COS(A+1.7)

TAN(X) tan(x) Real constant TAN(5.5)

Real variable TAN(A)

Real expression TAN(A+5.5)

ASIN(X)4 sin-1 (x) Real constant ASIN(0.707)

Real variable ASIN(X3)

Real expression ASIN(X3+0.707)

4 Note: All inverse trig functions return a value in radians. The absolute value of the argument (x) must be less than or equal to 1.

ACOS(X)5 cos-1 (x) Real constant ACOS(0.866)

Real variable ACOS(Y2)

Real expression ACOS(Y2+0.866)

5 Note: The absolute value of the argument (x) must be less than or equal to 1.

ATAN(X) tan-1 (x) Real constant ATAN(15.5)

Real variable ATAN(B)

Real expression ATAN(B+15.5)

ATAN2(Y,X) tan-1 (y/x) Real constant ATAN2(5.,2.3)

Real variable ATAN2(Y1,X1)

Real expression ATAN2(2*Y,X2)

FLOAT(J) returns
real
equivalent
Integer constant FLOAT(8)

Integer variable FLOAT(K)

Integer expression FLOAT(K/8)

INT(X)6 returns
integer
equivalent
Real constant INT(3.5)

Real variable INT(A)

Real expression INT(A/3.5)

6 Note: The fractional part will be truncated.This function is the same as the intrinsic function IFIX(X).

NINT(X)7 returns
nearest
integer
equivalent
Real constant NINT(5.6)

Real variable NINT(A)

Real expression NINT(A+5.6)

7 Note: The first example will return an integer value of 6. Likewise, NINT(-2.3) will return a value of -2

SQRT(X)8 x0.5 Real constant SQRT(3.5)

Real variable SQRT(A)

Real expression SQRT(A+3.5)

8 Note: The value of the argument must be greater than or equal to zero.

MOD(M,N) returns
integer
remainder
of M/N
2 integer variables MOD(K,5)

Example: K = MOD(M,N) will return a value K = M - N(M/N).
                   If M = 7, and N = 3, the value of M/N = 7/3 = 2; and the value of K = 7 - 3(2) = 1.

The MOD function is used when action needs to be taken at regular intervals, as shown below.

      K= 0

   10 K= K + 1

      IF (MOD(K,5).EQ.0) THEN

      WRITE(6,*) 'FIFTH K VALUE'

      END IF

      IF (K.LE.100) GO TO 10

      CONTINUE

This loop executes 100 times. The message FIFTH K VALUE will be printed when the counter K is a multiple of 5 (i.e., every fifth passage through the loop).


5.5 EXAMPLE PROGRAMS

Example Program 1: Assignment Statements

C234567890

      PROGRAM ASSIGNMENT
      CHARACTER*26 LAB*6,LABEL
      COMPLEX C1,C2,C3
      DOUBLE PRECISION D1,D2,D3
      LOGICAL L1,L2,L3
C-----ASSIGNMENT USING DATA STATEMENT
      DATA A,I,C1,D1,L1 /9.45,35,(2.,4.5),
     *1.05D-1,.TRUE./
C-----ASSIGNMENT WITH ASSIGNMENT STATEMENTS
      D2= 1234567890123456D0
      LAB= 'X_X_X_ '
      L2= .FALSE.
      J= I/2
      B= A + I/J
      C2= (3.,5.5)
C-----CALCULATIONS
      C3= C1 + C2
      D3= D1*D2*J
      L3= L1.AND..NOT.L2
      LABEL= 'ABCDEF'//'GHIJKLMNOPQRSTUVWXYZ'
C-----WRITE STATEMENTS
      WRITE(6,*) J,B,C3,D3,L3
      WRITE(6,*) LAB,LABEL
      END

This program defines a series of variables of different data types, including integer (I,J), real (A,B), double precision (D1, D2, D3), complex (C1, C2, C3), logical (L1, L2), and character (LAB, LABEL).

The program proceeds to do a series of valid operations on these variables, and writes the output directly to the screen. The first line of output from this program (unformatted output) is:

17 11.45000 (5.000000,10.00000) 2203703683870369. T

The second line is:

X_X_X_ ABCDEFGHIJKLMNOPQRSTUVWXYZ

Example Program 2: Tic-Tac-Toe Board

C234567890


      PROGRAM TIC_TAC_TOE
      CHARACTER*12 C1,C2,C3*17
      DATA K /0/
      DATA C1 /'     X     X'/
      DATA C2 /'     O     O'/
      DATA C3 /'XOXOXOXOXOXOXOXOX'/
   10 K= K + 1
      WRITE(6,*) C1
      WRITE(6,*) C2
      WRITE(6,*) C1
      IF(K.LT.3) THEN
      WRITE(6,*) C3
      GO TO 10
      END IF
      END

The output from this program is:

     X     X
     O     O
     X     X
X0X0X0XOXOXOXOXOX
     X     X
     O     O
     X     X
X0X0X0XOXOXOXOXOX
     X     X
     O     O
     X     X

Note the following features of this program:

  • Counter K is initialized with the value zero (0).
  • Initialization of character variables C1 and C2 includes embedded blank spaces.
  • Block IF construct and GO TO statement are used to conditionally transfer control to statement 10.
  • Implicit STOP is executed when control reaches END statement.


5.6 SUMMARY

This chapter describes constants, assignment statements, operations on arithmetic, logical and character variables, and a few commonly used intrinsic functions.

  • A constant is a value that can be stored in the computer's memory and referred to (referenced, accessed) by its variable name. Example: 3.5, in SUM = 3.5

  • An assignment statement defines via an equal sign (=) the value of an arithmetic, logical, or character variable. Examples:

    SUM = SUM + 5.3

    LVALUE = .TRUE.

    NAME= 'JONES'

  • An intrinsic function is a math subprogram that is supplied as part of the Fortran library. Example: Y = TAN(0.7)


CHAPTER 5 -- PROBLEMS

  1. Write a program to calculate SIN(X) by using the first 10 terms of the infinite product sin(x)/x = cos(x/2) cos(x/4) cos(x/8) cos(x/16)..., with x given in radians. Test your program with x= 30o.

  2. Write a program to print a banner with your initials, as shown below. (11 rows by 11 columns; letters separated among them by 6 blank spaces).

            J      DDDDDDDDD
            J      D        D
            J      D         D
            J      D         D
            J      D         D
            J      D         D
            J      D         D
            J      D         D
    J       J      D         D
    J       J      D        D
     JJJJJJJ       DDDDDDDDD

  3. Write an interactive program that reads an integer n in the range 0 to 100, calculates its factorial n!, and writes the result to the screen. Use the following output form: THE FACTORIAL of _____ is __________. If the entered number is out of range, write the following message to the screen: ENTERED NUMBER IS OUT OF RANGE. Use the program to calculate the factorial of 0, 33, 67, and 100. [Hint: Use a double-precision variable capable of storing a very large number, such as 100!].

  4. Write an interactive program to calculate the area A and perimeter P of a trapezoid of altitude h, parallel sides a and b, with angles θ and φ between the longer parallel side b and the nonparallel sides. The respective formulas are: A = (1/2) h (a+b); P = a + b + h(csc θ + csc φ). The input data (h, a, and b) can be entered in either SI units (centimeters) or U.S. customary (inches), with the angles θ and φ in degrees. [Hint: Use the following sequence at the start of the program]:

          WRITE(6,'(1X,A,$)')'ARE YOU USING SI UNITS? (Y/N): '
          READ(5,'(A)') ANSWER

  5. Write a program to calculate all six hyperbolic trigonometric functions. Test your program with θ= 30o.

  6. Write an interactive program to calculate the natural log of x using the first ten terms of the following infinite series: ln x = 2{[(x-1)/(x+1)] + (1/3)[(x-1)/(x+1)]3 + (1/5)[(x-1)/(x+1)]5 + ... }. Do not write out the 10 terms; rather, accumulate the sum in one storage location. Test your program with x= 1.5.

  7. Write an interactive program to calculate tan-1x by using the first 10 terms of the following infinite series: tan-1x = x - (x3/3) + (x5/5) - (x7/7) + ... , for |x| < 1. Do not write out the 10 terms; rather, accumulate the sum in one storage location. If |x| is greater than or equal to 1, write a message such as: ENTERED VALUE IS OUT OF RANGE. PLEASE TRY AGAIN, and prompt for a new value of x. Test your program with x= 1.5, and x= 0.5.

  8. Write an interactive program to calculate sin-1x by using the first 8 terms of the following infinite series: sin-1x = x + (1/2)(x3/3) + (1/2)(3/4)(x5/5) + (1/2)(3/4)(5/6)(x7/7) + ... , for |x| < 1. Do not write out the 8 terms; rather, accumulate the sum in one storage location. If |x| is greater than or equal to 1, write a message such as: ENTERED VALUE IS OUT OF RANGE. PLEASE TRY AGAIN, and prompt for a new value of x. Test your program with x= 1.5, and x= 0.5.

  9. Write a program to calculate and print to a file TEMPERATURE.OUT a temperature conversion table, from Farenheit to Celsius, for 32 ≤ oF ≤ 212, in intervals of 1oF. Use appropriate table headings, such as:        TEMPERATURE (oF)        TEMPERATURE (oC)

  10. Write a program to calculate the first positive root of a cubic equation of the form: y = ax3 + bx2 + cx + d. Hint: Evaluate y(x) for x= 0, 1., 2., and so on, in increments of 1., until y changes sign (goes from positive to negative, or viceversa). Then, decrease the increment to one-tenth of its value, return to the previous value of x, and continue to repeatedly evaluate the function y, until it changes sign again, decreasing the increment again, and repeating the process. Stop the calculation after 7 changes in sign (i.e., after 7 significant figures). Write the value of the first positive root x, preceded by an appropriate label. (THE FIRST POSITIVE ROOT OF THE CUBIC EQUATION IS = ). Test your program with the following data: a= 20., b= -35., c=15., d= -5.

  11. Repeat Problem 10 counting the number of trials that it took to get the answer, and reporting the total number of trials at the end.

  12. Repeat Problem 10 using the Newton approximation to the root. After the first sign change, return to the previous value of x (i.e., xo), and evaluate the new trial value of the root using Newton's approximation: xr= xo - [y(xo) / y'(xo)]. Repeat the process until the change in the value of the root is less than 1 × 10-6. Test your program using the same input data of Problem 10.

  13. Repeat Problem 12 counting the total number of trials that it took to get the answer, and reporting the total number of trials at the end.

  14. Write a program to print the following banner resembling the U.S. flag:

    * * * * * * RRRRRRRRRRRRRRRRRRRRRRRR
     * * * * *  WWWWWWWWWWWWWWWWWWWWWWWW
    * * * * * * RRRRRRRRRRRRRRRRRRRRRRRR
     * * * * *  WWWWWWWWWWWWWWWWWWWWWWWW
    * * * * * * RRRRRRRRRRRRRRRRRRRRRRRR
     * * * * *  WWWWWWWWWWWWWWWWWWWWWWWW
    * * * * * * RRRRRRRRRRRRRRRRRRRRRRRR
     * * * * *  WWWWWWWWWWWWWWWWWWWWWWWW
    * * * * * * RRRRRRRRRRRRRRRRRRRRRRRR
    WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
    RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
    WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
    RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR


081218