C This program prompts the user for the name of a data file which C contains homework scores. The first line of the file specifies, C in order, the number of assignments and the number of students. C The file is arranged such that all the first student's scores C appear on the first line, all of the second student's scores C appear on the second line, and so on. The program reads the C scores into a one-dimensional array that was declared with 2000 C elements. Subprograms treat the data as a two-dimensional array. C C After reading the scores the program reports the following: C * Mean and standard deviation of the entire set of scores. C * Lowest score, mean, highest score, and standard deviation for C each assignment. C * Lowest score, mean, highest score, and standard deviation for C each student. C C John Schneider C CptS 203, HW 9. C-------------------------------------------------------------------------- program grader implicit none integer nhw, nstd, i, j, size parameter (size=2000) real scores(size), m, stddev, low, high character filnam*50 print *, 'Enter the file name:' read *, filnam open(unit=12, file=filnam, status='old') C..... C Read number of assignments and student. Terminate program if too C many scores. Otherwise read data into a 1D array. C..... read(12,*) nhw, nstd if (nhw*nstd .gt. size) then print *, 'Too many scores. Total limit = ',size stop endif read(12,*)(scores(i),i=1,nhw*nstd) close(12) C.....Print the overall mean and standard deviation. call mean(scores,nhw,nstd,1,1,nhw,nstd,m,low,high) print *, 'Overall mean & std.dev.: ',m, & stddev(scores,nhw,nstd,1,1,nhw,nstd,m) C.....Print the low, mean, high and standard deviation for each homework. print *,'HW #, low, mean, high, & std. dev.' do i=1,nhw call mean(scores,nhw,nstd,i,1,i,nstd,m,low,high) print '(1x,i4,4(1x,f7.2))', & i, low, m, high, stddev(scores,nhw,nstd,i,1,i,nstd,m) enddo C.....Print the low, mean, high and standard deviation for each student. print *,'St #, low, mean, high, & std. dev.' do j=1,nstd call mean(scores,nhw,nstd,1,j,nhw,j,m,low,high) print '(1x,i4,4(1x,f7.2))', & j, low, m, high, stddev(scores,nhw,nstd,1,j,nhw,j,m) enddo stop end C**************************** end of main program *********************** C======================================================================== C Subroutine to calculate the mean of a rectangular section of a C 2D array. The high and low values over that section are also C found. The array is assumed to be "nrow" rows by "ncol" columns. C The subsection over which the mean is found has starting indices C (istart,jstart) and ending indices of (iend,jend). C..... subroutine mean(a,nrow,ncol,istart,jstart,iend,jend,m,low,high) implicit none integer i, j, istart, jstart, iend, jend, nrow, ncol real a(nrow,ncol), m, low, high C..... C Initialize mean ("m") and the low and high scores. The low and C high are set to the first value in the section being considered as C this will always be a valid starting point for further C comparisons. (Given that we're only considering homework scores, C initializing the low to 100.0 and the high to 0.0 would probably C work, but is not truly general.) C..... m = 0.0 low = a(istart,jstart) high = low do j=jstart,jend do i=istart,iend m = m + a(i,j) if (a(i,j) .lt. low) low = a(i,j) if (a(i,j) .gt. high) high = a(i,j) enddo enddo C.....Divide the sum of the elements by the number of elements to get mean. m = m/((iend-istart+1)*(jend-jstart+1)) return end C************************* end of subroutine mean *********************** C======================================================================== C Function to find the standard deviation of a rectangular section C of a 2D array. C..... real function stddev(a,nrow,ncol,istart,jstart,iend,jend,m) implicit none integer i, j, istart, jstart, iend, jend, nrow, ncol real a(nrow,ncol), m stddev=0.d0 do j=jstart,jend do i=istart,iend stddev = stddev + (a(i,j) - m)**2 enddo enddo stddev = sqrt(stddev/((iend-istart+1)*(jend-jstart+1))) return end C************************* end of function stddev ***********************