Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Fortran programming for beginners, Cheat Sheet of Physics

Fortran programming for beginners students, Fortran programming as for PG students

Typology: Cheat Sheet

2022/2023

Uploaded on 12/16/2023

prakhar-tripathi-4
prakhar-tripathi-4 🇮🇳

3 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ForTran program
A ForTran (Formula Translation) 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+I**2 Sum=sum+I**2
ELSE else
SUM=SUM+I**4 sum=sum+i**4
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.0E8
Character Type (A string of characters enclosed within apostrophes)
‘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 CHARACTER*10 DAT,INS,OUTS*12,NAMS*3
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Fortran programming for beginners and more Cheat Sheet Physics in PDF only on Docsity!

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

Character Type (A string of characters enclosed within apostrophes)

‘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

Selective Structure

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

  1. Logical IF Statement IF(logical expression) statement [For example, IF(I.GT.2.AND.I.LT.10) SUM=SUM+1] The logical IF statement conditionally executes a ‘statement’ if a particular ‘logical expression’ is true. In the example, the value of SUM is replaced by SUM+1 if I is greater than 2 and less than 10.
  2. Arithmetic IF Statement IF(arithmetic expression) s1, s2, s3 [For example, IF(3*x+2) 10, 30, 25] The arithmetic IF statement transfers control to s1, or s2 or s3 if the ‘arithmetic expression’ is less than, or equal to or greater than zero. The arithmetic IF statement is comparatively less used.
  3. Block IF Statement IF(logical expression) THEN [For example, IF(I.GT.2.AND.I.LT.10) THEN ……………………………… SUM=SUM+ .…………………………….. PRINT,SUM ELSE ELSE ……………………………… SUM=SUM+I ENDIF ENDIF ] The block IF statement conditionally executes a number of statements if a particular ‘logical expression’ is true and another set of statements if it is false. In the example, if I is greater than 2 and less than 10, the value of the variable SUM is replaced by the value of SUM+1 and the value of SUM printed on screen. If the condition is not satisfied (i.e., I 2 or I 10) the value of SUM is replaced by SUM+I. The following are some significant features of the block IF statement. (i) The ELSE statement is not essential in the block IF statement; i.e., when there is no operation under ELSE, it may contain IF-THEN-ENDIF only; e.g., the following programs are equivalent. IF(I.GT.2.AND.I.LT.10) THEN IF(I.GT.2.AND.I.LT.10) THEN SUM=SUM+1 SUM=SUM+ PRINT,SUM PRINT,SUM ENDIF ELSE ENDIF (ii) The block IF statement may contain any executable statement, including several other block IF statements (used in the forms of nested block IF or ELSEIF). IF(I.GT.2.AND.I.LT.10) THEN IF(I.GT.2.AND.I.LT.10) THEN SUM=SUM+1 SUM=SUM+ PRINT,SUM PRINT*,SUM ELSE ELSEIF(I.LE.2.OR.I.GE.10) THEN IF(I.LE.2.OR.I.GE.10) THEN SUM=SUM+I SUM=SUM+I ELSE ENDIF ENDIF ENDIF

Writing a ForTran90 program in Microsoft ForTran

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

  1. Close ‘Tip of the Day’
  2. File New
  3. Project Workspace OK
  4. Name (***) Create

To create, type and save a file

  1. File New
  2. Text File OK
  3. Tab (Adjust to 7th^ Column)
  4. Type the program
  5. Save Name (***. for) Save

To run a program

  1. Build Compile ***.for
  2. Build Build ***.exe
  3. Build Execute ***.exe

To close Microsoft ForTran

  1. File Close Workspace
  2. File Exit

Solution of Class Assignments

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

