2 - basic fortran

Upload: shaz333

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 2 - Basic Fortran

    1/27

    Department of Civil EngineeringUniversity of Malaya

    Course Code :KAEA 2101

    Course Name :Computer Programming

    Chapter 2 :Elementary of Fortran

    Prepared By :AP. Dr. Lai Sai Hin

    http://rds.yahoo.com/_ylt=A0S020s3DCBNJD0AfRCjzbkF/SIG=123o4esud/EXP=1294032311/**http:/www.mr-accounting.com/images/logo/UM.jpg
  • 8/11/2019 2 - Basic Fortran

    2/27

    Previous Example: Radioactive decay

    PROGRAM Radioactive_Decay!-------------------------------------------------------------------------------------------------------------------------------! This program calculates the amount of a radioactive substance that

    ! remains after a specified time, given an initial amount and its! half-life. Variables used are:! InitialAmount : initial amount of substance (mg)! Half Life : half-life of substance (days)! Time : time at which the amount remaining is calculated (days)! AmountRemaining : amount of substance remaining (mg)!! Input: InitialAmount, Half Life, Time

    ! Output: AmountRemaining!-------------------------------------------------------------------------------------------------------------------------------IMPLICIT NONE

    REAL :: InitialAmount, Half Life, Time, AmountRemaining

    ! Get values for InitialAmount, Half Life, and Time.PRINT *, "Enter initial amount (mg) of substance, its halflife (days)"PRINT *, "and time (days) at which to find amount remaining:"

    READ *, InitialAmount, Half Life, Time

    ! Compute the amount remaining at the specified time.AmountRemaining = InitialAmount * 0.5 ** (Time / Half Life)

    ! Display AmountRemaining.PRINT *, "Amount remaining =, AmountRemaining, "mg"

    END PROGRAM Radioactive_Decay

    Data Type

  • 8/11/2019 2 - Basic Fortran

    3/27

    Data Types

    Fortran Provides 5 basic data types

    Integer

    Real

    Complex

    Character

    Logical

    Numeric typesConstant/variable

    Chapter 3

  • 8/11/2019 2 - Basic Fortran

    4/27

    Data Types

    Integers

    A whole number

    Positive, negative, or Zero

    Does not contain comma or decimal point

    0137

    +2516

    -17745

    9,999

    16.0

    - - 5

    7-

    Valid integer constants

    Invalid

  • 8/11/2019 2 - Basic Fortran

    5/27

    Data Types

    Reals

    ordinary decimal numbers, or in exponential notation Positive or negative

    Must contain a decimal point, without comma

    123.234

    -0.01536+56473.02

    +56473.

    12,345.03

    5801.23234E2

    123.234 E0

    12323.4E-2

    123234E-3

    Valid real constants

    Invalid

    Valid exponential representation

  • 8/11/2019 2 - Basic Fortran

    6/27

    Data Types

    Character Strings

    Also called strings Sequences of symbols from the Fortran character set

    Must be enclosed between double quotes or betweenapostrophes.

    PDQ123-A

    John Q. Dow

    John Q. Doe

    Dont

    John Q. Doe

    John Q. DoeInvalid

    Valid

  • 8/11/2019 2 - Basic Fortran

    7/27

    Data Types

    Character Strings

  • 8/11/2019 2 - Basic Fortran

    8/27

    Data Types

    Identifiers

    MassSpeed_of_Light

    speed-ofLight6FeetVelocityVELOCITYvELocITy

    Invalid

    Same

    e.g.:READ, PRINT

    Valid

  • 8/11/2019 2 - Basic Fortran

    9/27

    Data Types

    Variables

    Symbolic name used to refer to a quantityInitialAmount, Half Life, Time, AmountRemaining

    Variable names are identifiers, must follow the rules for

    forming valid identifiers

    Necessary to declare the type of each variable in a

    Fortran program

    REAL :: list

    REAL :: InitialAmount, HalfLife, Time, AmountRemaining

    INTEGER :: list

    CHARACTER (LEN = N) :: list or CHARACTER (N) :: list

    CHARACTER (LEN = 15) :: FirstName, LastName

    CHARACTER :: Initial

    CHARACTER (LEN =15) :: FirstName, LastName*20, Initial*1, City

    Length = 1Override the specified length

  • 8/11/2019 2 - Basic Fortran

    10/27

    Data Types

    IMPLICIT NONE Statement

  • 8/11/2019 2 - Basic Fortran

    11/27

    Variable Initialization

    Initial values can be assigned to variables (at compile

    time)REAL :: list

    REAL :: W, X, Y, Z

    REAL :: W = 1.0, X = 2.5, Y = 7.73, Z = -2.956

  • 8/11/2019 2 - Basic Fortran

    12/27

    Constant Attribute

    Fortran allows programmer to specify a constant by including

    a PARAMETER attribute in the declaration of the identifier For frequently used constant such as:

    INTEGER :: Limit

    INTEGER, PARAMETER :: Limit = 50

    CHARACTER(2) :: Unit

    CHARACTER(2), PARAMETER :: Unit = cm

    REAL :: Pi, TwoPi, g

    REAL, PARAMETER :: Pi = 3.141593, TwoPi = 2.0*Pi, g = 9.81

    REAL, PARAMETER :: BirthRate = 0.1758, DeathRate = 0.1257

    Why need to declare attribute ???

    Increase readability,

    Easier to modification.

    Change = (0.1758-0.1257) * Population

    Population = Population + Change

    :

    PopulationIncrease = 0.1758 * populationPopulationDevrease = 0.1257 * Population

  • 8/11/2019 2 - Basic Fortran

    13/27

    Arithmetic Operations & Functions

    Operation

    In FortranAddition (+), Subtraction (-), Multiplication (*), Division(/), Exponentiation (**)

    B2 - 4AC B ** 24 * A * C

    - (A + B) OK

    N * -2 not OK N * (-2) (OK)

    When 2 constants or variables of the same type arecombined using one of the 4 basic arithmetic operation,the result has the same type as the operands

    3 + 4 = 7

    3.0 + 4.0 = 7.09.0 / 4.0 = 2.25

    9 / 4 = 2

  • 8/11/2019 2 - Basic Fortran

    14/27

    Arithmetic Operations & FunctionsMixed-Mode Expressions

    it is possible to combine integer and real quantities When an integer quantity is combined with a real one,

    the integer quantity is converted to its real equivalent,

    the result is of real type

    ???

    A real exponent

    should be never beused in place of an

    integer exponent

    or

  • 8/11/2019 2 - Basic Fortran

    15/27

    Arithmetic Operations & FunctionsMixed-Mode Expressions

    4. The standard order of evaluation can be modified using parentheses to enclose

    subexpressions, the subexpressions are evaluated first in the standard manner.

    5. If the parentheses are nested, the computations in the inner most parentheses are

    performed first.

    (5 * (115) ** 2) * 4 + 9 (5 * 6 ** 2) * 4 + 9 180 * 4 + 9 720 + 9 729

  • 8/11/2019 2 - Basic Fortran

    16/27

    Arithmetic Operations & Functions

    Functions

    Fortran provides library functions for many of thecommon mathematical operations and functions

    SQRT(argument)

    argument is a real-valued constant, variable, expression

    SQRT(7.0)

    SQRT(B**2-4.0*A*C) Nonnegative value only

    to calculate SQRT of an integer, necessary to convert it to a real value

    SQRT(REAL(7))

  • 8/11/2019 2 - Basic Fortran

    17/27

    Arithmetic Operations & Functions

    Basic Fortran Functions

  • 8/11/2019 2 - Basic Fortran

    18/27

    Arithmetic Operations & Functions

    The ASSIGNMENT STATEMENT

    Used to assign values to variables

    Suppose that Xcoordinate and YCoordinate are real variables, Number and Term

    are integer variables.

    REAL :: Xcoordinate, Ycoordinate

    INTEGER :: Number, Term

    Consider the following Assignment Statements

    XCoordinate = 5.23

    YCoordinate = SQRT(25.0) 5.0

    Number = 17

    Term = Number / 3 + 2 7

  • 8/11/2019 2 - Basic Fortran

    19/27

    Arithmetic Operations & Functions

    Mixed-Mode ASSIGNMENT

    If an integer-valued expression is assigned to a real variable,the integer value is converted to a real constant and thenassigned to the variable

    if N has the value 9, Alpha and Beta are real variable,

    Alpha = 3 3.0

    Beta = (N + 3) / 5 2.0

    If an real -valued expression is assigned to an integervariable, the fraction part of the real value is truncated, the

    integer part is assigned to the variableif X has the value 5.75, I, Kappa, Mu are integer variables

    I = 3.14159 3Kappa = X / 2.0 2Mu = 1.0 / X 0

  • 8/11/2019 2 - Basic Fortran

    20/27

    Input / OutputFortran provides 2 types of input/output statements

    Type 1

    - Explicitly specify the format of input & output.

    Type 2: list-directed input/output

    - predetermined standard formats that match the input/output

    list are automatically provided by the compiler- List-Directed InputREAD *, InitialHeight, InitialVelocity, Time

    - List-Directed Output

    Execution

    100.0, 90.0, 4.5

    100.0 90.0 4.5

    100.0 90.0

    4.5

  • 8/11/2019 2 - Basic Fortran

    21/27

    Example 1: Velocity and Height of a Projectile

    !

  • 8/11/2019 2 - Basic Fortran

    22/27

    Program Composition and FormatGeneral form of a program in Fortran Heading

    PROGRAM name

    Opening documentation

    Specification Part / Type DeclarationIMPLICIT NONE

    Declaration statement

    Execution partNonexecutable statements

    - provide information, do not cause any specific action to be performed during

    execution,

    e.g.: PROGRAM statement & type statements

    Executable statements- specify actions to be performed during execution

    e.g.: Assignment statements, input/output ststements

    Subprogram part- contain internal subprogram, chapter 6

    END PROGRAM statement

  • 8/11/2019 2 - Basic Fortran

    23/27

    File Input/OutputInteractive Input/Output So far we assumed the data for the sample program was entered from

    the keyboard, and the output was displayed on the screenInput/Output Using File To store input data in a disk file, design the program to read data from

    this file, and to store a program output in a disk file.E.g.: Projectile Program- input value are read from file named fig2-6.dat

    100.0, 90.04.5

    - output to be written to a file named fig2-6.outAt time 4.5000000 secondsthe vertical velocity is 45.8700752 m/secand the height is 4.0570767E+02 meters

    Opening Files / OPEN statement- before a file can be used for input or output, it must be opened

    OPEN (UNIT = unit-number, FILE = file-name, STATUS = status)unit-number is an integer that will be used to reference this file in a READ or WRITE

    statement, status =OLD if the file already exists, or NEW otherwise

    OPEN(UINT = 12, FILE = fig2-6.dat, STATUS = OLD)

    OPEN(UNIT = 13, FILE = fig-2.6.out, STATUS = NEW)

  • 8/11/2019 2 - Basic Fortran

    24/27

    File Input/Output Opening Files / OPEN statement

    - before a file can be used for input or output, it must be opened

    OPEN (UNIT = unit-number, FILE = file-name, STATUS = status)unit-number is an integer that will be used to reference this file in a READ or WRITE

    statement, status =OLD if the file already exists, or NEW otherwise

    OPEN(UINT = 12, FILE = fig2-6.dat, STATUS = OLD)

    OPEN(UNIT = 13, FILE = fig2-6.out, STATUS = NEW)

    File I/O- once a file has been given a unit number, data can be read from or written

    to that file.

    READ (unit-number, *) input-list

    WRITE (unit-number, *) output-list

    READ (12, *) InitialHeight, InitialVelocity, TimeWRITE (13, *) At time, Time, seconds

    WRITE (13, *) the vertical velocity is, Velocity, m/sec

    WRITE (13, *) and the height is, Height, maters

    At time 4.5000000 seconds

    the vertical velocity is 45.8700752 m/secand the height is 4.0570767E+02 meters

    Execution

    Fig2-6.out

  • 8/11/2019 2 - Basic Fortran

    25/27

    Example 2: Projectile program with file I/O

  • 8/11/2019 2 - Basic Fortran

    26/27

  • 8/11/2019 2 - Basic Fortran

    27/27

    Thank You