Professional Documents
Culture Documents
Beginning Fortran
Fortran (77) Basics
22 October 2009
Example Code
Write a program to read in five
values of temperature in Fahrenheit
and convert to degrees Celsius OR
Kelvin OR both.
Declaration of Variables
MAIN CODE
STOP
END
Declaration of Variables
MAIN CODE
STOP
END
Program Declaration
You declare what kind of Fortran file
you are writing on the first line.
Syntax: <TYPE> <NAME>
c234567
PROGRAMCONVERTF
Program Declaration
You declare what kind of Fortran file
you are writing on the first line.
Syntax: <TYPE> <NAME>
c234567
PROGRAMCONVERTF
Specifies the file as a program
Declaration of Variables
MAIN CODE
STOP
END
STOP
END
Main Code
We need to do several things:
Read in 5 values of temperature
Determine if we need to convert to
Celsius, Kelvin, or both
Output values
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
Read in 5 values of F
into array
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
Ki
From User
To User
DOK
Fi
Ci
DOC
To User
To User
From User
Main Code
c234567
DOI=1,NT
READ(*,*)F(I)
ENDDO
From User
Main Code
c234567
DOI=1,NT
READ(*,*)F(I)
ENDDO
From User
Main Code
c234567
DOI=1,NT
READ(*,*)F(I)
ENDDO
WRITE(*,*)ConverttoC?
READ(*,*)DOC
WRITE(*,*)ConverttoK?
READ(*,*)DOK
From User
Main Code
c234567
DOI=1,NT
READ(*,*)F(I)
ENDDO
WRITE(*,*)ConverttoC?
READ(*,*)DOC
WRITE(*,*)ConverttoK?
READ(*,*)DOK
Main Code
c234567
DOI=1,NT
C(I)=(5./9.)*(F(I)32.)
ENDDO
Main Code
c234567
DOI=1,NT
C(I)=(5./9.)*(F(I)32.)
ENDDO
Main Code
c234567
DOI=1,NT
C(I)=(5./9.)*(F(I)32.)
ENDDO
Main Code
c234567
IF(DOK.EQV..TRUE.)THEN
DOI=1,NT
K(I)=C(I)+273.15
ENDDO
ENDIF
Main Code
c234567
IF(DOK.EQV..TRUE.)THEN
DOI=1,NT
K(I)=C(I)+273.15
ENDDO
ENDIF
Logical trap: If we want to calculate Kelvin:
Main Code
c234567
IF(DOK.EQV..TRUE.)THEN
DOI=1,NT
K(I)=C(I)+273.15
ENDDO
ENDIF
Logical trap: If we want to calculate Kelvin:
Loop through temperatures and calculate Kelvin temps.
(If DOK = .FALSE., this entire loop is avoided)
Main Code
c234567
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..FALSE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C
ENDDO
ENDIF
c
IF((DOC.EQV..FALSE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,K(I),K
ENDDO
ENDIF
c
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C,K(I),K
ENDDO
ENDIF
Main Code
c234567
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..FALSE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C
ENDDO
ENDIF
c
IF((DOC.EQV..FALSE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,K(I),K
ENDDO
ENDIF
c
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C,K(I),K
ENDDO
ENDIF
c234567
PROGRAMCONVERTF
IMPLICITNONE
PARAMETERNT=5
REALF(NT)
REALK(NT)
REALC(NT)
LOGICALDOC
LOGICALDOK
INTEGERI
DOI=1,NT
READ(*,*)F(I)
ENDDO
WRITE(*,*)ConverttoC?
READ(*,*)DOC
WRITE(*,*)ConverttoK?
READ(*,*)DOK
DOI=1,NT
C(I)=(5./9.)*(F(I)32.)
ENDDO
IF(DOK.EQV..TRUE.)THEN
DOI=1,NT
K(I)=C(I)+273.15
ENDDO
ENDIF
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..FALSE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C
ENDDO
ENDIF
c
IF((DOC.EQV..FALSE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,K(I),K
ENDDO
ENDIF
c
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C,K(I),K
ENDDO
ENDIF
STOP
END
c234567
PROGRAMCONVERTF
IMPLICITNONE
PARAMETERNT=5
REALF(NT)
REALK(NT)
REALC(NT)
LOGICALDOC
LOGICALDOK
INTEGERI
DOI=1,NT
READ(*,*)F(I)
ENDDO
WRITE(*,*)ConverttoC?
READ(*,*)DOC
WRITE(*,*)ConverttoK?
READ(*,*)DOK
DOI=1,NT
C(I)=(5./9.)*(F(I)32.)
ENDDO
IF(DOK.EQV..TRUE.)THEN
DOI=1,NT
K(I)=C(I)+273.15
ENDDO
ENDIF
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..FALSE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C
ENDDO
ENDIF
c
IF((DOC.EQV..FALSE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,K(I),K
ENDDO
ENDIF
c
IF((DOC.EQV..TRUE.).AND.(DOK.EQV..TRUE.))THEN
DOI=1,NT
WRITE(*,*)F(I),F=,C(I),C,K(I),K
ENDDO
ENDIF
STOP
END
Program Start
Options/Variable Declaration
Main Code
Program End
Compilation
Compilation is performed in the
terminal:
Syntax:
<compiler> -o <exec. filename> <source
filename> <options>
Compilation
Compilation is performed in the
terminal:
Syntax:
<compiler> -o <exec. filename> <source
filename> <options>
Depends on system: f77, g77, pgf77, etc.
Compilation
Compilation is performed in the
terminal:
Syntax:
<compiler> -o <exec. filename> <source
filename> <options>
We wish to create an object that is an executable file with the following name
Compilation
Compilation is performed in the
terminal:
Syntax:
<compiler> -o <exec. filename> <source
filename> <options>
Use this *.f file to compile the executable
Compilation
Compilation is performed in the
terminal:
Syntax:
<compiler> -o <exec. filename> <source
filename> <options>
Also depends on compiler. Some frequent options:
Mextend allows you to go over column 70 in the code
Mbounds if you attempt to reference an array index out of bounds, will notify you
Mbyteswapio some formats require a byte-swap
Compilation
Compilation is performed in the
terminal:
Syntax:
<compiler> -o <exec. filename> <source
filename> <options>
pgf77oCONVERTF.exeCONVERTF.f