Fortran 77 question 
Author Message
 Fortran 77 question

Cans someone tell me why I keep getting some crazy value(a ten digit
number) for the maxvalu in this program.

     PROGRAM REPORT

C     THIS PRGRAM GENERATES A REPORT FROM THE DIALY
C     PRODUCTION INFORMATION FOR A SET OF OIL WELLS.

      INTEGER MO, DA, YR, ID, N, I
      REAL OIL(7), TOTAL, AVE, INDAVE
      DATA N, TOTAL /0,0.0/
      OPEN (UNIT=12, FILE='WELLS',STATUS='OLD')
      OPEN(UNIT=11, FILE='REPORT',STATUS='NEW')
      READ(12,*) MO, DA, YR
      WRITE(11,*) 'OIL WELL PRODUCTION'
      WRITE(11,5) MO, DA, YR
    5 FORMAT(1X,'WEEK OF ',I2,'-',I2,'-',I2)
      WRITE(11,*)
      WRITE(11,*) 'WELL ID          AVERAGE PRODUCTION'
      WRITE(11,*) '                  (IN BARRELS)'
      READ (12,*) ID, (OIL(I), I=1,7)
   10 IF (ID.NE.99999) THEN
         INDAVE = AVE(OIL,7)
         MAX=MAXVAL(OIL,7)
         WRITE(11,15) ID, INDAVE, MAX
   15    FORMAT(1X,I5,12X,F6.2,6X,F6.2)
         N=N+1
         TOTAL=TOTAL+INDAVE
         READ(12,*) ID, (OIL(I),I=1,7)
         GO TO 10
      END IF
      WRITE(11,*)  
      WRITE(11,20) N, TOTAL/REAL(N)
   20 FORMAT (1X, 'OVERALL AVERAGE FOR ',I3,' WELLS IS ',F6.2)
      END

      REAL FUNCTION AVE(X,N)
C     THIS FUNCTION COMPUTES THE AVERAGE OF A REAL
C     ARRAY WITH N VALUES.
      INTEGER N, I
      REAL X(N), SUM
      SUM= 0.0
      DO 10 I=1,N
         SUM=SUM+X(I)
   10 CONTINUE
      AVE=SUM/REAL(N)
      RETURN
      END

      REAL FUNCTION MAXVAL(K,N)
C     THIS FUNCTION FINDS THE MAX VALULE OF A ROW IN AN ARRAY
C     WITH N VALUES.
      INTEGER N, I
      REAL K(N)
      MAX= K(1)
      DO 10 I=1,N
         IF(K(I).GT.MAX)    MAX=K(I)
   10 CONTINUE
      MAXVAL=MAX
      RETURN
      END

Input file

05 06 92
52      87    136   0     54    60    82    51
63      54    73    88    105   20    21    105
24      67    98    177   35    65    98    0
8       23    34    52    67    180   80    3
64      33    55    79    108   118   130   20
66      40    44    63    89    36    54    36
67      20    35    76    87    154   98    80
55      10    13    34    23    43    12    0
3       34    56    187   34    202   23    34
2       98    98    87    34    54    100   20
25      29    43    54    65    12    15    17
18      45    65    202   205   100   99    98
14      36    34    98    34    43    23    9
13      0     9     8     4     3     2     10
36      23    88    99    65    77    45    35
38      23    100   134   122   111   211   0
81      23    34    54    98    5     93    82
89      29    58    39    20    50    30    47
99      100   12    43    98    34    23    9
45      23    93    75    93    2     34    8
88      23    301   23    83    23    9     20
77      28    12    43    43    92    83    98
39      98    43    12    23    54    23    98
12      43    54    92    84    75    72    91
48      83    138   189   73    27    49    10
99999   0     0     0     0     0     0     0



Thu, 11 Nov 1999 03:00:00 GMT  
 Fortran 77 question


Quote:
>Cans someone tell me why I keep getting some crazy value(a ten digit
>number) for the maxvalu in this program.

>     PROGRAM REPORT

>C     THIS PRGRAM GENERATES A REPORT FROM THE DIALY
>C     PRODUCTION INFORMATION FOR A SET OF OIL WELLS.

snip

MAXVAL is typed REAL in the function definition & needs to be typed REAL
in MAIN too. Also you probably intended MAX to be REAL.
 Walt.



Fri, 12 Nov 1999 03:00:00 GMT  
 Fortran 77 question

Quote:

> Cans someone tell me why I keep getting some crazy value(a ten digit
> number) for the maxvalu in this program.

