 Download Presentation 1.1 Basics

# 1.1 Basics

Download Presentation ## 1.1 Basics

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. 1.1 Basics

2. 程式架構(I) program {name} {declarations} {other statements} stop end program {name} • 紅色部分為Fortran 90的做法

3. 程式架構(II) • we now show a program that computes the surface area of a sphere from the formula A= : program area real r, Aarea c this program reads a real number r c and prints the surface area of a sphere that c has radius r. read(*,*) r Aarea = 4. * 3.14159 * r * r write(*,*) Aarea stop end

4. Column position rules(Fortran77) I • Columns 1 : : 空白 1: 數字,代表行數 c : 當成註解 • Columns 6 : 第六行若為非零以外字元(即&),表示本行接續上一行程式 EX. a=b-c & & -d+e 此為 a=b-c-d+e

5. Column position rules(Fortran77) II • Columns 7-72 :程式敘述區(開頭需空六格) 程式敘述……….. • Columns 73-80:不使用,超過部份不被當成程式碼 • Fortran 90無以上columns的規定

6. Fortran 77 vs. 90 • 註解: - c 以後的字元當成註解(Fortran 77) - ! 以後的字元當成註解(Fortran 90) • 儲存檔案: *.for(Fortran 77) *.f90(Fortran 90)

7. Read and Write • Read : read( * , * ) {variables} 代表UNIT(輸出位置) FMT(輸出格式) • Write : write(UNIT= ,FMT= ) {variables} • 包裝字串 : 使用‘’單引號(Fortran 77), “”雙引號(Fortran90)

8. Type and Declaration • 整數:Integer {list-of-variables} • 複數:Complex {list-of-variables} • 字元:Character {list-of-variables} • 邏輯變數:Logical {list-of-variables} • 浮點數(單精準):Real {list-of-variables} • 浮點數(雙精準):Double precision {list-of-variables} • 變數名稱可由a~z, A~Z, 0~9組成, 但須注意,要以字母開頭且變數長度不超過6個

9. Integer Variables • In a typical computer might be allocated for each variable x with 32bits , , and then x = Ex.

10. Floating Point Variables • Numbers that are stored in real or double precision variables are represented in Floating point style. • A typical 32-bit FP number x might have a 24-bit mantissa m and an 8-bit exponent e, • 64-bits computer has 56bits for mantissa.

11. Arithmetic Assignment • add(+) , subtract(-), multiply(*) , divide(/) : Ex. 7*4-6/3 • exponential : Ex. 2**3 (2的3次方=8) • build-function : Ex. sqrt(4) (4開平方=2.000000)

12. String Manipulation I • 宣告: character * 7 str 字串 str長度為7(初始設定為1) • 假設s1 = ‘abcde’ , s2=‘xyz’ : -連結: s=s1//s2 s=‘abcdexyz’ -指定: s1(2:4)=‘123’ s1=‘a123e’ -抽出: s=s1(2:5) s=‘bcde’

13. String Manipulation II • 內建函數 (當s=‘abcdabcd’) : -計算長度(length): Ex. length(s)=8 -比對內文(index): Ex. index(s,’da’) => s=abcdabcd => 4 index(s,’dc’) => s=abcdabcd => 0 -輸入數字,對照電腦使用的字元集(char): Ex. char(72)=H -輸入字元,對照電腦使用的字元集(ichar): Ex. ichar(H)=72 • Fortran 90可使用len求字串長度

14. Type conversion c-char r-real d-double precision i-integer x-real,double precision, integer Ex. If ksum is an integer and sum is real then sum = real (ksum) converts ksum to a floating representation(real), and stores the result in sum.

15. Parameter Statement • 在程式中,有時需要固定不變的資料常數,如圓周率, 重力加速度…等. 為了簡化程式,可直接宣告成 parameter. • Ex. program area real r, area1 ,fourpi parameter (fourpi = 12.566e0) read(*,*) r area1 = fourpi * r * r write(*,*) area1 stop end

16. 1.2 LOGICAL OPERATIONS

