* Fortran Example-1 ** Formatting of Fortran77 statements * One statement per line in general. ** 1. Column-1: the comment indicator: * C or * ** 2. Column-6: the continuation indicator(any non-blank character) to * indicate that the current line is the continuation of the * previous lin when a long statement occupies more than one C line. C 3. Columns-2:5 Statement number 1-9999 C 4. Columns-7:72 Statement body C * Structure of a program. * 1. The main program and any number of (external) subprograms. * 2. They can be placed in any order regardless of call sequence. * 3. Each program unit (main program and each subprogram) must finish * with the END statement. * 4. The main program has no header. C 5. Each subprogram must start with a header which is either type function f-name (p1,p2,...,pn) subroutine s-name (p1,p2,...,pk) C Until Fortran90, it did not have * a. recursion * b. pointers * c. Derived types such as records * d. case (multiple selections) * exit (premature loop termination) like 'break' of java/C++ * cycle (transfer control to the bottom of a loop) * like 'continue' of Java. * e. Some matrix functions: ** Dot_Product ** MatMul ** transpose ** Sum ** MaxVal and MinVal **************************************************** ! Some Major Changes of Fortran90 ! 1. Free source form (No Source formatting) ! a. No special designated column positions like C-1 or C-6 ! b. '&' appearing at the end of a line is to indicate that ! the next line is a continuation line. ! c. A line now has 132 positions. ! d. Multiple statements on a line when separated by ';' ! as in: X=X+1; Call Sub(A,B); X=MatMul(Y,Z) ! 2. A new program unit: Module ! It can be used for specifying global data types (possibly derived) ! and variables and subprograms which other program units can access ! with the use of 'USE' statement which should be the first ! specification statement of any program unit. ! 3. Separation of external subprograms from internal subprograms. ! 4. The main program, each external subprogram (not contained in ! any other program unit) can have any number of internal ! subprograms. Each module can have any number of module subprograms ! and each module subprogram can have any number of internal subprograms. ! No internal subprogram can have any further (internal) subprogram. ! That is, the max nesting level of internal subprograms is just one. ! 5. Array expressions. ! Arrays can appear in many expressions just like a scalar. ! ArrayB = ArrayA + 1.0 ! ArrX = ArrY * ArrZ ! ArrX(:, K:L) = ArrY(M, :, K+2:L+2) ! ArrX = Transpose(ArrY) ! ArrX = MatMul(ArrA, ArrB) ! 6. Recursive functions Recursive integer Function Fact(N) RESULT(Fa) ! RESULT Clause is needed as the function name is to be used ! in recursive calls and hence is not to be used in calculating ! the function value. 'Fa' is to be used for this latter purpose. integer N, Fa Fa=1 If (N .LE. 1) Return Fa = N * Fact(N-1) End function Fact C COSC3308 FORTRAN EXAMPLE-1 C Combination Calculation C Subroutine Printing Integer Comb(300,9), Many, Count Common Comb, Many, Count C Subroutine body follows write (99,105) Many, count !!! create a data file named FOR099.DAT 100 Format ('1# of values==', I4 /'0# of combinations==', I4/) 105 Format (' # OF VALUES==', I4 /' # OF COMBINATIONS==', I4/) Do 200 i = 1, Count write(99, 300) ( comb(i,j), j=1, many) ** Write (6, 300) ( Comb(i,j), j=1,Many) 300 FORMAT (' ', 9I4) 200 Continue End ! End of subroutine Printing Subroutine Combinations C CALCULATING COMBINATIONS OF NUMBERS, UP TO 9 NUMBERS C IMPLICIT INTEGER (A-Z) INTEGER COMB(300,9) LOGICAL MORE COMMON COMB,MANY,COUNT COUNT=1 DO 100 M=1, MANY 100 COMB(COUNT,M)=M ! THE FIRST COMBINATION: 1,2,3,...,MANY C C SET UP AN INFINITE LOOP IN WHICH TO PICK UP THE NEXT COMBINATION C MORE = .TRUE. DO WHILE (MORE) MORE= .FALSE. !!! ASSUME THAT NO MORE COMBINATION IS TO BE FOUND !!! DO 200 M=MANY,1,-1 IF (COMB(COUNT,M) .LT. 9-MANY+M) Then MORE = .true. Last = M exit End if 200 CONTINUE IF (MORE) THEN COUNT=COUNT+1 DO 500 N=1, Last-1 500 COMB(COUNT,N)=COMB(COUNT-1,N) KEEP=COMB(COUNT-1,Last) DO 600 N=Last,MANY KEEP=KEEP+1 600 COMB(COUNT,N) = KEEP END IF ! END OF IF (MORE) END DO ! END OF THE INFINITE LOOP !!!!!!!!!!!!! RETURN END CCC Main program left out for you to figure out. CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCC Outputs of the program # OF VALUES== 2 # OF COMBINATIONS== 36 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 3 2 4 2 5 2 6 2 7 2 8 2 9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