Week 3 Let's review! Fundamental data types List-directed input/output.

27
Week 3 Let's review! Fundamental data types List-directed input/output

Transcript of Week 3 Let's review! Fundamental data types List-directed input/output.

Page 1: Week 3 Let's review! Fundamental data types List-directed input/output.

Week 3

Let's review!Fundamental data types

List-directed input/output

Page 2: Week 3 Let's review! Fundamental data types List-directed input/output.

Fundamental types of numbers

Integers Whole numbers (positive/negative/zero) Examples:

1952

3456787890123

0

-2334567 Typical range on a 32-bit computer

-2 x 109 to +2 x 109

Page 3: Week 3 Let's review! Fundamental data types List-directed input/output.

Fundamental types of numbers Reals

+/- xxx.yyyyy

xxx integer part

yyyyy fractional part A better representation:

Sign: +/- Mantissa: a fraction between 0.1 and 1.0 Exponent: x 10e

- 0.923456 x 10-6 or -0.923456e-6

Page 4: Week 3 Let's review! Fundamental data types List-directed input/output.

real and integer variables

Variable declaration:type :: name

type :: name1, name2, … integer :: a, b, c real :: x, y, z

Page 5: Week 3 Let's review! Fundamental data types List-directed input/output.

Arithmetic expressions

A combination of variables, constants, operators, parentheses…

(4*Pi*Radius * *2 + 1.2098)/2.3

Page 6: Week 3 Let's review! Fundamental data types List-directed input/output.

Assignment

name = expression

replace the content of the variable name with the result of the expression

Page 7: Week 3 Let's review! Fundamental data types List-directed input/output.

List-directed input and output

read *, var_1, var_2, … only variables!

print *, item_1, item_2, … variables, constants, expressions, …

Value separators: Comma (,) Space Slash (/) End-of-line

Page 8: Week 3 Let's review! Fundamental data types List-directed input/output.

List-directed input and output

Two consecutive commas: a null value is read the value of the variable is not set to zero, simply

its value is not changed! If the terminating character is a slash then no

more dataitems are read; processing of the input statement is ended

Example

Page 9: Week 3 Let's review! Fundamental data types List-directed input/output.

Character data 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

a b c d e f g h i j k l m n o p q r s t u w x y z0 1 2 3 4 5 6 7 8 9lj = + - * / ( ) , . ' : ! " % & ; < > ? $

Declaration:character (len=length) :: name1, name2, …

character (len=6) :: a, b, c

character (len=10*3):: a

character (len=10) :: b

Assignmenta = "What a lovely afternoon!" a will have the value of "What a lovely afternoon!ljljljljljlj"b = ab will have the value of "What a lov"

Page 10: Week 3 Let's review! Fundamental data types List-directed input/output.

Character data Concatenation:

Operator // Example:

character (len=10) a, b, c

a = "James"

b = "Bond"

c = trim(a)//"ž"//trim(b)

c will have the value of "James Bond"

Page 11: Week 3 Let's review! Fundamental data types List-directed input/output.

Named constants

type, parameter :: name1=constant_expression1, …

real, parameter :: pi=3.1415926, pi_by_2 = pi/2.0

integer, parameter :: max_lines = 200

Page 12: Week 3 Let's review! Fundamental data types List-directed input/output.

Now, some new stuff...

Basic Building Blocks

Page 13: Week 3 Let's review! Fundamental data types List-directed input/output.

Procedures

Divide and rule!

Main program Procedure 1

Procedure 2 Procedure 3

Page 14: Week 3 Let's review! Fundamental data types List-directed input/output.

Programs and modules

Main program unit

program nameuse statements...Specification statements...Executable statements...

end program name

Page 15: Week 3 Let's review! Fundamental data types List-directed input/output.

Programs and modules

Module program unit

module nameuse statements...Specification statements...

containsProcedure definitions...

end module name

Page 16: Week 3 Let's review! Fundamental data types List-directed input/output.

Procedures

Divide and rule!

Main program Procedure 1

Procedure 2 Procedure 3

Page 17: Week 3 Let's review! Fundamental data types List-directed input/output.

Procedures

Procedures - origin “Write your own” (homemade) Intrinsic (built-in, comes with F )

•sin(x), cos(x), abs(x), … Written by someone else (libraries)

Procedures (subprograms) - form Functions Subroutines

Page 18: Week 3 Let's review! Fundamental data types List-directed input/output.

Procedures

The main program amd any subprogram need never be aware of the internal details of any other program or subprogram!

Main program

Procedure 1

Procedure 2Procedure 3

Page 19: Week 3 Let's review! Fundamental data types List-directed input/output.

Functionsfunction cube_root result(root)! A function to calculate the cube root of a positive

real number! Dummy argument declaration

real, intent(in) :: x! Result variable declaration

real :: root! Local variable declaration

real :: log_x! Calculate cube root by using logs

log_x = log(x)root = exp(log_x/3.0)

end function cube_root

Page 20: Week 3 Let's review! Fundamental data types List-directed input/output.

Functions

function name (d1, d2, …) result(result_name)

Specifications part

Execution part end function name Variables

Internal (local) variables Result variable (keyword result) Dummy argument (keyword intent(in))

attribute

Page 21: Week 3 Let's review! Fundamental data types List-directed input/output.

Functions

No side effects!

Restrictions on the statements that can appear within a function

Page 22: Week 3 Let's review! Fundamental data types List-directed input/output.

Subroutinessubroutine roots (x, square_root, cube_root, fourth_root, & fifth_root)! Subroutine to calculate various roots of positive real! Number supplied as the first argument, and return them in! the second to fifth arguments

! Dummy argument declarations real, intent(in) :: x real, intent(out) :: square_root, cube_root, & fourth_root, fifth_root ! Local variable declaration real :: log_x ! Calculate square root using intrinsic sqrt square_root = sqrt(x) ! Calculate other roots by using logs log_x = log(x) cube_root = exp(log_x/3.0) fourth_root = exp(log_x/4.0) fifth_root = exp(log_x/5.0)

end subroutine roots

Page 23: Week 3 Let's review! Fundamental data types List-directed input/output.

Subroutines

call name (arg1, arg2, …) intent(in), intent(out), intent(inout)

Page 24: Week 3 Let's review! Fundamental data types List-directed input/output.

Arguments

Actual arguments in the calling program Dummy arguments in the subroutine or

function The order and types of the actual

arguments must correspond exactly with the order and types of the corresponding dummy arguments

Page 25: Week 3 Let's review! Fundamental data types List-directed input/output.

Objects

Local variables vs. global variables private vs. public objects

Page 26: Week 3 Let's review! Fundamental data types List-directed input/output.

Saving the values of local objects

Local entities within a procedure are not accessible from outside that procedure

Once an exit has been made, they cease to exist

If you want their values to ‘survive’ between calls, usereal, save :: list of real variables

Page 27: Week 3 Let's review! Fundamental data types List-directed input/output.

Homework

Due on March 8 from Ellis & Philips

4.3 4.8 4.9 4.11