Repetitive Structure

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.

  1. DO loop or DO-CONTINUE loop This statement is used to perform repetitive works. It has a standard form like DO s u= us,ut,ui For example, DO 11 X=4.,10.,0. ……………………………… Y= X2+ 20.X .…………………………….. PRINT,X,Y s CONTINUE 11 CONTINUE where s (= 11) is a statement reference number, u (= X) is the DO control variable and us,ut,ui (= 4.,10.,0.2) are values of u that control the DO loop. us is the starting value, ut the terminal value and ui the incremental value of u. The example mentioned above assigns values of X ranging between 4. and 10. (increment 0.2) and defines variable Y= X^2 +20X. The PRINT statement in the next line writes both X and Y to the screen. The DO-CONTINUE loop ensures that this is done for each value of X. The following are some significant features of the DO-CONTINUE loop. (i) The values of us,ut and ui can be integer, real, zero, positive, negative or assigned numbers. Moreover if ui =1, it does not need to be written. The following are all valid forms of DO statement. DO 5 I=1, 15, 2 DO 10 I=1, 5 (meaning DO 10 I=1, 5, 1) DO 12 X = – 10., – 20., – 5. (But DO 12 X = – 10., – 20., 5. is not valid because it never ends) DO 10 X = A, A+20B, B/ (ii) It is not necessary to write CONTINUE at the end of a DO-loop; i.e., the example above can also be written as below. However, the CONTINUE statement helps to locate the end of the loop. DO 11 X=4.,10.,0. Y= X2+ 20.X 11 PRINT*,X,Y (iii) The control variable cannot be changed within the loop; i.e., the following loop is not valid DO 10 I=0, 5 10 I= (iv) Statements within the loop can be any valid operations, including more DO-CONTINUE loops.
  2. DO-ENDDO loop The DO-loop can also be written in the following general form, called DO-ENDDO loop. It avoids writing statement numbers, but otherwise its basic properties are similar to the DO-CONTINUE loop. DO u= us,ut,ui For example, DO X=4.,10.,0. ……………………………… Y= X2+ 20.X .…………………………….. PRINT,X,Y ENDDO ENDDO

Class Assignments

  1. Write a program that reads a number and writes on the screen if it is a prime number or not.
  2. Write a program to obtain the greatest and the smallest of n integer numbers and print them on screen.
  3. Write a program to generate the first n terms of the Fibonacci series (1, 2, 3, 5, 8, 13…….).
  4. Use the DO loop to calculate the summations of the following series for given values of x and n (i) 4 – 4/3 + 4/5 – 4/7…………….. nth^ term, (ii) 1 + x/1! + x^2 /2! + x^3 /3! +…………….. + xn/n! (iii) 1 x^2 /2! + x^4 /4! …………….. + ( x^2 )n/2n! (iv) x x^3 /3! + x^5 /5! …………….. + ( 1)n^ x2n+1/(2n+1)! Also compare the result with (i) the constant , (ii) ex, (iii) cos(x) and (iv) sin(x) respectively.
  5. Write a program that reads a set of numbers x 1 , x 2 , x 3 ,………, xn and calculates their arithmetic mean, geometric mean, standard deviation, skewness and kurtosis.
  6. The Bending Moment Diagram of a beam is shown below. Write a program to read P, a, L and calculate bending moments at the interval of L/20. M Maximum Bending stress = Mc/I Pa Pa

x

a (L-2a) a

  1. Write a program to evaluate the integral f(x)dx = [5 tan-1(x)–e–2 Cos x]dx between x = 0 and x =1 using (i) the Trapezoidal rule and (ii) Simpson’s Rule.
  2. Write a program to calculate the total score and percentage of grades obtained by each of n number of students in m class tests.

Lab Assignment 4

