Fortran mode
x
1
! Example Fortran code2
program average3
4
! Read in some numbers and take the average5
! As written, if there are no data points, an average of zero is returned6
! While this may not be desired behavior, it keeps this example simple7
8
implicit none9
10
real, dimension(:), allocatable :: points11
integer :: number_of_points12
real :: average_points=0., positive_average=0., negative_average=0.13
14
write (*,*) "Input number of points to average:"15
read (*,*) number_of_points16
17
allocate (points(number_of_points))18
19
write (*,*) "Enter the points to average:"20
read (*,*) points21
22
! Take the average by summing points and dividing by number_of_points23
if (number_of_points > 0) average_points = sum(points) / number_of_points24
25
! Now form average over positive and negative points only26
if (count(points > 0.) > 0) then27
positive_average = sum(points, points > 0.) / count(points > 0.)28
end if29
30
if (count(points < 0.) > 0) then31
negative_average = sum(points, points < 0.) / count(points < 0.)32
end if33
34
deallocate (points)35
36
! Print result to terminal37
write (*,'(a,g12.4)') 'Average = ', average_points38
write (*,'(a,g12.4)') 'Average of positive points = ', positive_average39
write (*,'(a,g12.4)') 'Average of negative points = ', negative_average40
41
end program average42
MIME types defined: text/x-fortran.
