Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ATM S 380 Exercise 2 - Introduction to Fortran programming Exercise goals • Learn basics of Fortran programming • Practice writing, compiling, running simple Fortran codes Getting started • Log on to any computer with your atmos account • Open a terminal (Applications => terminal) Fortran • http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/intro.html • other documents • https://www.stat.tamu.edu/~jnewton/604/chap7.pdf • http://www.uv.es/dogarcar/man/IntrFortran90.pdf • http://www.egr.unlv.edu/~ed/fortranv2.1.pdf • http://www.fortrantutorial.com/documents/IntroductionToFTN95.pdf fortran code • Create a directory ‘F90’ under ‘ATMS380’ • Create two text files - mean.f90 and quad.f90 and copy the program from the website. ! ------------------------------------------------------! Computes arithmetic, geometric and harmonic means ! ------------------------------------------------------PROGRAM ComputeMeans IMPLICIT NONE REAL :: X = 1.0, Y = 2.0, Z = 3.0 REAL :: ArithMean, GeoMean, HarmMean WRITE(*,*) 'Data items: ', X, Y, Z WRITE(*,*) ArithMean = (X + Y + Z)/3.0 GeoMean = (X * Y * Z)**(1.0/3.0) HarmMean = 3.0/(1.0/X + 1.0/Y + 1.0/Z) WRITE(*,*) 'Arithmetic mean = ', ArithMean WRITE(*,*) 'Geometric mean = ', GeoMean WRITE(*,*) 'Harmonic Mean = ', HarmMean END PROGRAM ComputeMeans mean.f90 compilation and execution • Compile the program >>ifort mean.f90 • Do you see an executable file ‘a.out’ in the directory? >>ls • Execute a.out >>./a.out ! --------------------------------------------------! Solve Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0 ! --------------------------------------------------PROGRAM QuadraticEquation IMPLICIT NONE REAL :: a, b, c REAL :: d REAL :: root1, root2 ! read in the coefficients a, b and c WRITE(*,*) 'A, B, C Please : ' READ(*,*) a, b, c ! compute the square root of discriminant d d = SQRT(b*b - 4.0*a*c) ! solve the equation root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root ! display the results WRITE(*,*) WRITE(*,*) 'Roots are ', root1, ' and ', root2 END PROGRAM QuadraticEquation quad.f90 Exercise • Write a fortran program ‘var.f90’ that calculates variance and standard deviation of 10 numbers (ex. 1-10)