File: sect7.html

package info (click to toggle)
ftnchek 2.11.2-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,392 kB
  • ctags: 2,790
  • sloc: ansic: 21,570; fortran: 2,921; yacc: 2,794; sh: 1,623; makefile: 693; lisp: 264; awk: 163
file content (277 lines) | stat: -rw-r--r-- 8,386 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<HTML>
<HEAD>
<TITLE>an Example</TITLE>
</HEAD>
<BODY>
<A HREF="toc.html">Table of Contents</A><P>
<P>Previous: <A HREF="sect6.html">Using Project  Files</A><HR><P>
<H2><A NAME="sect7" HREF="toc.html#toc7">an Example </A></H2>
The following simple Fortran program illustrates the messages 
given by <B>ftnchek </B>. The program is intended to accept an array of test scores 
and then compute the average for the series. <P>
 <BR>
 <PRE>
  
 C       AUTHORS: MIKE MYERS AND LUCIA SPAGNUOLO 
 C       DATE:    MAY 8, 1989 
 
 C       Variables: 
 C               SCORE -&gt; an array of test scores 
 C               SUM -&gt;   sum of the test scores 
 C               COUNT -&gt; counter of scores read in 
 C               I -&gt;     loop counter 
 
         REAL FUNCTION COMPAV(SCORE,COUNT) 
             INTEGER SUM,COUNT,J,SCORE(5) 
 
             DO 30 I = 1,COUNT 
                 SUM = SUM + SCORE(I) 
 30          CONTINUE 
             COMPAV = SUM/COUNT 
         END 
 
 
         PROGRAM AVENUM 
 C 
 C                       MAIN PROGRAM 
 C 
 C       AUTHOR:   LOIS BIGBIE 
 C       DATE:     MAY 15, 1990 
 C 
 C       Variables: 
 C               MAXNOS -&gt; maximum number of input values 
 C               NUMS    -&gt; an array of numbers 
 C               COUNT   -&gt; exact number of input values 
 C               AVG     -&gt; average returned by COMPAV 
 C               I       -&gt; loop counter 
 C 
 
             PARAMETER(MAXNOS=5) 
             INTEGER I, COUNT 
             REAL NUMS(MAXNOS), AVG 
             COUNT = 0 
             DO 80 I = 1,MAXNOS 
                 READ (5,*,END=100) NUMS(I) 
                 COUNT = COUNT + 1 
 80          CONTINUE 
 100         AVG = COMPAV(NUMS, COUNT) 
         END 
    
  </PRE><P>
The compiler gives no error messages when this program is compiled. Yet 
here is what happens when it is run: <P>
 <BR>
 <PRE>
  
 $ run average 
 70 
 90 
 85 
 &lt;EOF&gt; 
 $ 
   
  </PRE><P>
What happened?  Why didn't the program do anything? The following is 
the output from <B>ftnchek </B> when it is used to debug the above program: <P>
 
<P>
 <BR>
 <PRE>
  
 
 $ ftnchek <A HREF="list.html">-list</A> <A HREF="symtab.html">-symtab</A> average 
  
 FTNCHEK Version 2.11 July 1999 
 
 File average.f: 
 
       1 C       AUTHORS: MIKE MYERS AND LUCIA SPAGNUOLO 
       2 C       DATE:    MAY 8, 1989 
       3  
       4 C       Variables: 
       5 C               SCORE -&gt; an array of test scores 
       6 C               SUM -&gt;   sum of the test scores 
       7 C               COUNT -&gt; counter of scores read in 
       8 C               I -&gt;     loop counter 
       9  
      10         REAL FUNCTION COMPAV(SCORE,COUNT) 
      11             INTEGER SUM,COUNT,J,SCORE(5) 
      12  
      13             DO 30 I = 1,COUNT 
      14                 SUM = SUM + SCORE(I) 
      15 30          CONTINUE 
      16             COMPAV = SUM/COUNT 
                            ^ 
 Warning near line 16 col 20: integer quotient expr converted to real 

      17         END 
      18  
 
 Module COMPAV: func: real 
 
 Variables: 
   
       Name Type Dims     Name Type Dims     Name Type Dims     Name Type 
Dims 
     COMPAV real         COUNT intg             I intg*            J intg 
   
      SCORE intg  1        SUM intg    
 
 * Variable not declared. Type has been implicitly defined. 
 
 
 Warning: Variables declared but never referenced: 
          J          
 
 Warning: Variables may be used before set: 
        SUM          
 
 
      19  
      20         PROGRAM AVENUM 
      21 C 
      22 C                       MAIN PROGRAM 
      23 C 
      24 C       AUTHOR:   LOIS BIGBIE 
      25 C       DATE:     MAY 15, 1990 
      26 C 
      27 C       Variables: 
      28 C               MAXNOS -&gt; maximum number of input values 
      29 C               NUMS    -&gt; an array of numbers 
      30 C               COUNT   -&gt; exact number of input values 
      31 C               AVG     -&gt; average returned by COMPAV 
      32 C               I       -&gt; loop counter 
      33 C 
      34  
      35             PARAMETER(MAXNOS=5) 
      36             INTEGER I, COUNT 
      37             REAL NUMS(MAXNOS), AVG 
      38             COUNT = 0 
      39             DO 80 I = 1,MAXNOS 
      40                 READ (5,*,END=100) NUMS(I) 
      41                 COUNT = COUNT + 1 
      42 80          CONTINUE 
      43 100         AVG = COMPAV(NUMS, COUNT) 
      44         END 
 
 Module AVENUM: prog 
 
 External subprograms referenced: 
 
     COMPAV: real*   
 
 Variables: 
   
       Name Type Dims     Name Type Dims     Name Type Dims     Name Type 
