introduction to computer sciences references: [1] fortran 95/2003 for scientists and engineers (3e)...

40
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

Upload: griffin-robbins

Post on 19-Jan-2016

236 views

Category:

Documents


3 download

TRANSCRIPT

Page 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

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

Page 2: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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.

Page 3: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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)

Page 4: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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.

Page 5: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

(DeclarationSection)

(ExecutionSection)

(Termination section)

Page 6: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 7: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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)

Page 8: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

+ IMPLICIT NONE,

PROGRAM test_1 IMPLICIT NONE REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘, tmie END PROGRAM

Output:

Compile-time error! (depends on machines)

Page 9: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Program Examples

Example (Temperature conversion)

T (0F) = (9/5) T(0C) + 32

Page 10: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 11: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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)

Page 12: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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.

Page 13: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 14: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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.

Page 15: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 16: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Example:

ax2 + bx + c = 0,

x = -b ± ( b2 – 4ac )1/2

2a

If b2 – 4ac = 0

b2 – 4ac > 0

b2 – 4ac < 0

two distinct real roots

two complex roots

a single repeated root

Page 17: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 18: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 19: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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 quadraticequation ax2 + 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!)

Page 20: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Output: rootsrealrepeated realcomplex

Page 21: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

• while loops• iterative (or counting) loops

The While Loop

DO . . . IF (logical_expr) EXIT . . .END DO

a code block

Control Constructs: Loops

Page 22: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Evaluation a Function of Two Variables:

f(x,y) =

x + y, x 0 ≧ and y 0≧x + y2, x 0 ≧ and y < 0x2 + y, x < 0 and y 0≧x2 + y2, x < 0 and y < 0

Input: x, y

Output: f

Page 23: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Test: (Try it out!)

x y f

2. 3. 5. 2. -3. 11.-2. 3. 7.-2. -3. 13.

Page 24: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

[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

optional

Page 25: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Page 26: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

The Logical IF Statement

IF (logical_expr) Statement

e.g., IF ( (x >= 0.) .AND. (y >= 0.) ) f = x + y

Page 27: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

The Iterative or Counting Loop

DO index = istart, iend, incr Statement 1 . . . Statement nEND DO

e.g.,

(1) Do i = 1, 10 Statement 1 . . . Statement nEND DO

( incr = 1 by default)

(2) Do i = 1, 10, 2 Statement 1 . . . Statement nEND DO

( i = 1, 3, 5, 7, 9 )

Page 28: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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 = 1DO i = 1, n n_factorial = n_factorial * iEND DO

Page 29: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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!

Page 30: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

The CYCLE and EXIT Statements

Page 31: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

[name:] DO . . . IF (logical_expr) CYCLE [name] . . . IF (logical_expr) EXIT [name] . . . END DO [name]

Named Loops

While loop:

optional

Page 32: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

Counting loop:

optional

Page 33: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Nesting Loops and Block IF Construct

Page 34: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Nesting loops within IF constructs and vice versa:

e.g.,outer: IF ( a < b ) THEN . . . inner: DO i = 1, 3 . . . ELSE . . . END DO innerEND IF outer

illegal!

Page 35: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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

legal:

Page 36: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Example Statiscal Analysis:

Average: x_ave =

Σxii=1

N

N

Standard deviation:

S = N Σxi

2 – (i=1 i=1

N N

Σxi )2

N (N-1)

1/2

Input: x (i.e., xi , i = 1, 2, …, N) 0 ≧

Output: x_ave and S

Page 37: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Character Assignments and Character Manipulations

Character Assignment

character variables name = character expression

Character operators:1. substring specifications2. concatenation

Page 38: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Substring SpecificationsE.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!)

Page 39: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

Solu: a = ‘ABCDEFGH’ ( len = 8)∵

∵ b(7:8) = a(2:6) = ‘BC’

b = ‘123456BC’

c = a(5:7) = ‘EFG’ = ‘EFG□□□□□‘ ( len = 8)∵

(Cont.)

Page 40: Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester

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’)