News:

SMF - Just Installed!

Main Menu

sample fotran code

Started by Mark Barkey, June 08, 2017, 06:10:10 PM

Previous topic - Next topic

Mark Barkey

sample fortran code


      Program Examples
c     
c  Every Fortran program must start with the statement "program"
c


c  It is a good idea to identify the author, date, and purpose
c  of your program


c
c      Fortran Examples
c      M. E. Barkey
c      18 August 2009
c      This program will give some basic examples of Fortran Statements
c
c

c  In fixed-format fortran, where things are is important.


c23456789012345678901234567890123456789012345678901234567890123456789012
c
c     comments are any lines that have a character in position 1
c
c     columns 2-5 are for reference numbers or statement numbers
c
c     column 6 is reservered for a continuation character
c     any character except "0" (zero) can be used
C     !CAUTION!  DO NOT USE TAB to get over to column 6  use spaces!
c
c     in Fortran 77 (fixed format), programming statements must be at
c     column 7 or later and must not extend past column 72


c  The next section is for variable declarations.
c     Good programming practice is to declare ALL variables.
c     If a variable is not declared, it is assumed that
c     a-h and o-z are reals
c     i-n are integers (notice interger strates with i and n)
c     variables in F77 must start with a character, and be
c     no longer than 6 characters/numbers


      Real a, b(10), c(10), a1, a2, a3, a4, a5, k, k2
      Double Precision d(10)
      Character eChar*25
      Integer i, j, e(10)




c  Sample of how to write to screen

Write (*,*) 'This is a screen print' 





a = 10.78
a1 = a*1.0
j = a1*1.0

C making something an integer will TRUNCATE, not round the number
C in strict F77, mulitplying a real with an integer will result
c in truncation

c It is good practice to always use real numbers when multiplying
c real variables, e.g. a1 = a * 1.0, NOT a1 = a * 1


write (*,*) a, a1, j


c to take a number to a power, use two stars (**)
c addition, multiplication, subtraction, division are as usual

a2 = a**2.0
a3 = a + a
a4 = 2.0*a - 0.5*a
a5 = a2/a3


write (*,*) a2, a3, a4, a5







c  Do Loop to intialize an array
c  500 is reference line number
c  i is index, 1, 10 is go from 1 to 10
c  the last 1 is the step

write (*,*)
write (*,*)
write (*,*) 'this is the do loop'

c  it is good practice to indent the items
c  inside the do loop

Do 500, i = 1, 10, 1

b(i) = a*real(i)
c(i) = a*i
d(i) = a*2.0*real(i)
e(i) = int (d(i))

write (*,*) i, b(i), c(i), d(i), e(i)

500    Continue


k = 4.7
write (*,*)
write (*,*) k

c comparisons are done with .lt. .gt. .le. .ge. .eq.

if (k.le.3.0) then

   write (*,*) 'less than or equal to 3.0'

else

           write (*,*) 'must be greater than or equal to 3.0'

end if


c boolean operators are .AND. .OR. .NOT.

         if ((k.gt.4.0).AND.(k.lt.6.0)) then

           write (*,*) 'must be greater than 4.0'
   write (*,*) 'and less than 6.0'

end if


C file input/output

open (12, file = 'sample.dat')

echar = 'this is a string'

write (12, *) 'unformatted output'
write (12, *) k

write (12, *) 'formatted output'
write (12, 900) k, k, int(k), echar

close (12)






       open (14, file = 'sample.dat', status = 'old')

        read (14, *) echar
        read (14, *) k2

write (*,*) 'part of file'
        write (*,*) echar, k2
       
       close (14)





c      f is floating point, 6.2 means 6 total spots, 2 decimal
c      10x is ten spaces
c      i5 is 5 spot integer
c      a5 is 5 character string


900   format (f6.2, 10x, f6.1, i5, 5x, a5)


       End