03 Fortran

Download 03 Fortran

Post on 20-Nov-2015

1 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

F

TRANSCRIPT

<ul><li><p>CS 415: Programming LanguagesFortranAaron BloomfieldFall 2005</p></li><li><p>The IBM 704</p></li><li><p>Standard Fortran joke</p><p>GOD is REAL (unless declared INTEGER)." </p></li><li><p>End of lecture on 26 Aug 2005Between Fortran I and Fortran I program control</p></li><li><p>Fortran I program control</p><p>IF (arithmetic expression) N1, N2, N3</p><p>DO N1 variable = first_value, last_value</p></li><li><p>Punch cards</p></li><li><p>Fortran history reference</p><p>http://www.ibiblio.org/pub/languages/fortran/ch1-1.html</p></li><li><p>Installing Fortran on WindowsWell use Fortran 77 for this courseThe compiler has some Fortran 90 featuresInstall Cygwin: http://www.cygwin.com/Its a Unix-like shell for WindowsIn particular, when you install it:Install the gcc-g77 package in the Devel sectionWe may use Ocaml if so, then you will need to install the ocaml package (also in Devel)This can be done later, tooInstall a good editorI like Emacs: http://www.gnu.org/software/emacs/emacs.htmlQuite a learning curve, but the best editor out thereBinaries at http://ftp.gnu.org/pub/gnu/emacs/windows/ You can also install and use it as part of CygwinTutorials can be found online</p></li><li><p>Compiling a Fortran programEdit the program using your favorite editorAll program code must start on the 7th column!In Cygwin, change to that directoryThe c drive is at /cygdrive/c, etc.Enter the command:g77 ff90 file.fThen run the file:./a.exe</p></li><li><p>Hello world in Fortran</p><p> PROGRAM HelloWorld PRINT *,'Hello world!' END PROGRAM HelloWorld</p><p>Column 7Column 1</p></li><li><p>Fortran syntaxLines can only be 72 characters longComments start with a !First 6 columns must be spacesUnless its a commentNo semi-colons after each lineA newline is a statement terminator</p></li><li><p>Variable declarationThe types are real, integer, etc.</p><p>real :: x, y, zinteger :: a, b, ccharacter :: g</p><p>Always put implicit none at the beginningRight after the program linePrevents implicit variable declaration</p></li><li><p>Input and outputOutput statement:print *, "(", tri1x, ", ", tri1y, ")</p><p>Input statement:read *, tri3x</p><p>There are ways to do nicely formatted outputWe arent going over them</p></li><li><p>OperatorsBoolean operators: .and., .or., .not., etc.Basically the name of the operation in periodsBoolean values are .true. and .false.</p><p>Relational operators: , =, ==, /=</p></li><li><p>Built-in functionssqrt()log()sin()cos()exp()etc.</p></li><li><p>If statementsForms are (exp is a Boolean expression):</p><p>if (exp ) then ...endif</p><p>if ( exp ) then ...else ...endifif ( exp ) then ...else if ( exp ) then ...else if ( exp ) then ...endif</p></li><li><p>Case statementForm is:</p><p>select case ( expr )case ( value ) ...case ( value ) ...case ( value ) ...case default ...end caseWhere value can be:A single value(300:)A range of values(200:400)</p><p>Case default is not required</p></li><li><p>LoopingForm is:</p><p>do i = 1, 10 ...end do</p><p>do i = 1, 10, 2 ...end do</p><p>The first loops from 1 to 10</p><p>The second loops from 1 to 10, but odd numbers only</p></li><li><p>Loop controlExitExits the loop, not the program</p><p>CycleSimilar to next or continue in other languagesStarts the next iteration of the loop</p></li><li><p>Demo program! This program allows the user to input the number of degrees in an angle! and then computes the cosine, sine, and tangent. It continues until the! user inputs "n" or "N". PROGRAM angle IMPLICIT none! Type variables. REAL :: cosine, sine, tangent, degrees REAL :: pi = 3.141592 CHARACTER :: choice DO! Enter and read the number of degrees in the angle. PRINT *, "Enter the number of degrees in the angle." READ *, degrees! Convert number of degrees in angle to radians. degrees = degrees*(pi/180)! Use intrinsic functions to compute values. cosine=cos(degrees) sine=sin(degrees) tangent=tan(degrees)! Print results. PRINT *, "cosine=", cosine, " sine=", sine, " tangent=", tangent! Give user chance to exit program. PRINT * PRINT *, "Would you like to do this again?" PRINT *,"(Press n to exit - any other key to continue.)" READ *, choice! Exit loop if the value in choice is N or n. IF (choice == "N" .or. choice == "n") EXIT END DO STOP END PROGRAM angleComputes the sin, cos, tan, etc.</p></li><li><p>Demo program! This program averages a series of numbers input ! from the keyboard. PROGRAM average IMPLICIT none! Type variables. REAL :: data, sum, avg INTEGER num, i! Prompt for and enter number of numbers to average. PRINT *,"Enter the number of numbers to average." READ *,num sum = 0.0! Loop goes from 1 to number of values to average. DO i = 1, num! Prompt for and enter a number. PRINT *,"Enter a value for the number" READ *,data! Add number to total. sum = sum + data END DO! Calculate average. avg = sum/real(num)! Print results. PRINT *,"The average = ",avg STOP ENDComputes the average</p></li><li><p>Demo program! This program uses a function to find the average of three numbers. PROGRAM func_ave! Type variables in main program (a, b, and c are local variables). REAL :: a,b,c,average! Prompt for and get numbers to be averaged. PRINT *,"Enter the three numbers to be averaged." READ *, a,b,c! Invoke function average PRINT *,"The three numbers to be averaged are ",a,b,c PRINT *,"The average of the three numbers is ", average(a,b,c) STOP END PROGRAM func_ave! Function average REAL FUNCTION average(x,y,z)! Type variables in function (x, y, and z are local varialbes). REAL :: x,y,z! Function name contains the average the function calculates and returns. average = (x + y + z)/3.0 RETURN END FUNCTION averageComputes the average via a defined function</p></li><li><p>Fortran gotchasAll variables must be declared at the beginningRemember line limit of 72 characters!Consider:</p><p>The 8th variable is named eiThere is no 9th variable declaredNo continuation lines in Fortran 77== is comparison for if'sCant seem to be able to change the values of parameters in functionsinteger :: first, second, third, fourth, fifth, sixth, seventh, eighth, ninthColumn 72!</p></li><li><p>End of lecture on 29 Aug 2005Next 3 slides gone over a few days later</p></li><li><p>John BackusChemistry major at UVA (entered 1943)Flunked out after second semesterJoined IBM as programmer in 1950Developed Fortran, first commercially successful programming language and compiler</p></li><li><p>IBM 704 Fortran manual, 1956</p></li><li><p>Fortran issuesFortran language was described using EnglishImpreciseVerbose, lots to readAd hocDO 10 I=1.10Assigns 1.10 to the variable DO10I Early Fortrans didnt care about spaces!DO 10 I=1,10Loops for I = 1 to 10(Often incorrectly blamed for loss of Mariner-I)</p></li></ul>