Program rainfall
!Purpose:
!This program calculates the total monthly rainfall and the average by reading in the values from an opened file.
IMPLICIT NONE
!List of variables:
CHARACTER(len = 20):: filename !Input file name
CHARACTER(len = 20)::month !Month to be read
INTEGER::n = 0 !Number of input values.
INTEGER:: ierror !Status flag of i/o statement
REAL:: x !An input data value
REAL:: sum_x = 0 ! Sum of input values
REAL:: x_bar = 0 !Average of input samples
!Prompt the user and get the name of file for input
WRITE(*,*)'enter the name of the file:'
READ(*,*) filename
!Prompt the user for the month
WRITE(*,*)' enter the month:'
READ(*,*) month
!Open the input file
OPEN (Unit = 3, FILE = filename, STATUS = 'OLD', ACTION = 'READ', IOSTAT = ierror)
!Check to see if the file opened successfully
errorcheck: IF (ierror > 0) THEN
WRITE(*,*) filename
ELSE
!file opened successfully. Read input values x from the file
DO
READ ( 3,*,iostat= ierror) x
IF ( ierror /= 0) EXIT
n = n + 1
sum_x = sum_x + x !Calculate sum
END DO
!Calculate the mean rainfall
x_bar = sum_x / real(n)
!Tell user
WRITE(*,*)'Number of data points = ',n
WRITE(*,*)'Monthly total = ', sum_x
WRITE(*,*)'Monthly average = ', x_bar
!Close input file
CLOSE(Unit = 8)
End if errorcheck
End program rainfall