C This program reads in a list of real numbers. The number of C values entered is determined by the first value (assumed to be an C integer) which the user enters. The program reports the average C of the negative values, the average of the non-negatives values, C the minimum and maximum values, as well as the place in the list C where the minimum and maximum values occurred (i.e., the index for C these entries). C C John Schneider C CptS 203, HW 4, problem 3 C-------------------------------------------------------------------------- program avgdat implicit none C..... C min = minimum value C max = maximum value C sumneg = sum of the negative values C sumpos = sum of the non-negative values C x = used to hold the current entry C C i = dummy loop variable C n = the number of values to be entered C cntneg = count of the negative values C cntpos = count of the non-negative values C minpos = index for the minimum value C maxpos = index for the maximum value C..... real min, max, x, sumpos, sumneg integer i, n, cntpos, cntneg, maxpos, minpos print *, 'Enter the number of values.' read *, n C.....Read in first value and initialize variables. read *, x min = x max = x sumneg = 0.0 sumpos = 0.0 maxpos = 1 minpos = 1 C..... C Check if value is negative or not. If negative, add to sumneg and C initialize cntneg and cntpos to 1 and 0, respectively. Otherwise C add value to sumpos and initialize cntneg and cntpos to 0 and 1, C respectively. C..... if (x .ge. 0.0) then sumpos = sumpos + x cntneg = 0 cntpos = 1 else sumneg = sumneg + x cntneg = 1 cntpos = 0 endif C.....Loop over the rest of the data. do i=2,n read *, x if (x .ge. 0.0) then sumpos = sumpos + x cntpos = cntpos + 1 else sumneg = sumneg + x cntneg = cntneg + 1 endif C........ C Check if value is less then current minimum or greater than C current maximum. If so, reset min or max appropriately and C update index position holder. C........ if (x .lt. min) then min = x minpos = i elseif (x .gt. max) then max = x maxpos = i endif enddo C.....Print out averages, checking that counts are not zero. if (cntneg .eq. 0) then print *, 'There were no negative values.' else print *, 'Average of negative numbers: ', sumneg/cntneg endif if (cntpos .eq. 0) then print *, 'There were no non-negative values.' else print *, 'Average of non-negative numbers: ', sumpos/cntpos endif print *, 'Average of all numbers: ', & (sumneg+sumpos)/(cntneg+cntpos) print *, 'Minimum = ',min,': It was element ',minpos, ' in list.' print *, 'Maximum = ',max,': It was element ',maxpos, ' in list.' stop end