MAX and MAXVAL are simply not declared as real. Since the first letter
are within the I-N range, both are considered as integers.

Regards,

Jean Vezina



Fri, 12 Nov 1999 03:00:00 GMT  
 Fortran 77 question

Quote:

>Cans someone tell me why I keep getting some crazy value(a ten digit
>number) for the maxvalu in this program.

>     PROGRAM REPORT

>C     THIS PRGRAM GENERATES A REPORT FROM THE DIALY
>C     PRODUCTION INFORMATION FOR A SET OF OIL WELLS.

>      INTEGER MO, DA, YR, ID, N, I
>      REAL OIL(7), TOTAL, AVE, INDAVE
>      DATA N, TOTAL /0,0.0/
>      OPEN (UNIT=12, FILE='WELLS',STATUS='OLD')
>      OPEN(UNIT=11, FILE='REPORT',STATUS='NEW')
>      READ(12,*) MO, DA, YR
>      WRITE(11,*) 'OIL WELL PRODUCTION'
>      WRITE(11,5) MO, DA, YR
>    5 FORMAT(1X,'WEEK OF ',I2,'-',I2,'-',I2)
>      WRITE(11,*)
>      WRITE(11,*) 'WELL ID          AVERAGE PRODUCTION'
>      WRITE(11,*) '                  (IN BARRELS)'
>      READ (12,*) ID, (OIL(I), I=1,7)
>   10 IF (ID.NE.99999) THEN
>         INDAVE = AVE(OIL,7)
>         MAX=MAXVAL(OIL,7)
>         WRITE(11,15) ID, INDAVE, MAX
>   15    FORMAT(1X,I5,12X,F6.2,6X,F6.2)
>         N=N+1
>         TOTAL=TOTAL+INDAVE
>         READ(12,*) ID, (OIL(I),I=1,7)
>         GO TO 10
>      END IF
>      WRITE(11,*)  
>      WRITE(11,20) N, TOTAL/REAL(N)
>   20 FORMAT (1X, 'OVERALL AVERAGE FOR ',I3,' WELLS IS ',F6.2)
>      END

>      REAL FUNCTION AVE(X,N)
>C     THIS FUNCTION COMPUTES THE AVERAGE OF A REAL
>C     ARRAY WITH N VALUES.
>      INTEGER N, I
>      REAL X(N), SUM
>      SUM= 0.0
>      DO 10 I=1,N
>         SUM=SUM+X(I)
>   10 CONTINUE
>      AVE=SUM/REAL(N)
>      RETURN
>      END

>      REAL FUNCTION MAXVAL(K,N)
>C     THIS FUNCTION FINDS THE MAX VALULE OF A ROW IN AN ARRAY
>C     WITH N VALUES.
>      INTEGER N, I
>      REAL K(N)
>      MAX= K(1)
>      DO 10 I=1,N
>         IF(K(I).GT.MAX)    MAX=K(I)
>   10 CONTINUE
>      MAXVAL=MAX
>      RETURN
>      END

>Input file

>05 06 92
>52      87    136   0     54    60    82    51
>63      54    73    88    105   20    21    105

<snip>

The main problem seams to be that MAXVAL is not declared as real in the main
program but in the function subprogram. A compiler should warn about this if
the whole program is compiled as one file. Furthermore F6.2 in format 15 is
not allowed in order to edit the integer MAX.

In the function MAXVAL it seems that MAX is used as a real but is never
declared, thus assumed to be an integer and the result will be truncated if
the input data contains decimals (but perhaps this is what you want).

If you add REAL MAXVAL, MAX in the main and, if you don't want to truncate
the maximum value, REAL MAX in function MAXVAL the program should work.

BTW, _don't_ use intrinsic function names (as MAX) as variable names. It is
allowed but confusing. If your compiler have the extension IMPLICIT NONE it
is also a good idea to use it as some problems then is avoided.

Hope this helps,
Kurt


Department of Building Science, Lund Institute of Technology
Lund University, Sweden



Sat, 13 Nov 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Fortran 77 question

2. Fortran 77 question

3. Fortran 77 question

4. Fortran-77 question -- is this legal?

5. A Very Simple Fortran 77 question

6. Fortran 77 simple code question

7. some fortran 77 language question

8. Fortran 77 File I/O Write and Format Question

9. question about negative indices in fortran 77

10. question about negative indices in fortran 77

11. A few basic questions about FORTRAN 77 program

12. Question on FORtran 77 standard

 

 
Powered by phpBB® Forum Software