Write a program to calculate the height reached by a cricket ball after rebounding from ground. The concepts of impulse and momentum are used.

  1. Read the height of fall h 1 and height of rebound h 2 [e.g., h 1 = 30 , h 2 = 12 ]
  2. Calculate coefficient of restitution e = (h 2 /h 1 )
  3. Read the height of delivery y 1 , velocity u, angle of delivery and gravitational acceleration g [e.g., y 1 = 10 , u = 120 /sec, = 15 , g = 32.2 /sec 2 ]
  4. Calculate S = u^2 sin cos /g, and length of first bounce, x 1 = – S + (S^2 + 2u^2 y 1 cos^2 /g)
  5. Read the total length of pitch x [e.g., x = 60 ]
  6. (i) If x 1 x, print ‘Full-tossed’ (ii) Otherwise calculate distance x 2 = x–x 1 , vertical velocity of impact vy = (u^2 sin^2 +2 gy 1 ), horizontal velocity of impact vx = u cos , slope of impact s 1 = vy/vx Calculate height y 2 = e x 2 s 1 – g x 22 /(2u^2 cos^2 ), velocity w = (vx^2 + (evy) 2 – 2 gy 2 ) Print x 1 , x 2 , y 2 and w. Also, if (a) y 2 1 , print ‘Yorker’, (b) 1 y 2 2 , print ‘Over-pitched’, (c) 2 y 2 3.5 , print ‘Good-length’, (d) 3.5 y 2 , print ‘Short-pitched’

Home Assignment 4

  1. Write a program to calculate the rebound velocities vA2, vB2 and angles (^) A2, (^) B2 of two smooth spheres

(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].

  1. Write a program to perform the shear design of rectangular RC sections by the Working Stress Method using the following procedure. The units used for the calculations are kips, ksi, inch etc. 1. Read material properties f (^) c, fs [e.g., f (^) c = 2.5 ksi, fs = 18 ksi] and replace f (^) c by f (^) c/ 2. Read sectional properties b, d, As [e.g., b = 10 , d = 12.5 , As = 0.22 in^2 ] 3. Calculate Vc (max) = 5 f (^) c bd 4. Read design shear force V [e.g. V = 15 kips] 5. (i) If V Vc (max), print ‘Change the cross-section’ (ii) Otherwise, calculate Vc = 1.1 f (^) c bd, design spacing, S(req) = As fs d/(V–Vc), Vc0 = 3 f (^) c bd (a) If V Vc0, the stirrup spacing (S) should be the minimum of S(req) , d/4, 12 , As/0.0015b (b) If V Vc0, but V Vc, S should be the minimum of S(req) , d/2, 24 As/0.0015b (c) If V Vc, S should be the minimum of d/2, 24 and As/0.0015b Print S, As

Practice Problems

  1. For the computer programs shown below, write the outputs in the screen.

(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

  1. The horizontal (x) and vertical (y) distances traveled by a cricket ball are given by x = ut cos ……….(1) y = ut sin – gt^2 /2 ……….(2) Write a FORTRAN program to do the following, (i) Read u, , g and x from input file. (ii) Calculate t from equation (1), y from equation (2) and write t and y to the screen. (iii) Write (to output file) ‘Big Six’ if y 8, ‘Maybe Six’ if 0 y 8, ‘Not Six’ otherwise.
  2. The following FORTRAN program is written to calculate the cumulative total price of 4 items, which cost Tk. 60.5, 13.25, 87.0 and 55.5 respectively. Complete the program with OPEN statements and also write the input and output files. ………………………. ………………………. READ(2,)NITEM DO I=1,NITEM READ(2,)COSTI CSUMI=CSUMI+COSTI IF(I.EQ.NITEM)THEN WRITE(1,)’TOTAL COST IS = TK.’, CSUMI ELSE WRITE(1,)I, CSUMI ENDIF ENDDO END

Format-directed Input and Output

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

  1. The following are the Final Numbers obtained in various courses by different students in a class. (i) Read the Roll Numbers and the Final Numbers from an Input File, (ii) Add them to obtain the Total number for each student. If the full mark in each exam is 100, calculate the Percentage of Marks obtained by each student. (iii) Print the Roll Number, Final Numbers, Total Number and Percentage of Mark of each student to an Output File.

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.]

  1. (i) Read the following matrices (A and B) from an Input File. (ii) Multiply them (iii) Write the product matrix C to an Output File. A = 1.0 2.5 3.2 B = 3.0 2. 3.1 – 2.4 1.9 0.0 – 1. 1.2 0. [Hint: If A is a (L M) matrix and B is a (M N) matrix, C will be a (L N) matrix. C(i,j) = A(i,k) B(k,j); where is a summation for k =1 to M]

Subprograms

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)