17. Logical Variables • Declaration : logical a,b • Assigned either the value .TRUE. Or .FALSE. Ex. a = .TRUE. • It could be show like “T” to .TRUE. “F” to .FALSE. • It could be also assigned to .true. or .false. with the lower case.

18. Logical Expressions I • We define some relational operators : .LT. Less than .LE. Less than or equal .EQ. Equal .NE. Not equal .GT. Greater than .GE. Greater than or equal Ex. How to test if i between 3 and 7 ? Ans: a =(3 .LE. i).AND.(i .LT. 10)

19. Logical Variables II • Truth table (And ,Or , Not) :

20. “IF” Constructs I • The simplest ‘if’ statement : if ({logical expression}) {executable statement} Ex. The following statement prints count if count is positive. if (count .GT. 0) write(*,*) count

21. “IF” Constructs II • if ( {logical expression} ) then {statement} endif Ex. The following statement prints count if it is positive if (count .GT. 0) then write(*,*) count endif

22. “IF” Constructs III • The most general form of ‘if’ : if ( {logical expression} ) then {statement} elseif ( {logical expression} ) then {statement} : elseif ( {logical expression} ) then {statement} endif

23. “IF” Constructs IV • A program reads three distinct integers and prints the median: program median integer x,y,z,median1 read(*,*)x,y,z if ( (x-z)*(x-y) .LT. 0 ) then median1 = x elseif ( (y-x)*(y-z) .LT. 0 ) then median1 = y else median1 = z endif write(*,*) median1 stop end

24. Stylistic Considerations • Make the program statements involves indentation to enhances the readability of if-then-else constructs. • An example :

25. 1.3 LOOPS

26. While-Loops I • {label} if ( { logical expression }) then {statements} goto {label} endif • All the labels in a program must be unique, except in two program ,like between main program and subprogram. • The label is a number between 1 and 99999 and must be situated within columns 2 through 5.

27. While-Loops II • Example:Prints Fibonacci numbers that are strictly less than 200. program fibon integer x, y, z x = 1 y = 1 10    if ( x .LT. 200 ) then write(*,*)x z = x + y y = x x = z goto 10 endif stop end

28. Until-Loops • {label} continue {statements} if ( { logical expression }) goto {label} • Example:Prints Fibonacci numbers that are strictly less than 200. program fibonacci integer x, y, z x = 1 y = 1 10  continue z = x + y y = x x = z write(*,*)x if ( x .LE. 200 ) goto 10 stop end

29. Do-Loops I • do {label} {var} = { exp.1 },{ exp.2 },{ exp.3 } {statements} {label} continue • var is the loop index. exp.1 is an initial value of var. exp.2 is the terminating bound. exp.3 defines the increment(or decrement). labelis code used to assign the loop range.

30. Do-Loops II • Example:Prints Fibonacci numbers from x1 to x10. program fibonacci integer x, y, z x = 1 y = 1 do 10 count = 1, 10 z = x + y y = x x = z write(*,*)x 10 continue stop end

31. Do-Loops III • Example: compute s = n(n-1)…….(n-k). s = n do 20 j = n-1, n-k, -1 s = s*j 20 continue • The do statement here says “step from n-1 to n-k in steps of –1 ”.

32. Nesting Do-Loops • Example: Prints all integer triplets (i,j,k) with the property that . do 10 i = 1, 100 do 5 j = 1, 100 k = int( sqrt( real( i*i + j*j ) ) ) if ( k*k .EQ. i*i + j*j ) then write(*,*)i, j, k endif 5 continue 10 continue

33. More on “goto” and “continue” • The ‘no operation’continue statement is useful for specifying the target of the goto. For Example 10 continue {statement} goto 10 cause control to return up to statement 10 and continue (downward) form there. • The gotostatement should be used only as last resort since the presence ofgoto’s in a program makes for difficult reading. • A do-loop must be entered ‘from the top’. Never jump into the body of a do-loop.

34. Loops, Indentation, and Labels • Indentation : The body of a loop should be uniformly indented to highlight the program structure and it should be more readability. • Labels : Labels should be assigned so that the sequence of numbers is increasing,top to bottom. It’s good practice to leave gaps between the value of consecutive label and the subsequence program will be easily accommodated.

