220 likes | 302 Views
Programming Languages (ICE 1341) Lecture #13 April 14, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU). Announcements. The solution of the midterm exam is now available on the class homepage. FORTRAN ( For mula Tran slating System).
E N D
Programming Languages(ICE 1341)Lecture #13April 14, 2004In-Young Koiko .AT. icu.ac.krInformation and Communications University (ICU) ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Announcements • The solution of the midterm exam is now available on the class homepage ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN (Formula Translating System) • Designed to efficiently translate mathematical formulas into IBM 704 machine code IBM 704 (1954) “The first mass-produced computer with core memory and floating-point arithmetic” Photo: Lawrence Livermore National Laboratory ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Statement Number (1-5) FORTRAN Statement (7-72) Continuation (6) Identification Sequence (73-80) FORTRAN Coding Format FORTRAN programs were written in a coding form with a strict formatting rule J.W. Perry Cole, ANSI FORTRAN IV, wcb ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Punched Cards Photos: http://www.tno.nl/instit/fel/museum/computer/en/punchcards.html Coded FORTRAN programs were converted onto punched cards for loading ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Early FORTRAN Versions • FORTRAN I (1957) • Names could have up to six characters (IBM 704 has a 6-bit BCD character set and 36-bit word) • Post-test counting loop (DO), Formatted I/O, User-defined subprograms, Three-way selection statement (arithmetic IF) • No data typing statements (Implicit Type Declaration – Variables whose names begin with I, J, K, L, M, and N are integer types, all others are floating point) • FORTRAN II (1958) • Independent compilation of subroutines * AW Lecture Notes ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Later FORTRAN Versions • FORTRAN IV (1960-62) – ANSIstandard in 1966 • Explicit type declarations • Logical selection statement • Subprogram names could be parameters • FORTRAN 77 (1978) • Character string handling • Logical loop control statement • IF-THEN-ELSE statement • FORTRAN 90 (1990) • Free coding format • Modules, Dynamic arrays, Pointers, Recursion, CASE statement * AW Lecture Notes ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
A Sample FORTRAN Program C *** This is a sample FORTRAN IV program PROGRAM SAMPLE READ (5, 990) A, B 990 FORMAT (F5.2, F5.2) SUM = A + B WRITE (6, 81) A, B, SUM 81 FORMAT (1X, F5.2, 3X, F5.2, 3X, F6.2) STOP END J.W. Perry Cole, ANSI FORTRAN IV, wcb ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN Data Types (1) • Integer • Implicit Typing: Variables whose names begin with I~N • Explicit Declaration e.g., INTEGER A, TOTAL • Real – Single precision floating point number • Implicit Typing: Variables whose names begin with other than A~H or O-Z • Explicit Declaration e.g., REAL J, R • Double precision floating point number e.g., DOUBLE PRECISION A, X ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN Data Types (2) • Complex e.g., COMPLEX A, B, C A = (3.5, -7.24) B = (-8.21, 5.67) C = A + B • Character e.g., CHARACTER C, NAME*20, ADDR*30 CHARACTER*20 STR1, STR2 Character constants: e.g., ‘C’, ‘Go ICU’ Hollerith Strings: e.g., 1HC, 5HGoICU • Logical (Boolean) e.g., LOGICAL X, Y, FLAG Logical constants: .TRUE. or .FALSE. ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Variable Initializations and Arrays • Variable Initializations e.g., DATA A/1.0/, L/2/, B,C/4.0, 5.0/ DATA ARYX(3)/1.333/, T(1),T(2),T(3)/3*0.0/ DATA C(1)/1HS/, C(2)/1HD/, TAG/3HYes • Arrays e.g., DIMENSION A(5), B(10,7), N(3,5,20) INTEGER X(7,5) REAL MATRIX(-6:4, 7, -5:10), K DIMENSION K(20) J.W. Perry Cole, ANSI FORTRAN IV, wcb ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN Operators • Arithmetic Operators: +, -, *, /, ** (exponentiation) • Mixed mode expressions – evaluated in integer mode if all operands are integer, evaluated in real mode otherwise e.g., 86.3 * K + R / 16.5 ** J All are evaluated in real • Relational Operators: .LT.,.LE.,.GT.,.GE.,.EQ.,.NE. e.g., IF (RESULT .LT. 0.0) STOP • Logical Operators: .AND., .OR., .NOT. e.g., IF (N .EQ. 1 .OR. .NOT. R .LT. 0.0) GOTO 75 • Multiple Assignment Statements e.g., A = I = V = W = .2 * R + X A truncated value will be assigned to A and I ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Program Flow Control • GotoGOTO (10, 20, 30) KCOUNT • Logical IFIF (K .LT. 1) K = K + 1 • Arithmetic IFIF (A / T – S) 10, 20, 30 • Block IFIF (M .GT. 15) THEN M = M / 3 ELSE M = M * 3 ENDIF • Do loopsDO 10 I = 1, N, 2 SUM = SUM + I 10 CONTINUE ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Data Input • Standard I/O Units: stdin (5), stdout (6) READ (5, 99) NUM, VAL, STR 99 FORMAT (I3, 2X, F5.2, 1X, A20) READ (5, 98) N1, N2, N3, STR1, STR2 98 FORMAT (3I5, //2A15) READ (5, *) L, SUM, NAME OPEN (UNIT=10, FILE=‘data.txt’, * STATUS=‘OLD’) READ (10, *) N, A, X, S ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Data Output • Carriage Control Codes: 1H1 (new page), 1H0 (double space), 1Hb (single space)… WRITE (6, 97) A, B, C 97 FORMAT (1H1,//3F8.2) WRITE (6, 96) X, Y, SUM 96 FORMAT (1H0, 4HX = , F4.1, 3X, 4HY = , * F4.1, 4X, 6HSUM = , F5.1, 20(1H*)) PRINT *, ‘X = ’, X, ‘Y = ’, Y, ‘SUM = ’, SUM OPEN (UNIT=11, FILE=‘data.txt’, * STATUS=‘NEW’) WRITE (11, *) ‘Result = ’, SUM ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Subprograms - Subroutines SUBROUTINE SUMMARY(X, N, TOT) DIMENSION X(20) TOT = 0.0 DO 10 I = 1, N TOT = TOT + X(I) 10 CONTINUE RETURN END J.W. Perry Cole, ANSI FORTRAN IV, wcb ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Subprograms - Functions FUNCTION TOTAL(X, N) DIMENSION X(20) TOTAL = 0.0 DO 10 I = 1, N TOTAL = TOTAL + X(I) 10 CONTINUE RETURN END ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Subprogram Calls PROGRAM SAMPLE2 DIMENSION A(10), B(20) READ (5, 99) A 99 FORMAT (10F4.2) CALL SUMMARY(A, 10, SUM) WRITE (6, 98) A, SUM 98 FORMAT (1, H0, 10F7.2/, 6HSUM = , F7.2) READ (5, 99) B WRITE (6, 98) B, TOTAL(SUM, 10) STOP END ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
GNU FORTRAN77 Compiler • Download G77 from: http://www.geocities.com/Athens/Olympus/5564/g77.htm • G77 Installation and Execution Instructions: http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Fortran_g77/GettingStartedGnuFortran.pdf • FORTRAN77 Tutorial: http://www.stanford.edu/class/me200c/tutorial_77/ • FORTRAN77 Language Reference: http://www.ictp.trieste.it/~manuals/programming/sun/fortran/f77rm/ ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Homework #4 – A Weather Data Retrieval Program (1) • Install G77 on your computer and practice how to compile and run FORTRAN programs • Download a weather data file that contains the latest week’s weather information from: ftp://ftp.ncdc.noaa.gov/pub/data/globalsod/data.txt • Download the weather station list from: ftp://ftp.ncdc.noaa.gov/pub/data/globalsod/stnlist-sorted.txt • Check the weather data file format from: ftp://ftp.ncdc.noaa.gov/pub/data/globalsod/README.TXT ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Homework #4 – A Weather Data Retrieval Program (2) • Write a FORTRAN program that accepts a city name (e.g., TAEJON, SEOUL, PARIS), and prints the lowest and highest temperatures (in Celsius), and sea-level pressure values for the latest week like: City: TAEJON Date MIN MAX SLP 20040328 9.5 15.2 1002.7 20040329 12.3 20.1 1005.1 … ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Homework #4 – A Weather Data Retrieval Program (3) • To retrieve weather information for a city, use the weather station number that is firstly matched against the city name in the station list • Use INDEX function to match a city name from the station list IF (INDEX(NAME, ‘ICU’) .GT. 0) PRINT *, ‘Matched!’ • Submit the source program electronically by Midnight of Wednesday April 28, 2004 • Please do not collaborate with other students! ICE 1341 – Programming Languages (Lecture #13) In-Young Ko