April 14, 2004 1 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Programming LanguagesProgramming Languages(ICE 1341)(ICE 1341)
Lecture #13Lecture #13 April 14, 2004
In-Young Koiko .AT. icu.ac.kr
Information and Communications University (ICU)
April 14, 2004 2 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
AnnouncementsAnnouncements
The The solution of the midterm examsolution of the midterm exam is is now available on the class homepagenow available on the class homepage
April 14, 2004 3 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN FORTRAN ((ForFormula mula TranTranslating System)slating System)
Designed to efficiently translate mathematical Designed to efficiently translate mathematical formulas into formulas into IBM 704IBM 704 machine code machine code
IBM 704IBM 704 (1954) (1954)““The first mass-produced The first mass-produced
computer with computer with core core memorymemory and and floating-floating-
point arithmeticpoint arithmetic”” Photo: Lawrence Livermore National Laboratory
April 14, 2004 4 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN Coding FormatFORTRAN Coding Format
FORTRAN programs FORTRAN programs were written in a were written in a coding form with a coding form with a strict formatting rulestrict formatting rule
Continuation (6) Continuation (6)
FORTRAN Statement (7-72)FORTRAN Statement (7-72)
Identification Sequence (73-80)Identification Sequence (73-80)
Statement Number (1-5)Statement Number (1-5)
J.W. Perry Cole, ANSI FORTRAN IV,
wcb
April 14, 2004 5 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Punched CardsPunched Cards
Coded FORTRAN programs Coded FORTRAN programs were converted onto were converted onto
punched cards for loading punched cards for loading
Photos: http://www.tno.nl/instit/fel/museum/computer/en/punchcards.html
April 14, 2004 6 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Early FORTRAN VersionsEarly FORTRAN Versions
FORTRAN I (1957)FORTRAN I (1957) NamesNames could have up to could have up to six characterssix characters (IBM 704 has (IBM 704 has
a 6-bit BCD character set and 36-bit word)a 6-bit BCD character set and 36-bit word) Post-test counting loopPost-test counting loop ( (DODO), ), Formatted I/OFormatted I/O, , User-User-
defined subprogramsdefined subprograms, , Three-way selection Three-way selection statementstatement (arithmetic (arithmetic IFIF))
No data typing statements (No data typing statements (Implicit Type DeclarationImplicit Type Declaration –– Variables whose names begin with I, J, K, L, M, Variables whose names begin with I, J, K, L, M, and N are integer types, all others are floating point)and N are integer types, all others are floating point)
FORTRAN II (1958)FORTRAN II (1958) IndependIndependeent compilationnt compilation of subroutines of subroutines
* AW Lecture Notes
April 14, 2004 7 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Later FORTRAN VersionsLater FORTRAN Versions
FORTRAN IV (1960-62) FORTRAN IV (1960-62) –– ANSI standard in 1966 ANSI standard in 1966 Explicit type declarationsExplicit type declarations Logical selection statementLogical selection statement Subprogram names could be parametersSubprogram names could be parameters
FORTRAN 77 (1978)FORTRAN 77 (1978) Character stringCharacter string handling handling Logical loop control statementLogical loop control statement IF-THEN-ELSEIF-THEN-ELSE statement statement
FORTRAN 90 (1990)FORTRAN 90 (1990) Free coding formatFree coding format Modules, Dynamic arrays, Pointers, Recursion, Modules, Dynamic arrays, Pointers, Recursion, CASECASE
statementstatement* AW Lecture Notes
April 14, 2004 8 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
A Sample FORTRAN ProgramA Sample FORTRAN Program
C *** This is a sample FORTRAN IV programC *** This is a sample FORTRAN IV program
PROGRAM SAMPLEPROGRAM SAMPLE
READ (5, 990) A, BREAD (5, 990) A, B
990990 FORMAT (F5.2, F5.2)FORMAT (F5.2, F5.2)
SUM = A + BSUM = A + B
WRITE (6, 81) A, B, SUMWRITE (6, 81) A, B, SUM
8181 FORMAT (1X, F5.2, 3X, F5.2, 3X, F6.2)FORMAT (1X, F5.2, 3X, F5.2, 3X, F6.2)
STOPSTOP
ENDEND
J.W. Perry Cole, ANSI FORTRAN IV, wcb
April 14, 2004 9 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN Data Types (1)FORTRAN Data Types (1)
IntegerInteger Implicit TypingImplicit Typing: Variables whose names begin with I~N: Variables whose names begin with I~N Explicit DeclarationExplicit Declaration
e.g., e.g., INTEGER A, TOTALINTEGER A, TOTAL Real Real –– Single precision floating point number Single precision floating point number
Implicit TypingImplicit Typing: Variables whose names begin with : Variables whose names begin with other than A~H or O-Zother than A~H or O-Z
Explicit DeclarationExplicit Declaration
e.g., e.g., REAL J, RREAL J, R Double precisionDouble precision floating point number floating point number
ee.g., .g., DOUBLE PRECISION A, XDOUBLE PRECISION A, X
April 14, 2004 10 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN Data Types (2)FORTRAN Data Types (2)
Complex Complex e.g., e.g., COMPLEX A, B, CCOMPLEX A, B, C
A = (3.5, -7.24)A = (3.5, -7.24) B = (-8.21, 5.67)B = (-8.21, 5.67) C = A + BC = A + B
CharacterCharactere.g., e.g., CHARACTER C, NAME*20, ADDR*30CHARACTER C, NAME*20, ADDR*30 CHARACTER*20 STR1, STR2CHARACTER*20 STR1, STR2
Character constants: e.g., Character constants: e.g., ‘C’‘C’,, ‘Go ICU’ ‘Go ICU’ Hollerith StringsHollerith Strings:: e.g., 1HC, 5HGoICU e.g., 1HC, 5HGoICU
LogicalLogical (Boolean) (Boolean)e.g., e.g., LOGICAL X, Y, FLAGLOGICAL X, Y, FLAG
Logical constants: Logical constants: .TRUE..TRUE. oor r .FALSE..FALSE.
April 14, 2004 11 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Variable Initializations and ArraysVariable Initializations and Arrays
Variable InitializationsVariable Initializations
e.g., e.g., DATA A/1.0/, L/2/, B,C/4.0, 5.0/DATA A/1.0/, L/2/, B,C/4.0, 5.0/
DATA ARYX(3)/1.333/, DATA ARYX(3)/1.333/, T(1),T(2),T(3)/3*0.0/T(1),T(2),T(3)/3*0.0/
DATA C(1)/1HS/, C(2)/1HD/, TAG/3HYesDATA C(1)/1HS/, C(2)/1HD/, TAG/3HYes ArraysArrays
e.g., e.g., DIMENSION A(5), B(10,7), N(3,5,20)DIMENSION A(5), B(10,7), N(3,5,20)
INTEGER X(7,5)INTEGER X(7,5)
REAL MATRIX(-6:4, 7, -5:10), KREAL MATRIX(-6:4, 7, -5:10), K
DIMENSION K(20)DIMENSION K(20)J.W. Perry Cole, ANSI FORTRAN IV, wcb
April 14, 2004 12 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
FORTRAN OperatorsFORTRAN Operators
Arithmetic Operators: Arithmetic Operators: ++, , --, , **, , //, , **** (exponentiation) (exponentiation) Mixed mode expressions – evaluated in integer mode Mixed mode expressions – evaluated in integer mode if all if all
operands are integeroperands are integer, evaluated in real mode otherwise, evaluated in real mode otherwisee.g., e.g., 86.3 * K + R / 16.5 ** J86.3 * K + R / 16.5 ** J All are evaluated in All are evaluated in
realreal
Relational Operators: Relational Operators: .LT..LT.,,.LE.LE.,.,.GT..GT.,,.GE..GE.,,.EQ..EQ.,,.NE..NE.e.g., e.g., IF (RESULT .LT. 0.0) STOPIF (RESULT .LT. 0.0) STOP
Logical Operators: Logical Operators: .AND..AND., , .OR..OR., , .NOT..NOT.e.g., e.g., IF (N .EQ. 1 .OR. .NOT. R .LT. 0.0) GOTO 75IF (N .EQ. 1 .OR. .NOT. R .LT. 0.0) GOTO 75
Multiple Assignment StatementsMultiple Assignment Statementse.g., e.g., A = I = V = W = .2 * R + XA = I = V = W = .2 * R + X A truncated value will be assigned to A and IA truncated value will be assigned to A and I
April 14, 2004 13 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Program Flow ControlProgram Flow Control
GotoGoto GOTO (10, 20, 30) KCOUNTGOTO (10, 20, 30) KCOUNT
Logical IFLogical IF IF (K .LT. 1) K = K + 1IF (K .LT. 1) K = K + 1
Arithmetic IFArithmetic IF IF (A / T IF (A / T –– S) 10, 20, 30 S) 10, 20, 30
Block IFBlock IF IF (M .GT. 15) THEN M = M / 3IF (M .GT. 15) THEN M = M / 3ELSE M = M * 3ELSE M = M * 3ENDIFENDIF
Do loopsDo loops DO 10 I = 1, N, 2DO 10 I = 1, N, 2 SUM = SUM + ISUM = SUM + I
10 CONTINUE10 CONTINUE
April 14, 2004 14 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Data InputData Input
Standard I/O Units: stdin (Standard I/O Units: stdin (55), stdout (), stdout (66))
READ (5, 99) NUM, VAL, STRREAD (5, 99) NUM, VAL, STR
9999 FORMAT (I3, 2X, F5.2, 1X, A20)FORMAT (I3, 2X, F5.2, 1X, A20)
READ (5, 98) N1, N2, N3, STR1, STR2READ (5, 98) N1, N2, N3, STR1, STR2
9898 FORMAT (3I5, //2A15)FORMAT (3I5, //2A15)
READ (5, *) L, SUM, NAMEREAD (5, *) L, SUM, NAME
OPEN (UNIT=10, FILE=‘data.txt’,OPEN (UNIT=10, FILE=‘data.txt’,
** STATUS=‘OLD’) STATUS=‘OLD’)
READ (10, *) N, A, X, SREAD (10, *) N, A, X, S
April 14, 2004 15 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Data OutputData Output
Carriage Control Codes: Carriage Control Codes: 1H11H1 (new page), (new page), 1H01H0 (double space), (double space), 1Hb 1Hb (single space)(single space)……
WRITE (6, 97) A, B, CWRITE (6, 97) A, B, C9797 FORMAT (1H1,//3F8.2)FORMAT (1H1,//3F8.2)
WRITE (6, 96) X, Y, SUMWRITE (6, 96) X, Y, SUM9696 FORMAT (1H0, 4HX = , F4.1, 3X, 4HY = ,FORMAT (1H0, 4HX = , F4.1, 3X, 4HY = ,
* * F4.1, 4X, 6HSUM = , F5.1, 20(1H*))F4.1, 4X, 6HSUM = , F5.1, 20(1H*))PRINT *, ‘X = ’, X, ‘Y = ’, Y, ‘SUM = ’, SUMPRINT *, ‘X = ’, X, ‘Y = ’, Y, ‘SUM = ’, SUMOPEN (UNIT=11, FILE=‘data.txt’,OPEN (UNIT=11, FILE=‘data.txt’,
** STATUS=‘NEW’) STATUS=‘NEW’)WRITE (11, *) ‘Result = ’, SUMWRITE (11, *) ‘Result = ’, SUM
April 14, 2004 16 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Subprograms - SubroutinesSubprograms - Subroutines
SUBROUTINE SUMMARY(X, N, TOT)SUBROUTINE SUMMARY(X, N, TOT)
DIMENSION X(20)DIMENSION X(20)
TOT = 0.0TOT = 0.0
DO 10 I = 1, NDO 10 I = 1, N
TOT = TOT + X(I)TOT = TOT + X(I)
1010 CONTINUECONTINUE
RETURNRETURN
ENDEND
J.W. Perry Cole, ANSI FORTRAN IV, wcb
April 14, 2004 17 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Subprograms - FunctionsSubprograms - Functions
FUNCTION TOTAL(X, N)FUNCTION TOTAL(X, N)
DIMENSION X(20)DIMENSION X(20)
TOTAL = 0.0TOTAL = 0.0
DO 10 I = 1, NDO 10 I = 1, N
TOTAL = TOTAL + X(I)TOTAL = TOTAL + X(I)
1010 CONTINUECONTINUE
RETURNRETURN
ENDEND
April 14, 2004 18 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Subprogram CallsSubprogram Calls
PROGRAM SAMPLE2PROGRAM SAMPLE2DIMENSION A(10), B(20)DIMENSION A(10), B(20)READ (5, 99) AREAD (5, 99) A
9999 FORMAT (10F4.2)FORMAT (10F4.2)CALL SUMMARY(A, 10, SUM)CALL SUMMARY(A, 10, SUM)WRITE (6, 98) A, SUMWRITE (6, 98) A, SUM
9898 FORMAT (1, H0, 10F7.2/, 6HSUM = , F7.2)FORMAT (1, H0, 10F7.2/, 6HSUM = , F7.2)READ (5, 99) BREAD (5, 99) BWRITE (6, 98) B, WRITE (6, 98) B, TOTAL(SUM, 10)TOTAL(SUM, 10)STOPSTOPENDEND
April 14, 2004 19 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
GNU FORTRAN77 CompilerGNU FORTRAN77 Compiler
Download Download G77G77 from: from:http://www.geocities.com/Athens/Olympus/5564/g77.htmhttp://www.geocities.com/Athens/Olympus/5564/g77.htm
G77 Installation and Execution Instructions:G77 Installation and Execution Instructions:http://www.engineering.usu.edu/cee/faculty/gurro/http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Fortran_g77/GettingStartedGnuFortran.pdfSoftware_Calculators/Fortran_g77/GettingStartedGnuFortran.pdf
FORTRAN77 Tutorial:FORTRAN77 Tutorial:http://www.stanford.edu/class/me200c/tutorial_77/http://www.stanford.edu/class/me200c/tutorial_77/
FORTRAN77 Language Reference:FORTRAN77 Language Reference:http://www.ictp.trieste.it/~manuals/programming/sun/fortran/f77rm/http://www.ictp.trieste.it/~manuals/programming/sun/fortran/f77rm/
April 14, 2004 20 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Homework #4 Homework #4 –– A Weather Data A Weather Data Retrieval Program (1)Retrieval Program (1)
Install Install G77G77 on your computer and practice how on your computer and practice how to compile and run FORTRAN programsto compile and run FORTRAN programs
Download a Download a weather data fileweather data file that contains the that contains the latest week’s weather information from: latest week’s weather information from: ftp://ftp.ncdc.noaa.gov/pub/data/globalsod/data.txtftp://ftp.ncdc.noaa.gov/pub/data/globalsod/data.txt
Download the Download the weather station listweather station list from: from: ftp://ftp.ncdc.noaa.gov/pub/data/globalsod/stnlist-sorted.txtftp://ftp.ncdc.noaa.gov/pub/data/globalsod/stnlist-sorted.txt
Check the Check the weather data file formatweather data file format from: from: ftp://ftp.ncdc.noaa.gov/pub/data/globalsod/README.TXTftp://ftp.ncdc.noaa.gov/pub/data/globalsod/README.TXT
April 14, 2004 21 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Homework #4 Homework #4 –– A Weather Data A Weather Data Retrieval Program (2)Retrieval Program (2)
Write a FORTRAN program that Write a FORTRAN program that accepts a city accepts a city namename (e.g., TAEJON, SEOUL, PARIS), and (e.g., TAEJON, SEOUL, PARIS), and prints the lowest and highest temperatures (in prints the lowest and highest temperatures (in Celsius), and sea-level pressure valuesCelsius), and sea-level pressure values for the for the latest week like:latest week like:
City: TAEJONCity: TAEJON
DateDate MINMIN MAXMAX SLPSLP
2004032820040328 9.59.5 15.215.2 1002.71002.7
2004032920040329 12.312.3 20.120.1 1005.11005.1
……
April 14, 2004 22 ICE 1341 – Programming Languages (Lecture #13) In-Young Ko
Homework #4 Homework #4 –– A Weather Data A Weather Data Retrieval Program (3)Retrieval Program (3)
To retrieve weather information for a city, use To retrieve weather information for a city, use the the weather station numberweather station number that is firstly that is firstly matched against the city name in the station listmatched against the city name in the station list
Use INDEX function to match a city name from Use INDEX function to match a city name from the station listthe station list
IF (IF (INDEXINDEX(NAME, ‘ICU’) .GT. 0)(NAME, ‘ICU’) .GT. 0)
PRINT *, ‘Matched!’PRINT *, ‘Matched!’ Submit the source program Submit the source program electronicallyelectronically by by
Midnight of Wednesday April 28, 2004Midnight of Wednesday April 28, 2004 Please do not collaborate with other students!Please do not collaborate with other students!