BIL106E - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

BIL106E

Description:

An Integer is a whole number (positive, negative, or zero) and does not contain ... ATTENTION: Continuation line with ampersand symbol. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 43
Provided by: nuzhet
Category:

less

Transcript and Presenter's Notes

Title: BIL106E


1
BIL106E
  • Basic FORTRAN

2
Contents
  • Data Types
  • Names and Type Declarations
  • Arithmetic Expressions and Assignment
  • Simple Input/Output

3
Data Types
  • Five Basic Types of Fortran
  • Integer
  • Real
  • Complex
  • Character
  • Logical
  • Custom Data Types

Numerical Data Types
Non-Numerical Data Types
4
Integer
  • An Integer is a whole number (positive, negative,
    or zero) and does not contain commas or a decimal
    point.
  • Examples
  • 5
  • 1024
  • -512
  • a5 b1024 c-512
  • Integer variable declaration in F
  • integer a,b,c

5
Real
  • A real number must contain a decimal point, but
    no commas are allowed.
  • Examples
  • 5.125
  • -0.0273
  • 0.0571
  • 6.5274684E3 means 6.5274684X103
  • x5.125 y-0.0273 z0.0571
  • Real variable declaration in F
  • real x,y,z

6
Complex
  • A complex number is represented as a pair of real
    (i.e., floating point) numbers.
  • The first component of the pair represents the
    real part of the complex data object and the
    second represents the imaginary part.
  • For example
  • Z a i b Z (a,b)
  • -34i
  • NOTE Since you dont have enough background on
    complex numbers,
  • you are not responsible from complex numbers and
    its related
  • examples.

7
Character
  • Character constants, also called strings, are
    sequences of symbols from the ANSI standard
    character set for Fortran.
  • The number of character constants between double
    quotes is the length of constant
  • For example
  • "This is a string" (the length is 16)
  • "abcD-12o" (the length is 8)
  • "John Q. Doe" (the length is 11)
  • NOTE Use straight quotes " not smart quotes or
  • character variable declaration in F
  • Character (lenlenght) name1, name2
  • Lenght positive int or positive int constant
    expression

8
Allowed characters in F
  • A B C D E F G H I J K L M N O P Q R S T U V W X Y
    Z
  • a b c d e f g h i j k l m n o p q r s t u w x y z
  • 0 1 2 3 4 5 6 7 8 9
  • ? - / ( ) , . ' lt gt ! " ?
  • where ? represents the space or blank character

9
Logical
  • An object of logical type has the value
  • true or false, or (0 or 1), or (yes or no).
  • in F syntax
  • .TRUE.
  • .FALSE.
  • logical variable declaration in F
  • logical flag1, flag2

10
  • Data Types
  • Names and Type Declarations
  • Arithmetic Expressions and Assignment
  • Simple Input/Output

11
Names
  • Data objects are memory locations in which
    information is stored. There are 2 kind of basic
    data objects constants and variables.
  • Constant's value can NOT change during the
    execution of the porgram. It remains constant.
  • A variable's value CAN change during the
    execution of the porgram.
  • Names show the location of the data objects in
    the computers memory.

Computer's memory
Name of the data object
year 2003
Address of memory location
12
Names
  • Examples for names
  • Average, X, Y, Einstein, or Potential_Energy.
  • "Name"s are any kind of word either
  • - "reserved word"s ? can not be used by you,
  • - "identifier"s ? defined by you
  • IMPORTANT reserved words (keywords) such as
    program, write, and end are not actually names

13
Syntax for Names
  • Names in a program must conform to 3 rules
  • 1) A name may contain up to 31 letters, digits,
    and underscore characters (which must not be the
    last character of the name)
  • 2) The first character of a name must be a letter
  • 3) Imbedded blank characters are not permitted in
    a name
  • Examples
  • Degree aVariable CONSTANT
  • Name_Surname Var2 var_6_A
  • this_not_valid_
  • this is not_valid

14
Type Declarations for Variables
  • Every variable must appear in a type declaration
  • The type of a Fortran variable determines the
    type of value that may be assigned to that
    variable.
  • In every F program, the specification statement
    implicit none must immediately follow the program
    statement
  • program Research
  • implicit none
  • ...
  • end program Research
  • Type Declaration Form
  • Type List of names

15
Type Declarations for Variables
  • implicit none
  • integer Counts, Loop_Index
  • real Current, Resistance, Voltage
  • character (len12) MyName, YourName
  • Names defined as part of the F language,
    including keywords and intrinsic function names
    (such as sin, tan, abs, etc.), must be written in
    lower case.
  • Names that you invent can use any combination of
    upper and lower case, but each name must be
    written consistently.

16
Variable initialization
  • All variables are initially undefined
  • Initialization in the declarations
  • Examplesreal W1.2, z5.678,
    mass4.56integer year1998, count0

17
Constants
  • Constant's value can NOT change during the
    execution of the porgram.
  • 2003 is an integer constant
  • 3.1415 is a real constant
  • "Happy birty day to you" is a character constant
  • It is not a good practice to use the same
    constant with its fixed value at different places
    of the code. When we need to change the value we
    have to search and replace for all the
    appearances of it within the program which is a
    tedious and an error-prone task.
  • What should we do ?
  • Use ? Named Constants

18
Type Declaration for Named Constants
  • Declaration of a named constant is as follows
  • Type, parameter List of initializations
  • where each item in the List of initializations
    has the form
  • Name value
  • The value definition is an explicit constant.
  • Examples
  • integer, parameter LENGTH12
  • real, parameter PLANK6.6260755e-34,
    PI3.141593
  • real, parameter GRAVITY9.807,
    AVAGADRO6.0221367e23,
  • twoPI2.0PI
  • integer, parameter A20, HIGH30, NEON67
  • character (Len2), parameter unitsCm
  • ATTENTION Continuation line with ampersand
    symbol.
  • It's a good practice to write named constants in
    upper case

