Midterm Review Programming in Fortran

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Introduction to arrays
One Dimensional Arrays
Chapter 3 Program Design And Branching Structures.
1 Chapter 2 Basic Elements of Fortran Programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
ITEC113 Algorithms and Programming Techniques
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 8 Arrays and Strings
21 April, 2000 CS1001 Lecture 27 Final Examination Review.
Fundamentals of Python: From First Programs Through Data Structures
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a)
Fundamentals of Python: First Programs
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
IE 212: Computational Methods for Industrial Engineering
Chapter 8 Arrays and Strings
Scientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh Consulting Services Group.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007.
INPUT / OUTPUT STATEMENTS
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Information and Computer Sciences University of Hawaii, Manoa
Definition of the Programming Language CPRL
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Chapter 2 - Introduction to C Programming
The Selection Structure
Chapter 2 - Introduction to C Programming
MATLAB DENC 2533 ECADD LAB 9.
Introduction to MATLAB
Subroutine Comp 208 Yi Lin.
Arithmetic operations, decisions and looping
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
7 Arrays.
CS1100 Computational Engineering
Chapter 2 - Introduction to C Programming
PHP.
Chapter 2 - Introduction to C Programming
Spreadsheets Objective 6.02
Chapter 2 - Introduction to C Programming
Matlab Basics.
Spreadsheets Objective 6.02
Programming Languages and Paradigms
DATA TYPES AND OPERATIONS
Introduction to C Programming
REPETITION Why Repetition?
Presentation transcript:

Midterm Review Programming in Fortran Yi Lin Oct 12, 2006 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering What we have learned Units of Fortran programs Variable Expression Statement Control statements (IF-ELSE) IF-THEN-ELSE-ENDIF IF-THEN-ELSEIF-THEN-ELSE-ENDIF Repetition statements (DO LOOP) Count DO LOOP INFINITE DO LOOP Function and Subroutine 11/24/2018 Comp208 Computers in Engineering

Units of Fortran programs Smallest: Variables and constants Constants: the values are the same E.g., “hello”, 34, Variables: is a unique name which a FORTRAN program applies to a word of memory and uses to refer to it. Its values can be reassigned. E.g. a, b, Variable types INTEGER REAL LOGICAL .TRUE. .FALSE. CHARACTER 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Expression Composed of variables, constants and operators. For example: 3 + ½ 3 > 2 .AND. 4 >=3 “Prof. “ // ”Friedman” An expression has a value which is of a specific type (e.g., INTEGER, REAL, LOGICAL, CHARACTER) 3+1/2 has a value of 3.5 3 > 2 .AND. 4 >=3 has a value of .TRUE. “Prof. “ // “Friedman” has a value of “Prof. Friedman” 11/24/2018 Comp208 Computers in Engineering

Arithmetic Expressions An arithmetic expression is formed using the operations: + (addition), - (subtraction) * (multiplication), / (division) ** (exponentiation) If the operands are integers, the result will be an integer value If the operands are real, the result will be a real value If the operands are of different types, the expression is called mixed mode. 11/24/2018 Comp208 Computers in Engineering

Evaluate Simple Expressions 1 + 3 --> 4 1.23 - 0.45 --> 0.78 6.5/1.25 --> 5.2 8.4/4.2 --> 2.0 rather than 2, since the result must be of REAL type. -5**2 --> -25 12/4 --> 3 13/4 --> 3 rather than 3.25. Since the operands are of INTEGER type, so is the result. The computer will truncate the mathematical result to make it an integer. 3/5 --> 0 rather than 0.6. 11/24/2018 Comp208 Computers in Engineering

Evaluate Complex Expression Arithmetic operators: precedence 2+3*4 = ? = 5 * 4 = 20 Or = 2+12 = 14 (2+3)*4 = 20 operators Precedence () 1 ** 2 *, / 3 +, - 4 11/24/2018 Comp208 Computers in Engineering

Evaluation Complex Expression Arithmetic operators: associativity () Left to right ** Right to left *, / +, - associativity resolves the order of operations when two operators of the same precedence compete for three operands: 2**3**4 = 2**(3**4) = 2**81, 72/12/ 3 = (72/12)/3 = 6/3 = 2 30/5*3 = (30/5)*3 = 18 if *,/ associativity is from right to left 30/5*3 = 30/(5*3) = 2 11/24/2018 Comp208 Computers in Engineering

