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

Slides:



Advertisements
Similar presentations
Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Advertisements

Introduction to arrays
Chapter 3 Program Design And Branching Structures.
Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once. Type of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Program Design and Development
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Introduction to FORTRAN-90 University of Liverpool course.
High-Level Programming Languages: C++
Fortran 1- Basics Chapters 1-2 in your Fortran book.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CPS120: Introduction to Computer Science Decision Making in Programs.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
CPS120: Introduction to Computer Science Decision Making in Programs.
Introduction to Programming with RAPTOR
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Matlab Programming for Engineers
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Program design Program Design Process has 2 phases:
Chapter 4 – C Program Control
Programming For Nuclear Engineers Lecture 6 Arrays
Algorithms and Flowcharts
Arithmetic operations, decisions and looping
Chapter 8 JavaScript: Control Statements, Part 2
Midterm Review Programming in Fortran
CS1100 Computational Engineering
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
REPETITION Why Repetition?
Presentation transcript:

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

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.

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)

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.

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)

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

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)

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

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

Example (extra) Write a program for converting a 4 bits integer into a base 10 number, e.g., = 1 x x x x 2 0 = 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)

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.

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

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.

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

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

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

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

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

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

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

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

Test: (Try it out!) x y f

[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

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

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

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 )

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

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!

The CYCLE and EXIT Statements

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

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

Nesting Loops and Block IF Construct

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!

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

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

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

Substring Specifications E.g., str1 = ‘123456’ str1(2:4) contains the string ‘234’. PROGRAM substring CHARACTER (len=8) :: a,b,c a = ‘ABCDEFGHIJ’ b = ‘ ’ 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!)

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

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