Fortran mode

1
! Example Fortran code
2
  program average
3
 
4
  ! Read in some numbers and take the average
5
  ! As written, if there are no data points, an average of zero is returned
6
  ! While this may not be desired behavior, it keeps this example simple
7
 
8
  implicit none
9
 
10
  real, dimension(:), allocatable :: points
11
  integer                         :: number_of_points
12
  real                            :: average_points=0., positive_average=0., negative_average=0.
13
 
14
  write (*,*) "Input number of points to average:"
15
  read  (*,*) number_of_points
16
 
17
  allocate (points(number_of_points))
18
 
19
  write (*,*) "Enter the points to average:"
20
  read  (*,*) points
21
 
22
  ! Take the average by summing points and dividing by number_of_points
23
  if (number_of_points > 0) average_points = sum(points) / number_of_points
24
 
25
  ! Now form average over positive and negative points only
26
  if (count(points > 0.) > 0) then
27
     positive_average = sum(points, points > 0.) / count(points > 0.)
28
  end if
29
 
30
  if (count(points < 0.) > 0) then
31
     negative_average = sum(points, points < 0.) / count(points < 0.)
32
  end if
33
 
34
  deallocate (points)
35
 
36
  ! Print result to terminal
37
  write (*,'(a,g12.4)') 'Average = ', average_points
38
  write (*,'(a,g12.4)') 'Average of positive points = ', positive_average
39
  write (*,'(a,g12.4)') 'Average of negative points = ', negative_average
40
 
41
  end program average
42
 
 

MIME types defined: text/x-Fortran.