Mixed Mode Expressions If one operand of an arithmetic operator is INTEGER and the other is REAL the INTEGER value is converted to REAL the operation is performed the result is REAL 1 + 2.5  1/2.0  2.0/8  -3**2.0  1 + 5/2  4.0**(1/2)  3.5 0.5 0.25 -9.0 3 (since 5/22) 1.0 (since ½  0) 11/24/2018 Comp208 Computers in Engineering

Logical Expressions (.TRUE. .FALSE.) Relational operators lower precedence than arithmetic operator No associativity (illegal: 4 > 3 >2) <, <=, >, >=, ==, /= Logical operators lower precedence than Relational operators From left to right except .NOT. .NOT. .AND. .OR. .EQV., .NEQV. High Low 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Examples Suppose we have the declaration: INTEGER :: age=34, old=92, young=16 What is the value of the following expressions? age /= old age >= young age==56 .and. old/=92 age==56 .or. old/=92 age==56 .or. old/=92 .and. young==16 .not. age==56 .or. old/=92 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Control statements IF-THEN-ELSE-END IF Syntax: IF (logical-exp) THEN first statement block, ELSE second statement block, END IF .TRUE. .FALSE. Log Exp 1st Block 2nd block Stmt following END IF 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Control statements IF-THEN-END IF Syntax: IF (logical-exp) THEN first statement block, END IF .TRUE. .FALSE. Log Exp 1st Block Stmt following END IF 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Control statements Logical IF Syntax: IF (logical-exp) one statement .TRUE. .FALSE. Log Exp One statement Stmt following END IF 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Control statements .FALSE. IF-THEN-ELSEIF-THEN-ELSE-END IF Syntax: IF (log-exp1) THEN first statement block, ELSEIF (log-exp2)THEN second statement block, …… ELSE else block END IF Log Exp1 .FALSE. .TRUE. Log Exp2 .TRUE. 1st Block 2nd block …… Stmt following END IF 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering IF statement example Prints a season associated with a given month in terms of value. If the value of the integer month is not in the range 1-12, print “not a month!” 1, 2, 3: Winter 4, 5: Spring 6, 7, 8: Summer 9, 10: Fall 11, 12: Winter 11/24/2018 Comp208 Computers in Engineering

IF statement example (cont.) INTEGER::month READ(*,*) month !input an integer from keyboard IF (month ==4 .OR. month==5) THEN WRITE(*,*) “Spring” ELSEIF (month >=6 .AND. month <=8) THEN WRITE(*,*) “Summer” ELSEIF (month==9 .OR. Month==10) THEN WRITE(*,*) “FALL” ELSEIF (month==11 .OR. month==12 .OR. (month>=1 .AND. month<=3)) THEN WRITE(*,*) “WINTER” ELSE WRITE(*,*) “Not a month!” ENDIF 11/24/2018 Comp208 Computers in Engineering

Control statement: SELECT CASE The SELECT CASE construct provides an alternative to a series of repeated IF ... THEN ... ELSE IF statements. Syntax: SELECT CASE( expression ) CASE( value 1) block 1 ... CASE (value i) block I … [CASE DEFAULT block default] END SELECT 11/24/2018 Comp208 Computers in Engineering

SELECT CASE statement example INTEGER::month READ(*,*) month !input an integer from keyboard SELECT CASE(month) CASE (1) WRITE(*,*) “WINTER” CASE (2) CASE (3) CASE (4) WRITE(*,*) “Spring” CASE (5) CASE (6) WRITE(*,*) “Summer” CASE (7) CASE (8) CASE (9) WRITE(*,*) “FALL” CASE (10) CASE (11) CASE (12) CASE DEFAULT WRITE(*,*) “Not a month!” END SELECT 11/24/2018 Comp208 Computers in Engineering

SELECT CASE statement example Can be INTEGER, CHARACTER, LOGICAL No REAL INTEGER::month READ(*,*) month !input an integer from keyboard SELECT CASE(month) CASE (4,5) WRITE(*,*) “Spring” CASE (6:8) WRITE(*,*) “Summer” CASE (9,10) WRITE(*,*) “FALL” CASE (11, 12, 1:3) THEN WRITE(*,*) “WINTER” CASE DEFALUT WRITE(*,*) “Not a month!” END SELECT (value1, value2) (min:max) i.e., 6, 7, 8 (value1, value2, min:max) 11/24/2018 Comp208 Computers in Engineering

