CROSSREF Manual

FORTRAN
Sample Listing
1 PROGRAM SORT
2
3 C Program reads and sorts a file of up to 50 numbers.
4
5 INTEGER count, maxcount
6 REAL numbers
7 PARAMETER (maxcount=50)
8 COMMON count, numbers(maxcount)
9
10 OPEN (UNIT=2, FILE = 'datafile')
11 DO 100 count = 1, maxcount
12 READ (UNIT=2, FMT=901, END=200) numbers(count)
13 100 CONTINUE
14 C Check for too many values.
15 READ (UNIT=2, FMT=901, END=200) dummy
16 WRITE(4,FMT=902) maxcount
17 STOP
18 200 count = count - 1
19
20 CALL PRTNUMS ('Before sorting:')
21 CALL SORTNUMS (count, numbers)
22 CALL PRTNUMS ('After sorting:')
23
24 STOP
25 901 FORMAT (F6.2)
26 902 FORMAT (1X, 'Data file has too many values.', /,
27 + 1X, 'Maximum number of values for sort: ', I6)
28 END
29
30 SUBROUTINE SORTNUMS (icount, values)
31
32 C Sorts array (values) with icount elements in ascending order.
33
34 DIMENSION values(icount)
35
36 DO 600 i = icount-1, 1, -1
37 DO 500 j = 1, i
38 IF ( values(j) .LT. values(j+1) ) GOTO 500
39 temp = values(j)
40 values(j) = values(j+1)
41 values(j+1) = temp
42 500 CONTINUE
43 600 CONTINUE
44 RETURN
45 END
46
47 SUBROUTINE PRTNUMS (message)
48
49 C Prints message passed as parameter, numbers from common block.
50
51 CHARACTER message*(*)
52 INTEGER count, maxcount
53 REAL numbers
54 PARAMETER (maxcount=50)
55 COMMON count, numbers(maxcount)
56
57 WRITE (UNIT=4,FMT=100) message
58 WRITE (UNIT=4,FMT=200) (numbers(i), i = 1, count)
59 RETURN
60 100 FORMAT(/,1X,A,/)
61 200 FORMAT(1X,F6.2)
62 END
Figure 9-1. FORTRAN Sample Program
9-4