Advertisement
Guest User

Untitled

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