Repetition, DO statement Count loop uses a control clause to repeat a block of statements a predefined number of times. Note that count variable should not be modified within loop body. Syntax: DO count = start, stop [,step] block of statements END DO Infinite loop Use EXIT to get out. DO 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Count DO Loop examples Example 1: DO i=1, 10, 1 WRITE(*,*) i !write numbers 1, 2, …, 10 END DO Example 2: DO i=1, 10 ! Default step = 1 Example 3: DO i=1, 10, 2 ! i increased by 2 for each step WRITE(*,*) i !write numbers 1,3,5,7,9 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Count DO loop examples Example 4: DO j=10,2,-2 ! j decreased by 2 WRITE(*,*) j !write even numbers 10,8,6,4,2 END DO Example 5: i=1 DO WHILE(i<10) write(*,*) I i=i+1 11/24/2018 Comp208 Computers in Engineering

Infinite DO loop example INTEGER::I=0 DO IF(I>10) EXIT ! Loop terminated at I==11 WRITE(*,*) I ! WRITE number 1 to 10 I=I+1 ! I increased by 1 at each step END DO Without IF(i>10) EXIT, the program will not be able to stop. 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Array An array is a collection of individual data elements, all of the same type. E.g., The subscript (or index) of an array element is the position of that element within the array, for example: the first element is 51 and has a subscript 1, the second element is 6 and has a subscript 2. array Index: 1 2 3 4 5 6 7 8 51 34 61 75 53 element 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Declare an array Syntax type, DIMENSION(bound ) :: name ! Fortran 90 only type :: name(bound) Where, bound = [lower:]upper lower: smallest index of the elements, by default=1 upper: largest index of the elements E.g., to declare the previous array example: INTEGER, DIMENSION(8)::a INTEGER, DIMENSION(1:8)::a INTEGER::a(8) INTEGER::a(0:7) ! Then 51’s index=0, 6’s index=1, 5’s index=7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 51 34 61 75 53 11/24/2018 Comp208 Computers in Engineering

Multi-dimensional array Consider the following array This is one-dimensional array so it can only represent a vector. However, some data are more than one dimensional, e.g., matrix Syntax: TYPE, DIMENSION([1lb:][1ub], [2lb:][2ub])::name TYPE::name([1lb:][1ub], [2lb:][2ub]) 51 6 34 61 75 4 53 5 51 61 53 6 75 5 34 4 10 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Two dimensional array To declare an integer matrix with 3 rows and 4 columns They are equivalent INTEGER::a(1:3, 1:4) INTEGER::a(3,4) INTEGER, DIMENSION(1:3, 1:4)::a INTEGER, DIMENSION(3,4)::a a(i, j): to refer to an element at row i and column j, e.g., a(2, 3) j=3 i=2 a(2,3) 11/24/2018 Comp208 Computers in Engineering

Two dimensional array, example ! To set a matrix with 3 rows and 4 columns to zero PROGRAM test IMPLICIT NONE INTEGER::a(3,4), i, j DO i=1,3 DO j=1,4 a(i, j)=0 END DO END PROGRAM DO j=1,4 a(1, j)=0 END DO a(2, j)=0 a(3, j)=0 11/24/2018 Comp208 Computers in Engineering

Function and Subroutine type FUNCTION function-name (arg1, arg2, ..., argn) IMPLICIT NONE [declarations] [statements] [other subprograms] END FUNCTION function-name SUBROUTINE subroutine-name (arg1, arg2, ..., argn) END SUBROUTINE subroutine-name 11/24/2018 Comp208 Computers in Engineering

Where Do Function/Subroutine Definitions Go? External PROGRAM program-name IMPLICIT NONE INTERFACE [declaration of function/subroutine] END INTERFACE [declarations] [statements] END PROGRAM program-name [Function/subroutine definitions] INTERNAL PROGRAM program-name IMPLICIT NONE [declarations] [statements] CONTAINS [function/Subroutine definitions] END PROGRAM program-name 11/24/2018 Comp208 Computers in Engineering

Rules for Argument Association Rule 1: If an actual argument is an expression or a constant, it is evaluated and the result is saved into a temporary location. Then, the value in this temporary location is passed. INTEGER :: a = 10, b = 3, c = 37 WRITE(*,*) Minimum(18,c-a,a+b) When the function is invoked, new temporary variables we can call x, y and z are created. The value of x is initialized to 18, y to 27 and z to 13. The function returns 13. 11/24/2018 Comp208 Computers in Engineering

