




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Fortran programming for beginners students, Fortran programming as for PG students
Typology: Cheat Sheet
1 / 28
This page cannot be seen from the preview
Don't miss anything!
ForTran program A ForTran ( For mula Tran slation) program consists of a number of statements, each written in a separate line and represents a command to be performed by the computer. It may have comment lines (written with a C) and blank lines. A statement must skip the first six columns of the line (cannot start before the 7th^ column), except for statement numbers. The program is not case sensitive (i.e., it can be written in upper or lower cases or a mixture of both) but has to finish with a statement END. The following is a sample ForTran program written in upper case and mixed cases.
C******MAIN PROGRAM****** c******Main Program****** REAL SUM Real sum READ(,)N read(,)n SUM=0. sum=0. DO 10 I=1,N do i=1,N IF(I.GE.N/2)THEN If(i.ge.n/2)then SUM=SUM+I2 Sum=sum+I ELSE else SUM=SUM+I4 sum=sum+i ENDIF endif 10 CONTINUE 10 continue WRITE(,)SUM write(,)SUM END end
Constants (Data whose values cannot be changed during the program) Numeric Type (A string of digits, preceded by a single algebraic sign) Integer: 12 0 – 102 +127 (Not 12.0 – 2.5 5,234 5 – ) Real: 12.4 .1 102. +127.2 (Not 12 5,234.5) 3.27E-5 3.27E+10 3.269E8 -3.0E
‘CE-WRE (ECJ), Univ. of TX @ Austin’ ‘U. A. P.’ ‘Tom’s cabin’ Logical Type (.TRUE. .FALSE.)
Variables (Data whose values can be changed during the program) Can be: SUM1XY INSIDE SUM OF SOME (Not 1SUMXY IN-SIDE $SUM.OF+SOME READ END)
Type Specification INTEGER IX,JY,SUM REAL DIV1,SECT2,IMULT CHARACTER DAT,INS,OUTS or CHARACTER10 DAT,INS,OUTS12,NAMS* LOGICAL TRUE,FALSE
IMPLICIT Statement IMPLICIT REAL(A-H,O-Z) IMPLICIT INTEGER*3(I-N) Unless specified, variable names beginning with I-N are assumed integers and others real
Selected Functions
Intrinsic Functions Meanings Examples IABS, ABS Absolute value (^) IABS( 152) =152, ABS( 1.52) =1. MAX Maximum (^) MAX(1.,3.3, 2.5) = 3. MIN Minimum MIN(1.,3.3, 2.5) =-2. SQRT Square Root SQRT(4.41) = 4.41 = 2. EXP Exponential EXP(1.35) = e1.35= 3. LOG, LOG (ALOG, ALOG10) Log, Log^10 LOG(3.857) = 1.35, LOG10(3.857) = 0. SIN, ASIN, SINH Sin, Sin-^1 , Sinh SIN(1.) = 0.841, ASIN(1.) = / COS, ACOS, COSH Cos, Cos-^1 , Cosh COS(1.) = 0.540, ACOS(1.) = 0. TAN, ATAN, TANH Tan, Tan-^1 , Tanh TAN (1.) = 1.557, ATAN (1.) = / Besides .EQ. , .NE., .GT., .GE., .LT., .LE. are used for =, ,>, ,<, respectively
Mathematical Operations
Mathematical Expression ForTran Equivalent a + b/c – d A + B/C – D (a + b)/(c + d) (a + b)/(c + d) a^2 – b^2 A2 – B a/cd - b^2 A/(CD) – B* (3a^2 – b/c^2 ) SQRT(3A2^ –^ B/C2) cos(2x+y) + a^2 – b^2 + exy^ COS(2X+Y) + ABS( A2^ –^ B2) + EXP(XY) log 10 (a + 3y)^3 + loge (x + y) ALOG10( (A + 3Y)**3)+LOG (X + Y)
Examples ForTran Operation Result 9 – 6 + 3 6 32 + 4/5 9 32 + 4.0/5 9. 3 + 2.3 11. (3 + 2)3 125 (3 + 23)/5 2 (3 + 2.3)/5 2. 323 6561 (32)3 729
Logical Expressions and Operations A logical expression can be a logical constant, logical variable, a relation or a combination of these. The logical operators used are .NOT., .AND., .OR., .EQV. and .NEQV. meaning the following
Logical Operators Meanings Examples .NOT. Negation .NOT.TRUE. is .FALSE. .AND. Addition .TRUE.AND.FALSE. is .FALSE. .OR. Alternative .TRUE.OR.FALSE. is .TRUE.
. EQV. Equivalence FALSE.EQV.FALSE. is .TRUE. .NEQV. Non-Equivalence FALSE.NEQV.FALSE. is .FALSE.
The logical operations have equal precedence and hence are operated from left to right, but parentheses may be used to override this order. For example, if A = 1.0, B = 2.5 and C = 6. (i) A.GT.B.AND.AB.LE.C is FALSE.AND.TRUE; i.e., .FALSE. (ii) A.GT.B.OR.AB.LE.C is FALSE.OR.TRUE; i.e., .TRUE. (iii) NOT.A.GT.B.AND.AB.LE.C is TRUE.AND.TRUE; i.e., .TRUE. (iv) A.GT.B.AND.(A.LE.C.OR.B.NE.C).OR.NOT.(A.EQ.B) is FALSE.AND.(TRUE.OR.TRUE).OR.NOT.FALSE; or FALSE.AND.TRUE.OR.TRUE or FALSE.OR.TRUE.; i.e., .TRUE. GOTO Statement The GOTO (or GO TO) statement is used to direct the computer program to a specific statement by branching around one or more statements. The most common GOTO statement has the general form GOTO s [For example, GOTO 10] where s is the statement number of an executable statement. In the computer program, the next statement to be executed after the above is statement number s. In the example, the statement ‘GOTO 10’ transfers the program to statement number 10. Among other GOTO statements, the computed GOTO statement has the following form. GOTO (n1, n2……….nk), integer expression [For example, GOTO (10, 32, 15, 20),IJ+2] which shifts the program to statement number ni if the ‘integer expression’ is equal to i. If the ‘integer expression’ is not equal to any integer between 1 and k, the statement is not executed. In the example, the program shifts to statements 10, 32, 15 or 20 if (I*J+2) is equal to 1, 2, 3 or 4 respectively.
IF Statement The IF statement shifts control conditionally in a program. The three types of IF statements are
Path to Microsoft ForTran Start Programs ForTran Power Station 4.0 Microsoft Developer Studio (Or just double-click on the Microsoft Developer Studio icon on Desktop)
To create a Workspace
To create, type and save a file
To run a program
To close Microsoft ForTran
Problem # PRINT,'ENTER A,B,C' READ,A,B,C Using Logical IF Using Block IF IF(A.EQ.0)GOTO 3 IF(A.EQ.0)THEN X= C/B DET=BB 4AC PRINT,X IF(DET<0)GOTO 6 ELSE DET=BB 4AC X1=( B+SQRT(DET))/(2A) IF(DET<0)THEN X2=( B SQRT(DET))/(2A) PRINT,'ROOTS ARE IMAGINARY' PRINT,X1,X2 ELSE GOTO 7 X1=( B+SQRT(DET))/(2A) 6 PRINT,'ROOTS ARE IMAGINARY' X2=( B SQRT(DET))/(2A) GOTO 7 PRINT,X1,X 3 X= C/B ENDIF PRINT,X ENDIF 7 END END
Problem # REAL MT PRINT,'ENTER AT,CT,MT,FE' READ,AT,CT,MT,FE TOT=AT10/40+CT20/100+MT20/100+FE50/ PRINT*,'TOTAL IS',TOT
Using Logical IF Using Block IF IF(TOT>=90) PRINT,'GRADE IS A' IF(TOT>=90) THEN IF(TOT>=80.AND.TOT<90)PRINT,'GRADE IS B' PRINT,'GRADE IS A' IF(TOT>=70.AND.TOT<80)PRINT,'GRADE IS C' ELSE IF(TOT>=60.AND.TOT<70)PRINT,'GRADE IS D' IF(TOT>=80)THEN IF(TOT<60)PRINT,'GRADE IS F' PRINT,'GRADE IS B' ELSE END IF(TOT>=70)THEN PRINT,'GRADE IS C' ELSE IF(TOT>=60)THEN PRINT,'GRADE IS D' ELSE PRINT,'GRADE IS F' ENDIF ENDIF ENDIF ENDIF END
DO loops Although IF loops (combination of block IF and GOTO) can also perform repetitive operations, DO loops are used more often for this purpose.
x
a (L-2a) a
Write a program to calculate the height reached by a cricket ball after rebounding from ground. The concepts of impulse and momentum are used.
(weighing WA, WB) colliding at velocities vA1, vB1 and angles (^) A1, (^) B1 with the line of impact, if the coefficient of restitution is e [Refer to Example 315 of your Analytic Mechanics book].
(i) X1=1. (ii) DIMENSION F(10),X(10),Y(10),Z(10),RL(10) DO X=2,3 N= DY=FUN(X)–FUN(X1) DO I=2,N DX=X–X1 F(I)=10. SL=DY/DX Y(I)=2.+I PRINT,X,DY,DX,SL Z(I)=1. X1=X RL(I)=SQRT(X(I)2+Y(I)2+Z(I)2) ENDDO FX=FX+F(I)X(I)/RL(I) END FY=FY+F(I)Y(I)/RL(I) FZ=FZ+F(I)Z(I)/RL(I) FUNCTION FUN(Z) ENDDO FUN=TAN(Z)–LOG(Z)+EXP(–Z) R=SQRT(FX2+FY2+FZ**2) END PRINT 10, R,FX,FY,FZ 10 FORMAT(2X,F6.2,3(2X,F6.3)) END
In many applications of ForTran , it is desirable for the user to have complete control over the way the input data is read or the results are printed out. This may be applicable when reading organized data from input files rather than from screens and are particularly important when writing to output files where an organized output is frequently required. Input/Output Statements for Format-directed Input and Output The format-directed input and output statements are similar to the file input and output statements mentioned before but have a statement number instead of the * sign mentioned before. For example, to read the variable X from input file, the READ (input) statement may look like READ(1,70) X Here the number ‘1’ is the input file number mentioned in the OPEN statement before while the number ‘70’ refers to the FORMAT specification number to be used for the input. To print the variables I and J to an output file, the WRITE (output) statement would be WRITE(2,50) I, J The number ‘2’ is the output file number mentioned before and ‘50’ refers to the FORMAT specification number to be used for the output. As an alternative to their direct use in the READ and WRITE statements, file numbers can be integer variables whose values are assigned/calculated within the program before the corresponding statements. Format Statements The format specification statements referred from the READ and WRITE statements mentioned before can have several forms; e.g., I-format for integers, F, E-format for real data, A-format for characters, L-format for logical data, X-format for blanks, etc. Besides, there are format statements with G, H, apostrophe, slash and reusable formats. All of them will have the following general form s FORMAT (Specification) where s is the format statement number and ‘Specification’ is the way the input/output is to be arranged. The following specifications are shown for illustration.
(i) I-format (Iw) specification Specification Data Output I3 123 123 I5 123 øø I5 – 1234 – 1234 I3 – 1234 ***
(ii) F-format (Fw.d) specification Specification Data Output F10.3 123456.789 123456. F10.2 – 56.789 øøøø–56. F10.7 56.789 56. F10.5 123456.789 **********
(iii) E-format (Ew.d) specification Specification Data Output E14.8 123456.789 0.12345679E+ E10.2 – 56.789 ø–0.57E+ E10.5 56.789 ********** E15.5 0.00001234 øøøø0.12340E– 04
(iv) Examples of some other specifications 10 FORMAT(1X,I3) keeps 1 blank column, 3 columns for an integer data 30 FORMAT(2X,F10.3) keeps 2 blank columns, 10 columns for a real data (with 3 after decimal) 20 FORMAT(10(2X,I5)) keeps 10 sets, each with 2 blank columns and 5 columns for an integer data 15 FORMAT(‘TOTAL=’,I4) writes TOTAL= then keeps 4 columns for an integer data 15 FORMAT(I4//I5) keeps 4 columns for an integer, skips 2 lines then keeps 5 columns for an integer
Class Assignments
Roll No. Number 1 Number 2 Number 3 Number 4 Number 5 1 88.5 80.0 72.3 88.5 82. 2 76.2 61.7 72.4 89.1 47. 3 32.0 43.4 50.4 70.5 35. 4 90.5 87.0 70.7 100.0 77. 5 100.0 90.3 75.6 97.3 87. 6 55.9 57.8 43.0 75.2 55. 7 60.7 67.4 46.3 70.3 64. 8 40.0 50.7 41.20 60.0 48. 9 36.5 40.0 23.9 60.1 53. 10 56.7 65.0 45.4 66.7 57.
[Hint: Use OPEN statements to open input and output files. You can define 2 arrays IROLL(10) for Roll Numbers (integer) and FTN(10,7) (real numbers) for the Final and Total Numbers as well as Percentages. Use a DO-CONTINUE loop (for I=1,10) to add the Final Numbers for each student and print them to an output file.]
FUNCTION and SUBROUTINE These statements are used to perform certain operations outside the main program and are therefore called ‘Sub-programs’. They are useful in performing often repeated or widely used operations in large programs and in maintaining clarity and continuity of the ‘Main-programs’. The FUNCTION statement defines a ‘function’ that can be used in the main program. The following is an example of the FUNCTION statement. It defines a function PLUSSQ to calculate (A+B)**2 for various values of A and B and return the results to the main program.
C******MAIN PROGRAM*********************************** READ,X,Y SUM1=PLUSSQ(X,Y) SUM2=PLUSSQ(X,–2.Y) SUM3=PLUSSQ(SIN(X),Y2) SUM4=PLUSSQ(X,EXP(–Y)) SUM5=SUM1+SUM2+SUM3+SUM PRINT,SUM1,SUM2,SUM3,SUM4,SUM END C******FUNCTION PLUSSQ******************************** FUNCTION PLUSSQ(A,B) PLUSSQ=(A+B)* END
The SUBROUTINE statement does not represent a function, but performs operations that can help to define/redefine certain variables. It is invoked from the main program by the CALL statement. An example of the SUBROUTINE statement is shown below. Here, matrix SK and vector P are read in the main program from input file FUNSUB.IN, the set of equations [SK]{X}= {P} are solved using the SUBROUTINE GAUSS and the solution vector {X} is written as {P} in the screen.
C******MAIN PROGRAM**************************************** DIMENSION SK(990,990),P(990) OPEN(1,FILE='FUNSUB.IN',STATUS='OLD') READ(1,)NDF DO I=1,NDF READ(1,)(SK(I,J),J=1,NDF),P(I) ENDDO CALL GAUSS(SK,P,NDF) DO I=1,NDF PRINT*,P(I) ENDDO END C******GAUSS ELIMINATION*********************************** SUBROUTINE GAUSS(AG,BG,N) DIMENSION AG(990,990),BG(990)