Dims 
        AVG real         COUNT intg             I intg        MAXNOS intg* 
  
       NUMS real  1 
 
 * Variable not declared. Type has been implicitly defined. 
 
 
 Warning: Variables set but never used: 
        AVG          
 
 
 
  0 syntax errors detected in file average.f 
  6 warnings issued in file average.f 
 
 Subprogram COMPAV:  argument data type mismatch 
   at position 1: 
     Dummy arg SCORE is type intg  in module COMPAV line 10 file average.f 

     Actual arg NUMS is type real  in module AVENUM line 43 file average.f 

    
  </PRE><P>
According to <B>ftnchek </B>, the program contains variables which may be used 
before they are assigned an initial value, and variables which are not 
needed.  <B>ftnchek </B> also warns the user that an integer quotient has been 
converted to a real. This may assist the user in catching an unintended 
roundoff error.  Since the <B><A HREF="symtab.html">-symtab</A> </B> flag was given, <B>ftnchek </B> prints out 
a table containing identifiers from the local module and their corresponding 
datatype and number of dimensions.  Finally, <B>ftnchek </B> warns that the function 
COMPAV  is not used with the proper type of arguments. <P>
With <B>ftnchek </B>'s help, 
we can debug the program.  We can see that there were the following errors: 

<OL>
<LI>SUM  and COUNT  should have been converted to real before doing the division. 
</LI><LI>SUM  should have been initialized to 0 before entering the loop. </LI><LI>AVG  was 
never printed out after being calculated. </LI><LI>NUMS  should have been declared 
INTEGER  instead of REAL . </LI>
</OL>
<P>
We also see that I , not J , should have been 
declared INTEGER  in function COMPAV . Also, MAXNOS  was not declared as 
INTEGER , nor COMPAV  as REAL , in program AVENUM .  These are not errors, 
but they may indicate carelessness.  As it happened, the default type of 
these variables coincided with the intended type. <P>
Here is the corrected 
program, and its output when run: <P>
 <BR>
 <PRE>
  
 C       AUTHORS: MIKE MYERS AND LUCIA SPAGNUOLO 
 C       DATE:    MAY 8, 1989 
 C 
 C       Variables: 
 C               SCORE -&gt; an array of test scores 
 C               SUM -&gt;   sum of the test scores 
 C               COUNT -&gt; counter of scores read in 
 C               I -&gt;     loop counter 
 C 
        REAL FUNCTION COMPAV(SCORE,COUNT) 
             INTEGER SUM,COUNT,I,SCORE(5) 
 C 
             SUM = 0 
             DO 30 I = 1,COUNT 
                 SUM = SUM + SCORE(I) 
 30          CONTINUE 
             COMPAV = FLOAT(SUM)/FLOAT(COUNT) 
         END 
 C 
 C 
         PROGRAM AVENUM 
 C 
 C                       MAIN PROGRAM 
 C 
 C       AUTHOR:   LOIS BIGBIE 
 C       DATE:     MAY 15, 1990 
 C 
 C       Variables: 
 C               MAXNOS -&gt; maximum number of input values 
 C               NUMS    -&gt; an array of numbers 
 C               COUNT   -&gt; exact number of input values 
 C               AVG     -&gt; average returned by COMPAV 
 C               I       -&gt; loop counter 
 C 
 C 
             INTEGER MAXNOS 
             PARAMETER(MAXNOS=5) 
             INTEGER I, NUMS(MAXNOS), COUNT 
             REAL AVG,COMPAV 
             COUNT = 0 
             DO 80 I = 1,MAXNOS 
                 READ (5,*,END=100) NUMS(I) 
                 COUNT = COUNT + 1 
 80          CONTINUE 
 100         AVG = COMPAV(NUMS, COUNT) 
             WRITE(6,*) 'AVERAGE =',AVG 
         END 
  $ run average 
 70 
 90 
 85 
 &lt;EOF&gt; 
 AVERAGE =   81.66666 
 $ 
   
  </PRE><P>
With <B>ftnchek </B>'s help, our program is a success! <P>
  
<P><HR><P>Next: <A HREF="sect8.html">Interpreting the Output  </A>
</BODY></HTML>