Rules for Argument Association Rule 2: If an actual argument is a variable, the corresponding formal argument is made to refer to the same memory cell. INTEGER :: a = 10, b = 3, c = 37 WRITE(*,*) Minimum(a,b,c) When the function is invoked, there are no new variables created. The parameter x refers to a, y to b and z to c. We say x is an alias for a. There are two names for the same memory cell. The function returns 3. 11/24/2018 Comp208 Computers in Engineering

Argument passing example REAL::x=1, y=2 WRITE(*,*) "x=", x, “y=", y ! X=1.0 y=2.0 CALL swap(x,y) 1.0 a x 2.0 b y SUBROUTINE swap( a, b ) REAL, INTENT(INOUT):: a, b REAL:: temp temp = a a = b b = temp END SUBROUTINE swap 1.0 temp 2.0 a x 1.0 b y WRITE(*,*) "x=", x, “y=", y ! x=2.0 y=1.0 11/24/2018 Comp208 Computers in Engineering

Example passing array as argument ! Input a list of real number and calculate their sum. PROGRAM Test IMPLICIT NONE INTEGER, PARAMETER :: MAX_SIZE = 1000 INTEGER, DIMENSION(1:MAX_SIZE) :: Data INTEGER :: ActualSize INTEGER :: i READ(*,*) ActualSize READ(*,*) (Data(i), i=1, ActualSize) WRITE(*,*) "Sum = ", Sum(Data, ActualSize) CONTAINS INTEGER FUNCTION Sum(x, n) INTEGER, INTENT(IN):: n INTEGER, DIMENSION(n), INTENT(IN) :: x INTEGER :: Total Total = 0.0 DO i = 1, n Total = Total + x(i) END DO Sum = Total END FUNCTION Sum END PROGRAM Test 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering Implied DO Loops The implied DO loop can simplify this greatly. INTEGER :: data(100) INTEGER :: n, i READ(*,*) n READ(*,*) (data(i), i=1, n) If the value of n is 15, this READ(*,*) statement is equivalent to READ(*,*) data(1), data(2),. . ., data(15) What is the difference? The values read can appear on one or more lines since FORTRAN will automatically search for the next input on the current input line or go on to the next line if needed. 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering FORMAT statement, F Example REAL::x=1.0, y=1100.1003 write(*, 900) x, y 900 format (F3.1, F9.4) (F3.1,F9.4): 1.01100.1003 (F3.1,F10.4): 1.0#1100.1003 (F3.1,F8.4): 1.0******** *: Width=8 is not wide enough to output y. 4 integer digits + 4 decimal digits + 1 for “.” = 9 digits 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering FORMAT statement, I For integers only the field width is specified, so the syntax is Iw. Similarly, character strings can be specified as Aw but the field width is often dropped. INTEGER::a=1000 WRITE(*,100) “a=“, a 100 FORMAT(A5,I6) WRITE(*,200) “a=“,a FORMAT(A,I4) WRITE(*,300) “a=“,a 300 FORMAT(A,I3) ###a=##1000 A5 I6 a=1000 A I4 a=*** A I3 11/24/2018 Comp208 Computers in Engineering

Comp208 Computers in Engineering FORMAT statement, READ Example INTEGER::a,b READ(*,100) a,b FORMAT(2I3) ! eqv. To FORMAT(I3,I3) Correct inputs for (2I3), e.g., “##1##2” a=##1=1, b=##2=2 “1##2##” a=1##=1, b=2##=2 “#1##2#” a=#1#=1, b=#2#=2 11/24/2018 Comp208 Computers in Engineering

FILE input/output, Example ! Input 10 integers from keyboard and write them to file “inputData.txt” PROGRAM fileTest IMPLICIT NONE INTEGER::count, a OPEN(UNIT=10,FILE=“inputData.txt”) ! Open file “inputData.txt” DO count=1,10 WRITE(*,*) “Input an integer number from keyboard:” READ(*,*) a READ(10,100) “a=“, a ! Write to “inputData.txt” END DO CLOSE(10); ! Close file “inputData.txt” FORMAT(A2, I8) END PROGRAM a=######51 a=#######6 … inputData.txt 11/24/2018 Comp208 Computers in Engineering