143x Filetype PDF File size 0.09 MB Source: uomustansiriyah.edu.iq
Fortran Programming Lect.5 2nd-2019-2020 Example:: Write a program from the following series and print (S) where the difference between any two term is (5*10−5)as absolute value: 3 2 πxx 4 2πxx 6 3πxx 8 4πxx s =1− cos + cos − cos + cos −…… 3 2 5 2 7 2 9 2 sol: Real I,J Read *,x y=1.0; k=-1;J=2;I=1 Z=(3.14*X)/2.0 10 T=J/(J+1) *COS(I*Z) IF(ABS(T).LE.5E-5) GO TO 30 Y=Y+K*T K=-K I=I+1 J=J+2 GO TO 10 30 S=Y**(1.0/3.0) WRITE(*,20) X,S 20 FORMAT (2X,F10.4,F10.4) STOP END EXAMPLE: Write a program from the following series and print (S) where the maximum of term is (2*10-3 −5)as absolute value: 10 Fortran Programming Lect.5 2nd-2019-2020 7 5 3 x sin10πxx x sin7πxx x sin 4πxx s=6− + − +……………… 2 2∗4 2∗4∗6 sol: L=10 ;J=7;S=6.0;K=-1;N=2 READ *,X Z=3.14*X 20 F=1;I=2 10 IF(I.LE.N) THEN F=F*I I=I+2 GO TO 10 END IF T=X**J*SIN(L*Z)/F IF (ABS(T).LE.2E-3) GO TO 30 S=S+K*T K=-K L=L-3 N=N+2 GO TO 20 30 WRITE(*,40) X,S 40 FORMAT(5X,'X=',F8.3,/,5X,'S=',E16.8) STOP END Fortran Programming Lect.5 2nd-2019-2020 8. Loops For repeated execution of similar things, loops are used. If you are familiar with other programming languages you have probably heard about for-loops, while-loops, and until-loops. 8.1 Do-loops The do-loop is used for simple counting. Here is a simple example that prints the cumulative sums of the integers from 1 through n (assume n has been assigned a value elsewhere): integer i, n, sum sum = 0 do 10 i = 1, n sum = sum + i write(*,*) 'i =', i write(*,*) 'sum =', sum 10 continue The numerical value of statement labels have no significance, so any integer numbers can be used. Typically, most programmers increment labels by 10 at a time. The variable defined in the do-statement is incremented by 1 by default. However, you can define any other integer to be the step. This program segment prints the even numbers between 1 and 10 in decreasing order: integer i do 20 i = 10, 1, -2 write(*,*) 'i =', i Fortran Programming Lect.5 2nd-2019-2020 20 continue The general form of the do loop is as follows: do label var = expr1, expr2, expr3 statements label continue var is the loop variable (often called the loop index) which must be integer. expr1 specifies the initial value of var, expr2 is the terminating bound, and expr3 is the increment (step). Note: The do-loop variable must never be changed by other statements within the loop! This will cause great confusion.
no reviews yet
Please Login to review.