chapter 2:basic elements of fortran programming

Upload: abdu-al-hasani

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    1/47

    1

    Chapter 2

    Basic Elements of

    Fortran Programming

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    2/47

    2

    Character set (Table 2-1)

    26: UPPER CASE A Z

    26: lower case a z

    10: Digits 0 9

    1: Underscore _ 5: Arithmetic symbols + - * / **

    17: Other symbols ( ) . = , $ :

    ! % & ; < > ?

    and blank

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    3/47

    3

    Case insensitive

    Example: Apple

    apple

    ApPLe ApplE

    Example:

    read (*,*) Name

    write (*,*) name

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    4/47

    4

    Fortran Statements

    Executable statements: Actions taken by program

    Examples: Read (*,*) x, y Z = x + y Write (*,*) The result = , z

    Nonexecutable statements information for proper operation of program

    Examples:

    Program name ! This is a comment End program

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    5/47

    5

    Fortran Statements

    Each line is 132 characters long

    If it does not fit, use & to split a statement Example:

    Output = input1 + input2

    Output = input1 &

    + input2

    Output = input1 &

    & + input2

    A statement can be split up to 40 lines

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    6/47

    6

    Fortran Statements

    Statements can be named using a label

    Example: program counter 10 integer :: count = 5 20 write (*,*) count = , count

    end program A label should be unique It does not indicate line numbers It can be used more than once

    It does not indicate the program sequence/order Not used in modern Fortran 90/95

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    7/47

    7

    Fortran Statements

    Comments:

    Ignored by Fortran compiler

    can appear any where in a line

    start with ! to the end of the line Examples:

    ! This is a counting program

    a = b + 1 ! This statement adds one ! Can I put a comment here? a = b + 1

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    8/47

    8

    Fortran Program Structure

    Declaration section Programs name

    Types of variables and constants

    Execution section Actions to be performed by program

    Termination section

    Stopping (ending) program execution

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    9/47

    Fortran Program Structure

    Program name

    Declaration statements

    .Executable statements

    ..

    END Program name

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    10/47

    10

    Fortran Program Structure

    Example:

    program first_program

    ! This is my first program

    integer, parameter :: x = 3, y=4 integer :: z

    z = x + y

    write (*,*) x + y = , z end program

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    11/47

    Options for Program Termination

    end program name end program

    end

    stop ! to be used in special cases

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    12/47

    12

    Rules of NAMES Any name (program/variable/constant) can be used only once

    program counter integer :: counter = 5 write (*,*) counter = , counter end program

    Names

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    13/47

    13

    Program styles

    A programmer should use a consistent style: Example 1:

    PROGRAM example1

    REAL :: x, y, z

    WRITE (*,*) Enter x, y WRITE (*,*)

    READ (*,*) x, y, z

    z = x + y

    WRITE (*,*) x + y = , z

    END PROGRAM

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    14/47

    14

    Program styles

    Another programmer can use a different style: Example 2:

    program example1

    real :: x, y, z

    write (*,*) Enter x, y write (*,*)

    read (*,*) x, y, z

    z = x + y

    write (*,*) x + y = , z

    end program

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    15/47

    15

    Program styles

    This style is not acceptable (but it works!): Example 3:

    program example1

    real :: x, y, z

    WRITE (*,*) Enter x, y write (*,*)

    READ (*,*) x, y, z

    z = x + y

    write (*,*) x + y = , z

    end PROGRAM

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    16/47

    16

    Variable vs. Constant

    Constant: Once declared, cannot be changed during execution If you try to change it, you get an error Example:

    REAL, PARAMETER :: GRADE = 88

    GRADE = GRADE / 100 Variable:

    Can change value during execution Example

    REAL :: GRADE = 88 GRADE = GRADE / 100

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    17/47

    17

    Data dictionary

    In the header of the program

    Example: program converter

    ! This program converts US Dollars to Omani Rials.

    ! We use the variables:

    ! USD: US Dollars

    ! OR: Omani Rials

    end program

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    18/47

    18

    More about data types

    3 Numeric: INTEGER REAL

    COMPLEX (not covered)

    1 Strings of Characters: CHARACTER

    1 Logical: LOGICAL

    Others: Chapter 12: derived data types (not covered in this course)

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    19/47

    19

    More about data types

    INTEGER:

    Either constant or variable

    +ve, -ve, zero

    1,000,000 (error: commas not allowed)

    -100.

    (error: decimal point not allowed)

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    20/47

    20

    More about data types

    REAL: Constants must have a decimal points ( 300.) 10,000,000.

    (error: commas not allowed)

    105 (error: decimal point missing)

    123E5

    (error: decimal point missing in mantissa)

    -34E2.5 (error: decimal point not allowed in exponents)

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    21/47

    21

    More about data types CHARACTER:

    Example1 Character :: name name = Ramadhan Write (*,*) name

    Example2

    Character (len=8) :: name name = Ramadhan Write (*,*) name

    Example3 Character (len=14) :: word1

    Character (len=6) :: word2 word1 = Ramadhan Word2 = kareem Write (*,*) word1, word2

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    22/47

    22

    More about data types

    CHARACTER:

    Using single/double quotes Example1:

    Name = Abdullah Name = Abdullah Name = Abdullah Name = Abdullah Write (*,*) I read quran daily Write (*,*) I read quran daily Write (*,*) I read quran daily

    Each one surrounds the other: Solar energy is a clean source of energy Hes wasting time watching useless TV programs

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    23/47

    23

    Implicit none

    It checks that variables types are declared Without it:

    Any undeclared variable starting with I, J, K, L, M, N areintegers (default typing)

    Other variables are real (default typing)

    Examples: Program checking read (*,*) monthly_income annual_income = monthly_income * 12 write (*,*) Annual income = , annual_income End program

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    24/47

    24

    Initializing Variables

    Three ways to initialize

    Initialize at declaration section

    Using assignment statement at execution section

    Using READ to initialize from input device Non-initialized variables might or might not produce an error. Program might

    work in some machines and fail in others or at the same machine might worksome times and fail other times depending on the values stored at thememory location.

    Rule: All variables must be initialized before using them.

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    25/47

    25

    Input/output statments READ (*,*)

    Standard input device (keyboard) Free input format (decided by variable type) e.g: (try inputting more values for each statement)

    READ i READ i, j READ i, j, x, char

    (Note: character with specific length will be left justified with all others filled with blankif not entered)

    WRITE(*,*) Standard output device (screen) Free output format E.g:

    WRITE(*,*) x WRITE(*,*) Result is: , x WRITE(*,*) Result is: , COS(x) WRITE(*,*) Result is: , x, And cosine will be: , cos(x)

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    26/47

    26

    Arithmetic operators Assignment:

    Variable_name = expression Example:

    Days = months * 30

    = is called assignment operator

    Binary arithmetic operators (e.g. a + b): + Addition - Subtraction * Multiplication / Division

    ** Exponentiation Unary arithmetic operators (e.g. b)

    + 34 - a

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    27/47

    27

    Rules No two operators may occur side by side

    A * - b A ** -2 A ** (-2)

    Implied multiplication is illegal x ( y + z ) x * ( y + z )

    Use parentheses to group terms 2 ** ((8+2)/5)

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    28/47

    28

    Real Arithmetic (const. & var.) 3. / 4. = 0.75

    4. / 4. = 1. 5. / 4. = 1.25 6. / 4. = 1.5

    7. / 4. = 1.75 8. / 4. = 2. 9. / 4. = 2.25

    1. / 3. = 0.3333333

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    29/47

    29

    Integer Arithmetic (const. & var.) 3 / 4 = 0

    4 / 4 = 1 5 / 4 = 1 6 / 4 = 1

    7 / 4 = 1 8 / 4 = 2 9 / 4 = 2

    Truncation of fractions

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    30/47

    30

    Be careful..

    3. * (1. / 3.) 1. 3. * (0.3333333) = 0.9999999 2. * (1. / 2.) = 1. 2. * (0.5) = 1.

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    31/47

    31

    Evaluating expressions

    Example: Distance = 0.5 * accel * time **2 Distance = (0.5 * accel * time) **2

    Rules:Parentheses first ( innermost)

    2 * ( 3 + ( 4 2 ) 2 )Exponentials (right to left)

    2 **2 **3 = 2 **8 = 256Multiplication & Division (left to right)

    2 * 4 / 6Additions & Subtractions (left to right)

    2 + 6 - 12

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    32/47

    32

    Evaluating expressions Exercise:

    Power = (2 6) + ( 2 1 * (5+5) **2 **0) 8 = (2 6) + ( 2 1 * (10) **2 **0) 8 = (2 6) + ( 2 1 * (10) **1) 8

    = (2 6) + ( 2 1 * 10) 8 = (2 6) + ( 2 10) 8 = 4 + ( 8) 8

    = 4 8 8 = 20 Note: parentheses must be balanced.

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    33/47

    33

    Mixed-Mode expressions

    1 + 1 /4 = 1

    1. + 1 / 4 = 1.

    1 + 1. / 4 = 1.25

    Rule: An integer is automatically converted into real in case of

    mixed arithmetic

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    34/47

    34

    Mixed-Mode expressions

    1 + 1 /4 = 1

    1. + 1 / 4 = 1.

    1 + 1. / 4 = 1.25

    Rule: An integer is automatically converted into real in case of mixed arithmetic Raising a negative number to real power is not possible

    2 ** 2 = 4 -2 ** 2 = 4

    4 ** 0.5 = 2 -2 ** 0.5 ??

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    35/47

    35

    Data conversion To convert real to integer, use INT

    Anything after the decimal point is truncated Example: INT(3.3) = 3 INT(3.) = 3 INT(0.3) = 0

    To convert integer to real, use REAL A decimal point is added Example: REAL(3) = 3.

    Be careful: NINT INT NINT is used to round to the nearest integer Example: NINT(3.2) = 3 NINT(3.5) = 4 INT(3.5) = 3

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    36/47

    36

    INTRINSIC FUNCTIONS Functions:

    Generic functions (accept more than one type of inputs) Specific functions (accept one data type only)

    Examples: SQRT(X) ABS(X) SIN(X), COS(X), TAN(X) [X in radian] ASIN(X), ACOS(X), ATAN(X) [result in radian] EXP(X) LOG(X), LOG10(X) MAX(A,B), MIN(A,B)

    MOD (A,B)

    More (Table 2-4)

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    37/47

    More Examples

    y = SIN (theta)y = SIN(theta*(3.141593/180.) ! theta is in degreesREAL, PARAMETER :: deg_2_rad = 3.141593/180.! a constant to convert from !degrees to radian

    y = SIN (theta*deg_2_rad)y =SIN(2.57)y = SIN (x)y = SIN(pi*x)

    y = SIN(SQRT(x))

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    38/47

    38

    Debugging Fortran Program Errors (bugs)

    Eliminating error (debugging)

    Types of errors: Syntax errors

    Run-time errors

    Logical errors

    Good practice: Use IMPLICIT NONE

    Echo all inputs

    Initialize all variables

    Use parentheses properly If statement is very long break it into multiple lines

    Make sure all function and variables in same units

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    39/47

    Example 2-3

    Design a Fortran program that

    reads an input temperature in

    degrees Fahrenheit, converts it

    to an absolute temperature in

    kelvins, and writes out the result.

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    40/47

    Example 2-3 SolutionThe relationship between temperature in degrees

    Fahrenheit (F) and temperature in kelvins (K) canbe found in any physics textbook. It is

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    41/47

    Example 2-3 SolutionOur program must perform the following steps:

    1. Prompt the user to enter an inputtemperature in F.

    2. Read the input temperature.

    3. Calculate the temperature in kelvins from

    previous conversion equation.

    4. Write out the result, and stop.

    Example 2 3 Program (cont )

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    42/47

    Example 2-3 Program (cont.)

    Example 2-4 Background

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    43/47

    Example 2-4 Background

    From simple circuit theory, the rms current /,

    the real power P, reactive power Q, apparent

    power S, and power factor PF supplied to theload are given by the equations

    Example 2-4

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    44/47

    Example 2-4

    Given the rms voltage of the powersource Vand the magnitude and

    angle of the impedance Z, write aprogram that calculates the rms

    current I, the real power P, reactivepower Q, apparent power S, and

    power factorPF

    of the load.

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    45/47

    Example 2-4 Solution

    Example 2-4 Program

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    46/47

    Example 2-4 Program

    Example 2 4 Program (cont )

  • 7/30/2019 Chapter 2:Basic Elements of Fortran Programming

    47/47

    Example 2-4 Program (cont.)