program test cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c This program is a collection of frequently used fortran c c commands. You need an input file 'numbers.dat' with 7 lines c c and 2 number entries in each line. Check the output in c c 'maxed.dat' . You also need the header-file named 'declar.fh' . c c c c Elsebeth Schroder March 14, 2000 c cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c23456789012345678901234567890123456789012345678901234567890123456789012 c c A character in the first position of a line makes the compiler c ignore the line. A character in the 6th position is a code c to tell the compiler that the line is a continuation of the c previous line. You can number a line (for use with "goto") c using the 2nd to 5th character. However, goto-commands othen c lead to bad programming, and the command can most of the time c be avoided by using instead slightly non-standard commands. c All modern compilers will understand these commands, and c I use them in this sample program. c c You can write your commands from position 8 to 72. Anything c at position 73 and onwards is ignored by the compiler. c implicit none c c In Fortran you do not have to declare all your variables; c they are implicitly declared depending on the starting letter c in their name. This is dangerous: if you misspell a variable c name you suddenly calculate something different than what you c think. For this reason, I always turn off the implicit c variable declaration using the command "implicit none". c Now the compiler will complain if you forgot to declare c a variable or misspelled its name. c include 'declar.fh' c c If you put declarations that are also used in functions/subroutines c in a separate file you will only need to change e.g. the maximum c numbers of rows (max_iy) in the header file, not in every c function/subroutine declaration. c call myread(tabel) c do iy=1,max_iy tabel(1,iy)=mymax(tabel(1,iy),tabel(2,iy)) tabel(2,iy)=tabel(1,iy) enddo c call mywrite(tabel) c stop end c c This ends the main program. Below are the functions and c subroutines. They can also be saved in external files, one c file for each procedure/subroutine. With separate files more c than one person at a time can edit the program, provided that c the changes are consistent with the rest of the program. c cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c c function mymax(value1,value2) c c This function simply finds the maximum value of two numbers. c This is only meant as an example of the use of "function", c "if" etc. There already exists a similar function in c Fortran, "max". c implicit none include 'declar.fh' real*8 value1, value2 c if (value1.gt.value2) then mymax=value1 else mymax=value2 endif end c c The commonly used relational operators are c .gt. corresponds to > (greater than) c .ge. corresponds to >= (greater than or equal) c .lt. corresponds to < (less than) c .le. corresponds to <= (less than or equal) c .eq. corresponds to = (equal to) c .and. c .or. inclusive or (this or that or both) c .nor. exclusive or (this or that but not both) c ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c subroutine myread(tabel) c implicit none include 'declar.fh' c open(unit=1,file='numbers.dat',type='old') do iy=1,max_iy read(1,*) tabel(1,iy),tabel(2,iy) enddo close(1) end c c units 5 and 6 are reserved for standard input and standard c output (often: keyboard and screen). You will get strange results c if you try to use unit 5 or 6 for files. c ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c subroutine mywrite(tabel) c implicit none include 'declar.fh' c open(unit=2,file='maxed.dat',type='unknown') do iy=1,max_iy write(2,*) real(tabel(1,iy)),real(tabel(2,iy)) enddo close(2) c end