Introduction to FORTRAN

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Introduction to arrays
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Fortran Jordan Martin Steven Devine. Background Developed by IBM in the 1950s Designed for use in scientific and engineering fields Originally written.
PL/SQL.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.
Introduction to Fortran Fortran Evolution Drawbacks of FORTRAN 77 Fortran 90 New features Advantages of Additions.
Chapter 2 Basic Elements of Fortan
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
1 CS 415: Programming Languages Fortran Aaron Bloomfield Fall 2005.
MIPS Instruction Set Advantages
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1.
Introduction to FORTRAN-90 University of Liverpool course.
Fortran: Specification Statements Session Six ICoCSIS.
5.3 Machine-Independent Compiler Features
Introduction to FORTRAN
April 14, ICE 1341 – Programming Languages (Lecture #13) In-Young Ko Programming Languages (ICE 1341) Lecture #13 Programming Languages (ICE 1341)
Simple Procedural Languages: FORTRAN and C Lecture 9: Dolores Zage.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
Class Review. Basic Unix Commands list files in a directory: ls list files in a directory: ls remove files: rm remove files: rm rename files: mv rename.
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
1 Serial Run-time Error Detection and the Fortran Standard Glenn Luecke Professor of Mathematics, and Director, High Performance Computing Group Iowa State.
sequence of execution of high-level statements
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
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 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
Fortran MATTHEW CARSON. History of FORTRAN FORTRAN ; First High Level Language FORTRAN I 1957; First Compiled Language FORTRAN II ; Independent.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Introduction to Computer Programming using Fortran 77.
The Evolution of Programming Languages Day 1 Lecturer: Xiao Jia The Evolution of PLs1.
Computer Programming in C++ 黃鐘揚 教授 Prof. Chung-Yang (Ric) Huang Department of Electrical Engineering National Taiwan University 2007/06/26.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Unsur-unsur Algoritma
7 - Programming 7J, K, L, M, N, O – Handling Data.
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Fortran Tutorial Fortran spaces for code qqqqqqCODE
MIPS Instruction Set Advantages
Algol 60 John Cowan N Languages in N Months Meetup June 7, 2016
Decisions Chapter 4.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Chapter 5: Arrays: Lists and Tables
Data Structures and Abstract Data Types
MATLAB DENC 2533 ECADD LAB 9.
Midterm Review Programming in Fortran
Introduction to Fortran 90/95
Lecture 4 : January 2001 Dr. Andrew Paul Myers
Outline Defining and using Pointers Operations on pointers
Multidimensional Arrays
mdul-cntns-sub-cmmnts2.f90
Outline Announcements Differences between FORTRAN and C
Basic Programming Concepts
Chapter 6 Programming the basic computer
Computer Architecture
REPETITION Why Repetition?
Control Structures.
Some Important Programming Languages
Presentation transcript:

Introduction to FORTRAN Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University

Reference 彭國倫, Fortran 95 程式設計, 碁峰, 2001.

Outline History of FORTRAN Basics Declaration Program Flow Control Arrays Functions Pointers Modules

History of FORTRAN FORmula TRANslator First FORTRAN compiler John Backus, IBM, 1957 1st high level programming language Most popular in 1960’s FORTRAN 66 1st ANSI standard FORTRAN FORTRAN 77, 1978 FORTRAN 90, 1992 Pointers and OO FORTRAN 95, 1997 Support for parallel computation

Fixed Format (*.f, *.for) C FIXED FORMATDEMO program main write(*,*) ‘Hello’ write(*,*) 1’Hello’ 100 write(*,*) ‘Hello’ 10 Stop end

Free Format (*.f90) ! Free Format program main write(*,*) “Hello” ! This is also a comment write(*,*) & “Hello” wri& &te(*,*) “Hello” end

Basic Data Types and Arithmetic Operators INTEGER REAL COMPLEX CHARACTER LOGICAL Arithmetic operators +, -, *, /, **, ()

Demo Program program example integer a real b real*8 c complex d character(len=10) s logical t a = 2 + 2*4 – 3 b = 2.5 + 3.0/2.0 c = 0.1 write(*,*) a, “ “, b, “ “, c d = (1.0, 2.0) ! d = 1.0 + 2.0 i write(*,*) d s = “Hello” write(*,*) s t = .true. write(*,*) t stop end

Read Data and Formatted Output program readWriteDemo integer a, b, c real d complex e read(*,*) a, b, c write(*,100) a + b + c 100 format(I4) d = 12.3 write(*, “(1X, F5.2)”) d e = (1, 2) write(*, 200) e 200 format(1X, F4.1, F4.1) end

implicit and parameter Statements program demo implicit none integer :: i = 123 + 321 real pi write(*,”(‘123 + 321 =‘, I4)”) i parameter (pi = 3.141592653) write(*,”(F4.2)”) sin(pi/6) end

DATA Statement program demo implicit none integer a real b complex c character* (20) str data a, b, c, str /1, 2.0, (1, 2), ‘FORTRAN 77’/ write(*,*) a, b, c, str stop end

type Statement (FORTRAN 90) program typeDemo implicit none type::person character(len=30) :: name integer :: age end type person type(person) :: a; write(*,*) “Name:” read(*,*) a%name write(*,*) “Age:” read(*,*) a%age write(*,100) a%name, a%age 100 format(/,”Name: “, A10/,”Age:”,I3) stop end

if Statement and Logical Operations (FORTRAN 77) PROGRAM Demo IMPLICIT NONE REAL speed LOGICAL hasBarrier WRITE(*,*) “speed:” READ(*,*) speed hasBarrier = .false. IF ( speed .GT. 100 .OR. hasBarrier ) THEN WRITE(*,*) “Slow down” ELSE WRITE(*,*) “Keep going” END IF STOP END

if Statement and Logical Operations (FORTRAN 90) program Demo implicit none integer rain, windSpeed write(*,*) “Rain:” read(*,*) rain write(*,*) “Wind:” read(*,*) windSpeed if( rain >= 500 .or. windSpeed >= 10 ) then write(*,*) “Take one day off” else write(*,*) “Go to office” endif stop end

Multiple if-else if Statement program demo implicit none integer score character grade write(*,*) “Score:” read(*,*) score if ( score > 100 ) then grade = ‘?’ else if ( score >= 90 ) then grade = ‘A’ else if ( score >= 80 ) then grade = ‘B’ else end if write(*,”(‘Grade:’, A1)”) grade stop end

Nested if Statement program demo implicit none integer score character grade write(*,*) “Score:” read(*,*) score if ( score > 100 ) then grade = ‘?’ else if ( score >= 90 ) then grade = ‘A’ grade = ‘B’ end if write(*,”(‘Grade:’, A1)”) grade stop end

select case Statement program demo implicit none integer score character grade write(*,*) “Score:” read(*,*) score select case(score) case(90:100) grade = ‘A’ case default grade = ‘B’ end select write(*,”(‘Grade:’,A1)”) grade stop end

goto Statement program NOT_RECOMMENDED implicit none real height real weight write(*,*) “height:” read(*,*) height write(*,*) “weight:” read(*,*) weight if( weight > height – 100 ) goto 200 100 write(*,*) “Under control” goto 300 200 write(*,*) “Too fat!” 300 stop end

Arithmetic goto program NOT_RECOMMENDED implicit none real a, b real c data a, b /2.0, 1.0/ c = a - b if ( c ) 10, 20, 30 10 write(*,*) ‘a < b’ goto 40 20 write(*,*) ‘a = b’ 30 write(*,*) ‘a > b’ 40 Stop end

do Statement (.f90) program demo implicit none integer counter integer parameter :: lines = 10 do counter=1, lines, 1 write(*,*) “Happy New Year”, counter end do stop end

do Statement (*.for) PROGRAM demo IMPLICIT NONE INTEGER limit PARAMETER (limit=10) INTEGER counter INTEGER ans DATA ans /0/ DO 100, counter = 2, limit, 2 ans = ans + counter 100 CONTINUE WRITE(*,*) ans STOP END

do while Statement (*.f90) program demo implicit none integer, parameter :: limit = 10 integer counter integer :: ans = 0 counter = 2 do while ( counter <= limit ) ans = ans + counter counter = counter + 2 end do write(*,*) ans stop end

cycle Statement (*.f90) program elevator implicit none integer :: dest = 9 integer floor do floor = 1, dest if ( floor == 4 ) cycle write(*,*) floor end do stop end

exit Statement (*.f90) program WeightGuess implicit none real, parameter :: weight = 45.0 real, parameter :: error = 0.0001 real :: guess do while ( .true. ) write(*,*) “Weight:” read(*,*) guess if ( abs(guess – weight) < error ) exit end do write(*,*) “Correct!” stop end

One-Dimensional Array program Demo implicit none integer, parameter :: students = 5 integer student( students ) integer i do i = 1, students write(*,”(‘Number ‘, I2)”) i read(*,*) student(i) end do do while ( .true. ) write(*,*) “Query” read(*,*) i if( i <= 0 .or. i>students ) exit write(*,*) student(i) stop end

Two-Dimensional Array (1/2) program MatrixAddition implicit none integer, parameter :: row = 2 integer, parameter :: col = 2 integer matrixA(row, col) integer matrixB(row, col) integer matrixC(row, col) integer r integer c write(*,*) “Matrix A” do r = 1, row do c = 1, col write(*,”(‘A(‘,I1,’,’,I1,’)=‘)”) r, c read(*,*) matrixA(r,c) end do

Two-Dimensional Array (1/2) write(*,*) “Matrix B” do r = 1, row do c = 1, col write(*,”(‘B(‘,I1,’,’,I1,’)=‘)”) r, c read(*,*) matrixB(r,c) end do write(*,*) “Matrix A + B = “ matrixC(r,c) = MatrixB(r,c) + MatrixA(r,c) write(*,”(‘C(‘,I1,’,’,I1,’)=‘,I3)”) r, c, & matrixC(r,c) stop end

Declaration of Arrays integer a(5) integer a(0:5) integer a(-3:3) integer b(2:3, -1:3)

Setting Array Data integer a(5) data a /1, 2, 3, 4, 5/ integer i data (a(i), i=2,4) /2, 3, 4/ integer a(2,2) integer I, j data ( (a(I,j), i=1,2), j=1,2) /1, 2, 3, 4/

Column Major Storage A(1,1) A(3,1) A(2,2) A(3,2) A(1,3) A(2,3) A(3,3)

Adjustable Array (*.f90) program Demo implicit none integer students integer, allocatable :: a( integer I write(*,*) “How many students:” read(*,*) students allocate( a(students) ) do I = 1, students write(*, “(‘Number ‘, I3)”) I read(*,*) a(i) end do deallocate( a ) stop end

Subroutines program example implicit none call message() stop end subroutine message() write(*,*) “Hello.” return

Pass by Reference program example implicit none integer :: a = 1 integer :: b = 2 write(*,*) a, b call add(a) call add(b) stop end subroutine add(num) integer :: num num = num + 1 return

Functions program example implicit none real :: a = 1 real :: b = 2 real, external :: add write(*,*) add(a,b) stop end function add(a, b) real :: a, b real :: add add = a + b return

common Statement program example implicit none integer :: a, b common a, b a = 1 b = 2 call showCommon() stop end subroutine showCommon() integer :: num1, num2 common num1, num2 write(*,*) num1, num2 return

Labeled common Blocks program example implicit none integer :: a, b common /group1/ a common /group2/ b a = 1 b = 2 call showGroup1() call showGroup2() stop end subroutine showGroup1() integer :: num common /group1/ num1 write(*,*) num1 return

Block Data program example implicit none integer :: a, b common a, b integer :: c, d common /group1/ c, d write(*,*) a, b, c, d stop end block data integer a, b data a, b /1, 2/ integer c, d common c, d data c, d /3, 4/ end block data

Recursive Functions (*.f90) program example implicit none integer n integer, external :: fact write(*,*) ‘N =‘ read(*,*) n write(*,”(I2,’! = ‘,I8)” ) n, fact(n) stop end recursive integer function fact(n) result(ans) integer, intent(in) :: n if( n <= 1 ) then ans = 1 return end if ans = n * fact(n-1)

Pointers I program example implicit none integer, target :: a = 1 integer, pointer :: p p=>a write(*,*) p a = 2 p = 3 write(*,*) a stop end

Pointers II program example implicit none integer, target :: a = 1 integer, pointer :: p allocate(p) p = 1 write(*,*) p deallocate(p) p=>a stop end

Pointers III program example implicit none integer, pointer : a(:) allocate( a(5) ) a = (/ 1, 2, 3, 4, 5 /) write(*,*) a deallocate( a ) stop end

Modules (1/4) module constant implicit none real, parameter :: pi = 3.14159 real, parameter :: g = 9.81 end module module typedef type player real :: angle real :: speed real :: distance end type

Modules (2/4) module shoot use constant use typedef implicit none contains real function AngleToRad( angle ) real angle AngleToRad = angle * pi/180.0 return end subroutine GetDistance( person ) type(player) :: person real rad, Vx, time

Modules (3/4) rad = AngleToRad( person%angle ) Vx = person%speed * cos( rad ) time = 2.0 * person%speed * sin( rad )/g person % distance = Vx * time return end subroutine end module

Modules (4/4) program example use shoot implicit none integer, parameter :: players = 2 type(player) :: people(players) = (/ & player(30.0, 25.0, 0.0 ), & player(35.0, 21.0, 0.0 ) /) integer :: i do i = 1, players call GetDistance( people(i) ) write(*,”(‘Player ‘,I1,’ =‘,F8.2)”) i, & people(i)%distance end do stop end