19
  • Data Types
  • Names and Type Declarations
  • Arithmetic Expressions and Assignment
  • Simple Input/Output

20
Arithmetic Expressions and Assignment
  • Expression An expression consists of a
    combination of operators and operands
  • Examples
  • 4
  • -6
  • 421
  • a(bc/d)/20
  • q52
  • xq3
  • agt3

21
Arithmetic Operators in F
  • Operator Meaning
  • addition
  • - substraction
  • multiplication
  • / division
  • exponentiation

22
Priorities of Operators
  • Operator Priority
  • High (1st)
  • and / Medium (2nd)
  • and - Low (3rd)
  • Examples
  • A b c d / e f g / h I j k
  • 2 3 2 512
  • 10/5 2 2 2 4
  • To calculate 51/3 5.0(1.0/3.0)
  • but not
  • 5.0(1/3) à 5.00 à 1.0

23
Priority Rules
  • All exponentiations are performed first
    consecutive exponentiations are performed from
    right to left
  • All multiplications and divisions are performed
    next in the order in which they appear from left
    to right
  • Additions and subtractions are performed last, in
    the order in which they appear from left to right

24
Example
  • 2 3 2 512
  • 10/5 2 2 2 4
  • (23)2 82 64
  • 10/(52) 10 / 10 1
  • To be at the safe side
  • Use ? ( )

25
Assignment Statement
  • Formvariable expression
  • Assigns the value of expression to variable
  • Assignment is not a statement of algebraic
    equality
  • it is a replacement statement
  • ExamplesDensity 2000.0Volume 3.2Mass
    DensityVolumeWeightRatio log(Mass/90.)

26
Mixed-mode assignment
  • Assume that, b is a real variable whose value is
    100.0, while c and d are integers having the
    values 9 and 10, respectively.
  • a bc/d
  • result is 90.0
  • a c/db
  • a gets 0 value.
  • This phenomenon is known as integer division

27
Statement
  • Statements are the primary building blocks of a
    program.
  • Two kind simple and compound (block)
  • Some Simple statements
  • Declaration real aValue, x
  • Assignment aValue 34 x
  • Function call call aFunction(aValue)
  • Control if (x 0) then

  • aValue 34
  • end if
  • Null ! explanation
  • They are executed in sequence for their effect,
    and do NOT have values.

28
  • Data Types
  • Names and Type Declarations
  • Arithmetic Expressions and Assignment
  • Simple Input/Output

29
Simple Input/Output
  • Two kinds of I/O (for the moment!)
  • Formatted I/O
  • List-directed I/O
  • List-directed outputprint , output-listwrite
    (unit, fmt) output-list
  • List-directed inputread , input-listread
    (unit, fmt) input-list

30
Simple Input/Output
  • An asterisk as the unit in a read or write
    controls list designates the default input device
    (the keyboard) or the default output device (the
    terminal screen)
  • An asterisk as the format designates
    list-directed formatting. Input data values for
    on-line list-directed input are entered at the
    computer keyboard in free form. Values must be
    separated by blanks.
  • For example
  • read (unit , fmt ) xval,yval,zval
  • can be entered as
  • 10.0 100.0 2.5

31
Simple Input/Output
  • IMPORTANT The items in an input list must be
    variable names.
  • Examples
  • write (unit , fmt ) Please enter the
    diameter of a circle
  • read (unit , fmt ) Diameter
  • write (unit , fmt ) Diameter ,
    Diameter, circumference ,
  • 3.1416Diameter, Area , 3.1416Diameter2
  • print , "Tell me your birthday"
  • write (unit, fmt) a, b, c2
  • read , day, month, year

32
Program style and design
  • A program must be correct, readable, and
    understandable. The basic principles for
    developing a good program are as follows
  • 1) Programs cannot be considered correct until
    they have been validated using test data.
  • 2) Programs should be well structured
  • Use a top-down approach when developing a program
    for a complex problem.
  • Strive for simplicity and clarity
  • 3) Each program unit should be documented
  • Include opening documentation
  • Use comments
  • Use meaningful identifiers
  • Label all output
  • 4) A program should be formatted in a style that
    enhances its readability
  • 5) Programs should be readable and understandable
  • Do not use magic numbers
  • Use comments to describe the purpose of a program
    and variables

33
Example
34
Example
35
Example
36
Example
37
Example
38
Example
39
Warnings for Frequently Made Errors
  • 1) Do NOT use TAB in the editor
  • 2) Do NOT use single quatition mark for
    characters
  • 3) Place , between successive variables in
    command like print , write, declerations etc

40
Homework 1
  • Every student will caluculate
  • Perimeter and Area of Circle, Rectangle and
    Triangle
  • USE named constant(s), Variables
  • Input all necessary variable data from keyboard
    and print out all data and results.
  • Run your program for three different data sets.
  • Include both sourse code and program output in
    your work
  • Use Courier New font for source code and output
  • Due date 20/2/2003
  • Homeworks will be delivered in a floppy disk

41
Term Project
  • Establish teams for TERM PROJECT consisting of 5
    student, choose a team representative. Each
    representative email me the members of the teams.
  • Due Date 20/2/2003

42
Self Study
  • ELLIS PHLIPS book
  • p. 55 Self-test Exercise 3.1
  • Problem 9
  • p. 63 Self-test Exercise 3.2
  • Problem 4
  • Reading Assignement
  • p. 103-134
Write a Comment
User Comments (0)
About PowerShow.com