PROGRAM OCEAN ************************************************************************ * Program to find the average ocean depth in each half (separated by * * the diagonal) of a square section of the ocean. Identifiers used: * * DEPTH : a two-dimensional array of depth readings * * FNAME : name of the file containing depth readings * * LIMIT : limit on the size of DEPTH (parameter) * * N : the number of rows (or columns) * * I, J : subscripts * NSUM : the sum of the northern depths * * NAVE : the average of the northern depths * * SSUM : the sum of the southern depths * * SAVE : the average of the southern depths * * OSUM : the overall sum * * OAVE : the overall average * * HALF : number of elements in each half * * Note: It is assumed that the elements on the diagonal are * * included in the overall average but not in either half. * * * * Input: The elements of array DEPTH * * Output: The array DEPTH in table format, NAVE, SAVE, and OAVE * ************************************************************************ INTEGER LIMIT PARAMETER (LIMIT = 11) CHARACTER*20 FNAME INTEGER N, I, J, HALF REAL DEPTH(LIMIT,LIMIT), NSUM, NAVE, SSUM, SAVE, OSUM, OAVE * Get the name of the input file, open it for input PRINT *, 'ENTER NAME OF DATA FILE:' READ '(A)', FNAME OPEN (UNIT = 10, FILE = FNAME, STATUS = 'OLD') * Read N and the array DEPTH from the file; initialize sums to 0 READ (10,*) N, ((DEPTH(I,J), J = 1, N), I = 1, N) NSUM = 0.0 SSUM = 0.0 OSUM = 0.0 * Calculate the north, south, and overall sums DO 20 I = 1,N DO 10 J = 1, N IF (I .LT. J) THEN NSUM = NSUM + DEPTH(I,J) ELSE IF (I .GT. J) THEN SSUM = SSUM + DEPTH(I,J) END IF OSUM = OSUM + DEPTH(I,J) 10 CONTINUE 20 CONTINUE * Calculate the north, south, and overall average depths HALF = (N**2 - N) / 2 NAVE = NSUM / REAL(HALF) SAVE = SSUM / REAL(HALF) OAVE = OSUM / REAL(N**2) * Display the DEPTH array and the average depths PRINT 100 PRINT 110, ((DEPTH(I,J), J = 1, N), I = 1, N) 100 FORMAT(1X, T29, 'OCEAN DEPTHS') 110 FORMAT(/1X, 11F6.1) PRINT 120, NAVE, SAVE, OAVE 120 FORMAT(// 1X, 'NORTHERN HALF AVERAGE DEPTH', T30, F6.2, ' FEET', + // 1X, 'SOUTHERN HALF AVERAGE DEPTH', T30, F6.2, ' FEET', + // 1X, 'OVERALL AVERAGE DEPTH', T30, F6.2, ' FEET') END