Download presentation
Presentation is loading. Please wait.
Published byGriffin Robbins Modified over 9 years ago
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 unit format The list-directed output statement: WRITE (*,*) output_list I/O unit format
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-time error! (depends on machines)
8
+ IMPLICIT NONE, PROGRAM test_1 IMPLICIT NONE REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘, tmie END PROGRAM Output: Compile-time error! (depends on machines)
9
Program Examples Example (Temperature conversion) T ( 0 F) = (9/5) T( 0 C) + 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 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 = 11
11
Assignment Statements and Logical Calculations Assignment statements: logical variable name = logical expression Logical operators: relational operators combinational operators Relational Operators a 1 op a 2 a 1, a 2 : 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 l 1.op. l 2 and.NOT. l 1 (.NOT. is a unary operator) l 1, l 2 : 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. L 1.FALSE. (b) L 1.OR. L 3.TRUE. (c) L 1.AND. L 3.FALSE. (d) L 2.NEQV. L 3.TRUE. (e) L 1.AND. L 2.OR. L 3.TRUE. (f) L 1.OR. L 2.AND. L 3.TRUE. (g).NOT. (L 1.EQV. L 2 ).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: ax 2 + bx + c = 0, x = -b ± ( b 2 – 4ac ) 1/2 2a Ifb 2 – 4ac = 0 b 2 – 4ac > 0 b 2 – 4ac < 0 two distinct real roots two complex roots a single repeated root
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 Write a complete Fortran program for a quadratic equation ax 2 + 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’ (Try it out!)
20
Examples Using Block IF Constructs Example The Quadratic Equation: (ax 2 + bx + c =0) Write a program to solve for the roots of a quadratic equation, regardless of type. Input: a, b, c Output: roots real repeated real complex
21
while loops iterative (or counting) loops The While Loop DO... IF (logical_expr) EXIT... END DO a code block Control Constructs: Loops
22
Evaluation a Function of Two Variables: f(x,y) = x + y, x ≧ 0 and y ≧ 0 x + y 2, x ≧ 0 and y < 0 x 2 + y, x < 0 and y ≧ 0 x 2 + y 2, x < 0 and y < 0 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
[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 Block 2 Block 3 Named Block IF Constructs optional
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., (1) Do i = 1, 10 Statement 1... Statement n END DO ( incr = 1 by default) (2) Do i = 1, 10, 2 Statement 1... Statement n END DO ( i = 1, 3, 5, 7, 9 )
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. Input: n ( n > = 0 ) N ! = N × (N-1) × (N-2) … × 3 × 2 × 1, N > 0. 0 ! = 1 Output: n!
30
The CYCLE and EXIT Statements
31
[name:] DO... IF (logical_expr) CYCLE [name]... IF (logical_expr) EXIT [name]... END DO [name] Named Loops While loop: optional
32
[name:] DO index = istart, iend, incr... IF (logical_expr) CYCLE [name]... END DO [name] Counting loop: 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
outer: IF ( a < b ) THEN... inner: DO i = 1, 3... END DO inner... ELSE... END IF outer legal:
36
Example Statiscal Analysis: Average: x_ave = ΣxiΣxi i=1 N N Standard deviation: S = N Σ x i 2 – ( i=1 N N Σ x i ) 2 N (N-1) 1/2 Input: x (i.e., x i, i = 1, 2, …, N) ≧ 0 Output: x_ave and S
37
Character Assignments and Character Manipulations Character Assignment character variables name = character expression Character operators: 1.substring specifications 2.concatenation
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’)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.