1 / 40

Introduction to Computer Sciences References:

Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester Forsythe. Grading system: 1. Performance (quizzes): 30% 2. Midterm: 30% 3. Final: 40%. Office hours: (PH224)

overton
Download Presentation

Introduction to Computer Sciences References:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester Forsythe. Grading system: 1. Performance (quizzes): 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Mon. 13:40~15:30 2. Wed. 10:10~12:00

  2. An Intuitive Walk Through the World of Computers What is a Computer Program? A computer program consisters of a collection of instructions that put together In a specific order to make a computer accomplish some task. e.g., PROGRAM sq_root REAL :: square_root square_root = SQRT(2.0) WRITE(*,*) square_root END PROGRAM What a Computer Can’t do? Computers have no built-in intelligence. They are not smart.

  3. Computer Languages • Machine language: The actual language that a computer • recognizes and executes. • High-level languages: Basic, C, Fortran, … • The History of the Fortran Language • Fortran = Formula translation • Fortran 66 Fortran 77 Fortran 90 Fortran 95 • (1966) (1977) (1991) (1996) Fortran 2003 (2004)

  4. High-Level Languages Fortran program  Fortran compiler  Machine language • Learn to Design First • Think before you act! • It is essential to use your mind first and create designs for your • programs. • Program Design: • Grasp the problem. • Break the problem down. • Shape the solution for each main idea. • Debug/Test the program. • Make each program unit clear and understandable.

  5. The Structure of a Fortran Program (A simple Fortran program) PROGRAM my_first_program ! Purpose: … ! Declare the variables INTEGER :: i, j, k !All variable are integers ! Get the variables WRITE (*,*) " Enter the numbers to multiply:" READ (*,*) i, j k = i * j ! Write out the result WRITE (*,*) 'Result = ', k STOP END PROGRAM (Declaration Section) (Execution Section) (Termination section)

  6. List-directed (or free-format) Input and Output Statements • The list-directed input statement: • READ (*,*) input_list • I/O unitformat • The list-directed output statement: • WRITE (*,*) output_list • I/O unitformat

  7. The IMPLICIT NONE Statement When the IMPLICIT NONE statement is included in a program, any variable that does not appear in an explicit type declaration statement is considered an error. e.g., PROGRAM test_1 REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘,tmie END PROGRAM Output: Run-timeerror! (depends on machines)

  8. + IMPLICIT NONE, PROGRAM test_1 IMPLICIT NONE REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘,tmie END PROGRAM Output: Compile-timeerror! (depends on machines)

  9. Program Examples Example (Temperature conversion) T (0F) = (9/5) T(0C) + 32

  10. Example (extra) Write a program for converting a 4 bits integer into a base 10 number, e.g., 1 0 1 1 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 11

  11. Assignment Statements and Logical Calculations • Assignment statements: • logical variable name = logical expression • Logical operators: • relational operators • combinational operators Relational Operators a1 op a2 a1, a2: arithmetic expressions, variables, constants, or character strings. op: the relational logical operators. (see Table below)

  12. operation meaning = = equal to / = not equal to > greater than > = greater than or equal to < less than < = less than or equal to e.g., operation result 3 < 4 .TRUE. 3 < = 4 .TRUE. 3 = = 4 .FALSE. ‘A’ < ‘B’ .TRUE. (in ASCII, A 65, B 66) 7+3 < 2+11 .TRUE.

  13. Combinational Logic Operators l1 .op. l2 and .NOT. l1(.NOT. is a unary operator) l1, l2: logical expressions, variables, or constants. op: the binary operators. (see Table below) operation meaning .AND. logical AND .OR. logical OR .EQV. logical equivalence .NEQV. logical non-equivalence .NOT. logical NOT

  14. Example L1 = .TRUE., L2 = .TRUE., L3 = .FALSE. (a) .NOT. L1 .FALSE. (b) L1 .OR. L3 .TRUE. (c) L1 .AND. L3 .FALSE. (d) L2 .NEQV. L3 .TRUE. (e) L1 .AND. L2 .OR. L3 .TRUE. (f) L1 .OR. L2 .AND. L3 .TRUE. (g) .NOT. (L1 .EQV. L2) .FALSE.

  15. The Block IF Construct This construct specifies that a block of code will be executed if and only if a certain logical expression is true. IF (logical_expr) THEN Statement 1 Statement 2 . . . END IF a block

  16. Example: ax2+ bx + c = 0, -b ± ( b2 – 4ac )1/2 x = 2a b2 – 4ac > 0 two distinct real roots If b2 – 4ac = 0 a single repeated root b2 – 4ac < 0 two complex roots

  17. Fortran: IF ( (b**2 – 4.*a*c) < 0. ) THEN WRITE(*,*) ‘Two complex roots!’ END IF The ELSE and ELSE IF Clauses For many different options to consider, IF + ELSE IF (one or more)+ an ELSE

  18. IF (logical_expr_1) THEN Statement 1 Statement 2 . . ELSE IF (logical_expr_2) THEN Statement 1 Statement 2 . . ELSE Statement 1 Statement 2 . . END IF Block 1 Block 2 Block 3

  19. Fortran: IF ( (b**2 – 4.*a*c) < 0. ) THEN WRITE(*,*) ‘two complex roots’ ELSE IF ( (b**2 – 4.*a*c) == 0. ) THEN WRITE(*,*) ‘two identical real roots’ ELSE WRITE(*,*) ‘two distinct real roots’ END IF (Try it out!) Write a complete Fortran program for a quadratic equationax2 + bx + c = 0. Input: a, b, c (e.g., 1., 5., 6. or 1., 4., 4. or 1., 2., 5.) Output: ‘distinct real’ or ‘identical real’ or ‘complex roots’

  20. Examples Using Block IF Constructs Example The Quadratic Equation:(ax2 + bx + c =0) Write a program to solve for the roots of a quadratic equation, regardless of type. Input: a, b, c real Output: roots repeated real complex

  21. Control Constructs: Loops • while loops • iterative (or counting) loops The While Loop DO . . . IF (logical_expr) EXIT . . . END DO a code block

  22. Evaluation a Function of Two Variables: x + y, x≧0 and y ≧ 0 x + y2, x≧0 and y < 0 x2 + y, x < 0 and y ≧ 0 x2 + y2, x < 0 and y < 0 f(x,y)= Input: x, y Output: f

  23. Test: (Try it out!) x y f 2. 3. 5. 2. -3. 11. -2. 3. 7. -2. -3. 13.

  24. Named Block IF Constructs [name:] IF (logical_expr_1) THEN Statement 1 Statement 2 . . ELSE IF (logical_expr_2) THEN [name] Statement 1 Statement 2 . . ELSE [name] Statement 1 Statement 2 . . END IF [name] Block 1 optional Block 2 optional Block 3

  25. Notes Concerning the Use of Logical IF Constructs Nested IF Constructs: outer: IF ( x > 0. ) THEN . . inner: IF ( y < 0. ) THEN . . END IF inner . . END IF outer

  26. The Logical IF Statement IF (logical_expr) Statement e.g., IF ( (x >= 0.) .AND. (y >= 0.) ) f = x + y

  27. The Iterative or Counting Loop DO index = istart, iend, incr Statement 1 . . . Statement n END DO e.g., (2) (1) Do i = 1, 10 Statement 1 . . . Statement n END DO Do i = 1, 10, 2 Statement 1 . . . Statement n END DO ( i = 1, 3, 5, 7, 9 ) ( incr = 1 by default)

  28. Example The Factorial Function: N ! = N × (N-1) × (N-2) … × 3 × 2 × 1, N > 0. 0 ! = 1 e.g., 4 ! = 4 × 3 × 2 × 1 = 24 5 ! = 5 ×4 × 3 × 2 × 1 = 120 Fortran Code: n_factorial = 1 DO i = 1, n n_factorial = n_factorial * i END DO

  29. Problem: Write a complete Fortran program for the factorial function. N ! = N × (N-1) × (N-2) … × 3 × 2 × 1, N > 0. 0 ! = 1 Input: n ( n > = 0 ) Output: n!

  30. The CYCLE and EXIT Statements

  31. Named Loops While loop: [name:] DO . . . IF (logical_expr) CYCLE [name] . . . IF (logical_expr) EXIT [name] . . . END DO [name] optional

  32. Counting loop: [name:] DO index = istart, iend, incr . . . IF (logical_expr) CYCLE [name] . . . END DO [name] optional

  33. Nesting Loops and Block IF Construct

  34. Nesting loops within IF constructs and vice versa: e.g., outer:IF ( a < b ) THEN . . . inner: DO i = 1, 3 . . . ELSE . . . END DO inner END IF outer illegal!

  35. legal: outer:IF ( a < b ) THEN . . . inner: DO i = 1, 3 . . . END DO inner . . . ELSE . . . END IF outer

  36. Example Statiscal Analysis: Average: x_ave = N Σxi i=1 N Standard deviation: N N Σxi )2 1/2 N Σxi2 – ( i=1 i=1 S = N (N-1) Input:x (i.e., xi , i = 1, 2, …, N) ≧ 0 Output: x_ave and S

  37. Character Assignments and Character Manipulations • Character operators: • substring specifications • concatenation Character Assignment character variables name = character expression

  38. Substring Specifications E.g., str1 = ‘123456’ str1(2:4) contains the string ‘234’. PROGRAM substring CHARACTER (len=8) :: a,b,c a = ‘ABCDEFGHIJ’ b = ‘12345678’ c = a(5:7) b(7:8) = a(2:6) WRITE(*,*) 'a=', a WRITE(*,*) 'b=', b WRITE(*,*) 'c=', c END PROGRAM a = ? b = ? c = ? (Try it out!)

  39. Solu: a = ‘ABCDEFGH’ (∵ len = 8) ∵ b(7:8) = a(2:6) = ‘BC’ b = ‘123456BC’ c = a(5:7) = ‘EFG’ = ‘EFG□□□□□‘ (∵ len = 8) (Cont.)

  40. The Concatenation Operator E.g., PROGRAM concate CHARACTER (len=10) :: a CHARACTER (len=8) :: b,c a = ‘ABCDEFGHIJ’ b = ‘12345678’ c = a(1:3) // b(4:5) // a(6:8) WRITE(*,*)’c=‘,c END PROGRAM c = ? (Try it out: c =‘ABC45FGH’)

More Related