35. 1.4 ARRAYS

36. One-dimensional Arrays I • Declaration {type} {name} (size) Ex. real a(100) - one-dimensional - real array of size 100

37. One-dimensional Arrays II • Content the content of cell 1 is denoted by a(1) • Declaration {name} ({first_index}:{last_index}) Ex. Claim a real array of size 100, with index range –41 to 58: real b(-41:58)

38. Two-dimensional Arrays I • Declaration {name} (number of row, number of column) Ex. integer A(4,5), identify a two-dimensional integer array of size 4x5:

39. Two-dimensional Arrays II • Declaration • The total size of the array is (last_index1 - first_index1 + 1)*(last_index2 - first_index2 + 1) • integer k(20,100)integer k(1:20,1:100) • It’s the programmer’s responsibility to ensure that the index values are meaningful. Do not assume that array entries are automatically initialized to zero by the compiler.

40. Space Allocation for Two-dimensional Arrays I • Fortran stores a two-dimensional array as a contiguous, linear sequence of cells, by column. For example: 4x5 array

41. Space Allocation for Two-dimensional Arrays II • Note the storage of the 3-by-3 times table matrix in the 4-by-5 array results in unused array space: (the following matrix elements are not contiguous in memory)

42. Space Allocation for Two-dimensional Arrays III • Address of an array element is addr[A(i,j)]=addr[A(1,1)]+(j-1)*adim+(i-1) where A is an m-by-n matrix stored in array A that has row dimension adim.

43. Space Allocation for Two-dimensional Arrays IV • Physical address: Example: A cell may be 4 bytes long for integer arrays and 8 bytes long for double precision arrays. Then,the physical address is addr[A(i,j)]=addr[A(1,1)]+[(j-1)*adim+(i-1)]*(4 or 8)byte

44. program frank integer F(15, 15), n, i, j do 25 n = 2, 6, 2 do 10 i = 1, n do 5 j = 1, n if ( i .LE. j ) then F(i, j) = n-j+1 elseif ( i .EQ. j+1 ) then F(i, j) = n-j else F(i, j) = 0 endif 5 continue10 continue do 15 i = 1, n write(*,*)(F(i, j), j=1, n)15 continue do 20 i = 1, n write(*,*)(F(j, i), j=1, n)20 continue25 continue stop end

45. Packed Storage • Packed form: It’s a useful data structure when dealing with symmetric and triangular matrices,and only store the lower triangular portion in column-by-column fashion in a one-dimensional array. Ex. k = 1 do 10 j = 1, n do 5 i = j, n c c a(k) = A(i, j) c a(k) = i*j k = k+1 5 continue 10 continue

46. Arrays of Higher Dimension • It’s possible to manipulate arrays of dimension up to seven. real A( 2, 3, 6, 2, 8, 7, 4) means that there are 2x3x6x2x8x7x4=16128 memory locations are reserved. Ex. Assign (abcd)2 to hcube(a,b,c,d) [binary to decimal] do 40 a = 0 , 1 do 30 b = 0 , 1 do 20 c = 0 , 1 do 10 d = 0 , 1 hcube(a,b,c,d)=8*a+4*b+2*c+d 10 continue 20 continue 30 continue 40 continue

47. 1.5 SUBPROGRAMS

48. Built-in Functions • Specific A specific function requires arguments of a particular type and returns a predefined type. Ex. char(72) and we will have a string“H” • Generic A generic function accepts arguments of any appropriate type ;the return value is determined by the types of the arguments. Ex. x = cos(y)

49. Functions I • Declaration {type} function {name} ({list-of-variables}) {Declaration} {statements} return end

50. Functions II • Rules for user-defined function -The value produced by the function is returned to the calling program via a variable whose name is the function’s name. Ex. f(x) is returned in the variable f -A function has a type and it must be declared in the calling program. -The body of a function resembles the body of a main program and the same rules apply. -Execution begins at the top and flows to the bottom. Control is passed back to calling program when returnstatement is encountered the subroutine is exited. -A reference to a defined function can appear anywhere in an expression,subject to type consideration.