FORTRAN 90/95 compiler.



Example Program

  • The "Implicit None" statement forces the programmer to declare all variables.
  • The "Read *" statement indicates the program reads from standard input with no formatting.
  • The "Print *" statement indicates the program prints to standard output with no formatting.
  • Alternate forms of Read and Print include Print(*,*), where the second asterisk is a label that references a format for how the data will be visually represented.

  1. ! Fortran 95 Example program
  2. ! Input: An integer, List_Len, where List_Len is less
  3. ! than 100, followed by List_Len-Integer values
  4. ! Output: The number of input values that are greater
  5. ! than the average of all input values
  6. Implicit none
  7. Integer Dimension(99) :: Int_List
  8. Integer :: List_Len, Counter, Sum, Average, Result
  9. Result= 0
  10. Sum = 0
  11. Read *, List_Len
  12. If ((List_Len > 0) .AND. (List_Len < 100)) Then
  13. ! Read input data into an array and compute its sum
  14. Do Counter = 1, List_Len
  15. Read *, Int_List(Counter)
  16. Sum = Sum + Int_List(Counter) End Do
  17. ! Compute the average
  18. Average = Sum / List_Len
  19. ! Count the values that are greater than the average
  20. Do Counter = 1, List_Len
  21. If (Int_List(Counter) > Average) Then
  22. Result = Result + 1
  23. End If
  24. End Do
  25. ! Print the result
  26. Print *, 'Number of values > Average is:', Result
  27. Else
  28. Print *, 'Error - list length value is not legal'
  29. End If
  30. End Program Example


Readability Example

Fortran 90/95 introduced whole array operations, which is on display in this simple example source code. You can see how in the statement "c = a + b" (a, b, and c are arrays of 4 byte floats, each 100 elements long), every element in "a" will be added to every corresponding element in "b" to produce a vector "c" with 100 unique values. Without whole array operations, this would have to be done in a loop.

To compile, download and install the compiler, then paste the source code below into a text document with the extension ".f95". Compile at the command line with the following statement: g95 sourceFile.f95. This will produce an executable called a.exe.

  1. program array_operations
  2. !
  3. ! define variables
  4. !
  5. real(kind=4), dimension (100) :: a, b, c
  6. !
  7. ! fill a and b
  8. !
  9. do i=1,100
  10. a(i) = real(i)
  11. b(i) = 1./a(i)
  12. end do
  13. !
  14. ! now compute c as an array operation
  15. !
  16. c = a + b
  17. !
  18. do i=1,100
  19. print *, c(i)
  20. end do
  21. !
  22. stop
  23. !
  24. end program array_operations
  25.  
  26.  
  27.  
  28. !***Source code adapted from:
  29. !http://www.colostate.edu/~pburns/tue/arrays.f90.fm.html