Fortran Lecture
Fortran Lecture
Fortran Lecture
The first higher level language to gain widespread acceptance was FORTRAN.
It was developed at IBM (International Business Machine) by John Backus
and his team.
Fortran is a general purpose programming language, mainly intended for
mathematical computations in e.g. engineering. Fortran is an acronym for
FORmula TRANslation, and was originally capitalized as FORTRAN.
However, following the current trend to only capitalize the first letter in
acronyms, we will call it Fortran.
By convention, a Fortran version is denoted by the last two digits of the year
the standard was proposed. Thus we have:
Fortran 66
Fortran 77
Fortran 90 (95)
The most common Fortran version today is still Fortran 77, although Fortran
90 is growing in popularity. Fortran 95 is a revised version of Fortran 90.
Why will we learn Fortran?
Fortran is the dominant programming language used in
engineering applications. It is therefore important for engineering
graduates to be able to read and modify Fortran code. From time
to time, so-called experts predict that Fortran will rapidly fade in
popularity and soon become extinct. These predictions have
always failed. Fortran is the most enduring computer
programming language in history.
Fortran is especially suited to numeric computing and scientific
computing.
Fortran came to dominate thin area of programming early on and
has been in continual use for over half a century in
computationally intensive areas such as numerical weather
prediction, finite element analysis, computational fluid
dynamics, computational physics, and computational
chemistry.
Problem Solving Steps
The following steps which are usually identified in
the computer-aided problem-solving process are
given below:
1. Problem analysis
2. Algorithm development [(PDL), Pseudocode]
3. Program coding
4. Program compilation and execution
5. Program debugging and testing
6. Program documentation
Answer Please!
• What is an algorithm? What are the essential characteristics
and features of a good algorithm?
• What is a flow chart? List the various commonly used flow
chart symbols with their significance.
• Difference between Algorithm and flowchart.
• Draw the layout of a FORTRAN 77 coding sheet.
• Discuss the use of various columns in the FORTRAN
coding sheet.
• Explain the difference between compilation and execution.
• Briefly describe four levels of debugging or errors detected
in a program coding statement.
Algorithm Development
• Algorithm: A step by step procedure that must be
followed in solving a particular problem on computer
is called algorithm.
• Flowchart: A graphical representation of a specific
sequence of steps of an algorithm.
• Symbols: 1. Terminal 2. Input/output 3. Computation
or process 4. Flow line 5. Decision 6. Connector
7. Loop structure 8. Sub-procedure 9. Annotation
10. Replacement
Features of an algorithm:
1. Finite number of inputs
2. It terminates after a finite number of steps
3. Actions specified in each step are precise and unambiguous
4. All operations specified can be done exactly and in a finite
amount of time
5. Having one or more outputs derived from inputs
Quality of an algorithm:
1. An algorithm should be relatively fast. The number of
operations required for solving a problem should be minimal.
2. An algorithm should require minimal computer memory to
produce the required output in an acceptable amount of
time.
Programs do not execute successfully for first time, programs contain
few errors or bugs.
The programs are, therefore debugged or errors or bugs are eliminated
till programs execute successfully.
Parts of a program:
1. Initialization that establishes initial values for some variables, prints
headings, messages.
2. Input that performs input of data required by the program.
3. Validation that performs validation of input data to detect errors or omissions.
4. Processing that performs computation or data manipulation.
5. Output that performs output of data to be provided by program.
6. Error handling that performs analysis of error condition and outputs error
messages.
7. Closing procedure that performs procedures to end the execution of the
program.
Structure of program
1. Sequential Structure consists of
- one action followed by another
- perform operation A and then B and so on.
REAL A, B
READ*, A, B
SUM=A+B
AVG=SUM/2
PRINT*, SUM, AVG
STOP
END
2. Selective structure: It consists of a test for a
condition followed by two or more alternative paths
for the program to follow. The program selects one
of the paths depending on the test of the condition.
4. STOP statement
5. END statement
6.3 Assignment statement
--Assigns the value of expressions to variable
Variable = expression
M: integer variable
M= 2.21 → 2 assigns the integer constant 2 to M
READ*, v1,v2,v3………….vn
Where, v1→ a variable name that refers to the first input
data item
PRINT*, v1,v2,v3………….vn
Where, v1→ a variable name that refers to the first output
data item
PRINT*, ‘sum=’, s
READ*, NAME
READ*, DEPTNO
READ*, RATE
READ*, HOURS
READ*, REGULAR
Number comparisons
Assume
X+5 .GT. M*Y X=20.0
→ 20.0 + 5.0 .GT. 5.0 * 4.0 Y=4.0
M=5.0
→ 25.0 .GT. 20.0
The value of the relational expressions is .TRUE.
Table 7.1
Character comparison:
‘computer’ .GT. ‘compute’ ‘computer’ .GT. ‘compute$’ is
.TRUE.
‘WINK’ .EQ. ‘WINK’ .TRUE.
‘WINK’ .EQ. ‘WIN’ .FALSE.
‘WINK’ .EQ. ‘WINK$$’ .TRUE.
‘WINK’ .EQ. ‘WINKLE’ .FALSE.
.TRUE. .FALSE.
.FALSE. .TRUE.
A B A. AND. B A. OR. B A. EQV. B A. NEQV.
B
T T T T T F
T F F T F T
F T F T F T
F F F F T F
7.2 Logical expression: Table 7.3, 7.4
7.3 precedence of relational and logical operations
Arithmetic>Relational>Logical
.NOT.> .AND.> .OR.> .EQV.> .NEQV
( .TRUE.)
NEGATION
Relational operator Negated operator
.EQ. .NE.
.NE. .EQ.
.GT. .LE.
.GE. .LT.
.LT. .GE.
.LE. .GT.
7.5 GOTO STATEMENT
READ*,V1,V2
LARGER=VI
IF(V2.GT.V1) LARGER=V2
PRINT*, LARGER
STOP
END
7.6.2 BLOCK IF STATEMENT
ELSE
statement-3
statement-4 statement set 2
…….
ENDIF
A program which read two integer values from the input and print
the larger of the two values.
ELSE
statement SET-4
ENDIF
Compare two equal and unequal numbers
using nested block IF
INTEGER V1,V2,LARGER
READ*,V1,V2
ELSE
ENDIF
STOP
END
MULTI ALTERNATIVE SELECTIVE STRUCTURE
Block IF statement using an ELSE IF
IF (logical expression 1) THEN
statement SET-1
ELSEIF (logical expression 2) THEN
statement SET-2
ELSEIF (logical expression 3) THEN
statement SET-3
…..
….
ELSE
statement SET N
ENDIF
Compare two equal and unequal numbers
Program using Block IF with ELSEIF
READ*,V1,V2
ENDIF
STOP
END
Practice Program
Prob: 1
Prob:
Solution
REAL LOAN, INTS
READ*, LOAN
Starting Terminal
value of u value of u
DO 10 I = 1, 5
Body of the
DO Loop
DO 40 k = 10, 1, -1
N=?
10
Statements Comment
1. DO 10 I=1,5 VALID
2. DO 10 I=1,10,1 ?
4. DO 10 I= -10, 10, 4 ?
5. DO 10 I= -10, -20, -3 ?
6. DO 10 I= M, M+N, K ?
Execution of a DO LOOP:
1. If the computed execution number N is a real number, it is
truncated to nearest integer. (If N is 9.9, then N=9)
2. If N<= 0, DO LOOP is not executed
3. If N>0, initial value of DO control variable u is set to us.
4. The steps 5 through 6 are repeated N times.
5. All statements beginning immediately after DO statement upto,
and including, the statement whose reference number is s are
executed.
6. u= u + ui
7. The statement continues
Program: Read and print sum 50 times
Program:
DO 100 ICOUNT=1, 50, 1
READ*, A,B
SUM=A+B
PRINT*, SUM
100 CONTINUE
STOP
END
NESTED DO LOOP:
1. Several DO loops can be executed within same
program, they follow one another.
2. There is no specified limit to the number of DO
loops that can be nested.
3. When nesting one DO loop inside another, the inner
loop must be contained within the range of outer loop
4. There must be no overlapping of statements, each
loop must have its own unique control variable,
5. The loops have same terminal statement to provide a
clear style.
8.3 NESTED DO LOOP
DO 30 V1 ……
12+22+32+……………+N2
Program:
integer sum
read*, n
sum=0
do 200 k=1,n
sum =sum + k**2
200 continue
print*,'sum=', sum
stop
end
Problem 03: Write an algorithm and draw a flowchart to
find the sum of series and implement it into FORTRAN.
1+3+32+……upto 20
terms
Program:
integer sum
sum=0
do 100 k = 0, 19
sum = sum + 3**k
100 continue
print*,'sum=',sum
stop
end
1+3+5+……………+ (2N-1)
Problem 04: Write an algorithm and draw a flowchart to
find the sum of series and implement it into FORTRAN.
1-2+3-……………-100 2+4+6+……………+2N
Program: Program:
integer sum integer sum
sum=0 read*, n
j=1
sum=0
do 12 k = 1, 100
sum = sum + j*k do 7 i = 1, n, 1
j = -j sum =sum + 2 * i
12 continue 7 continue
print*,'sum=',sum print*,'sum=',sum
stop stop
end end
Problem 05: Write an algorithm and draw a flowchart to
find the sum of series and implement it into FORTRAN.
1 + x + x2 +……………+ xk
Program:
integer sum
read*, k, x
sum=0
do 12 i = 0, k
sum = sum + x**i
12 continue
print*,'sum=', sum
stop
end
Problem 06: Write an algorithm and draw a flowchart to find the
product of series and implement it into FORTRAN.
I5 123 $$123
I5 -1234 -1234
I5 1234 $1234
I3 1234 ***
I3 -123 ***
10.3 Format-Directed Output
I – format output specification Iw
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
- 2 3 4 - 1 2 3 2 4
X Y Z
F (Real) – format output specification Fw.d
WRITE (6,700) A, B, C
700 FORMAT (F8.2, F8.4, I3)
Where, A = 2.136
B = -3.124
C = 21
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 . 1 4 - 3 . 1 2 4 0 2 1
A B C
10.3.3 E – format output specification (Self study)
10.8 Horizontal positioning specifications
Controls horizontal spacing between data items.
WRITE (6 ,20) N, Y
20 FORMAT (4X, I3, T10, F5.1)
Where, N = 123
Y = 9876
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 9 8 7 . 6
4X I3 F5.1
T10
10.8 Horizontal positioning specifications
INPUT DATA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
M=? N=?
Chapter 11
Subprograms
A subprogram is a separate program unit used
(called) by a main program, or by other
subprogram.
Advantage
‘A subprogram is complete and independent’-
explain (11.1.3)
Classification of subprograms
1. Function subprogram
2. Subroutine subprogram
Purpose and use of subprogram: 1. Repetition: Used to avoid rewriting the same
sequence of code at two or more locations in a program.
2. Universal use: some procedures might be needed in more than one program. By
writing a subprogram and making it available to others who need it, the duplication
of effort can be avoided.
3. Modularity: A program for complex problems can be developed in modular
fashion by designing individual modules or subprograms.
4. Team work: Programming is done by programing team. If the problem is divided
into subprograms, each subprograms can be written by one or two members of the
team.
Terminology: All programs we have written so far is called main program, they
are not invoked by any other program. The subprogram can not be run by
themselves, they are only run when main program invokes or calls them by
including a statement “CALL”
Main program is generally reserved for program which is not invoked by any other
program.
Calling program and called program are frequently used in place of main
program and subprogram.
Communication and argument: Most of the communication between calling and
called program takes place through different parameters and different arguments.
Function subprogram: Returns a single value to program where it is invoked.
Also required to write subprograms that return more than one value.
Subroutine subprogram: used in such a situation and invoked by a CALL
statement that contains the subroutine name.
The name used for arguments in function statement may also be used
elsewhere in the same program, otherwise the arguments used to define
a function are dummy.
POLY(X, Y)= X*X+X*Y+Y*Y
X= POLY(B, A)- B**2- A**2
Y= POLY( X+4, X-4)
Statement function
Dummy Argument
Example 2
Example 3
SUBROUTINE CALC (A, B, SUM,, PROD)
READ*, X, Y
SUM = A + B
CALL CALC (X, Y, SUM, PROD)
PROD = A* B
PRINT*, SUM, PROD
RETURN
STOP
END
END
SUBROUTINE CALC
REAL A, B, PLUS, PROD
PLUS= A+B
PROD= A*B
RETURN
END
Problem 01: Write a FORTRAN program to compute the
binomial co-efficient (nCr) defined as n!/(r!(n-r)!) by using a user
defined function subprogram FACT(X) where X is integer.
Program:
MAIN PROGRAM
READ*, N, R
FACT (N, R, (N-R))
NCR = FACT (N)/ (FACT (R) * FACT (N-R))
PRINT*, NCR
STOP
END
FUNCTION SUBPROGRAM
FUNCTION FACT (X)
FACT = 1
DO 10 I = 1, X
FACT = FACT * I
10 CONTINUE
RETURN
END
Problem 02: Write a function subprogram to calculate area
of a triangle, where a, b, c are the three sides of the
triangle.
FUCTION AREA (A, B, C)
P=A+B+C
S=P/2
AREA=SQRT(S*(S-A)*(S-B)*(S-C))
RETURN
END
MAIN PROGRAM
PRINT*,’ENTER THREE SIDES OF THE TRIANGLE’
READ*,X, Y, Z
TAREA=AREA(X, Y, Z)
PRINT*,’TAREA=’, TAREA
STOP
END
Problem 03: Write a subroutine subprogram to find largest
of three numbers and sum of the numbers.
MAIN PROGRAM
INTEGER SUM
READ*, X, Y, Z
CALL RESULT (X, Y, Z, LARGE, SUM)
PRINT*,’SUM=’, SUM
PRINT*,’LARGE=’,LARGE
STOP
END
Subscripted Variables (Chapter-09)
A single memory location can store COST = 4.00
only one value at a time. SUM = 10.00
9.2 Array:
A collection of values, or data, that are related in some
way can often be processed conveniently by storing the
data items in consecutive memory locations, each of
which can be accessed directly.
Subscript Notation: Test score of 100 students using the subscript
notation as
score1, score2, score3…………………………score100 where s is
single group name for all students, subscript (1,2,3…..100)
identifies the test score, score1 is the score of 1th student, score10
is the score of 10th student and so on…..subscript also can be
written as score(1), score(2)………….
3 91
4 89
s
5 37
Score(3,4)
6 53
7 48
Score(5,2)
8 76
Array Comment
Names
A(5) Valid, 5th element of array A
A(M,3) Valid
A(7*2, 6) ?
A(N+2,6) ?
DIMENSION list
Where list is a sequence of array declarations of the
form.
Array name(m1:n1, m2:n2,….., mk:nk)
Subscripted
DIMENSION score(100) variable
Array declaration: 1. The number of dimension can be at most 7, each pair
(m, n) of integer constants, usually called parameters.
2. Minimum value of subscript with colon need not be specified, if minimum
value is 1
DIMENSION TEST (1:10) , DIMENSION RESULT (10) , DIMENSION TEST
(1:5, 1:4)
DIMENSION TEST (5:4), DIMENSION SCORE (1:30, 31:60, 61:100)
DIMENSION RESULT (10) TEST (5:4) SCORE (1:30, 31:60, 61:100)
1
DIMENSION score(10,4) 12 18 16 19
2 13 19 17 11
3 14.5 18 12 13
10 students: 4 CT Marks 4 4.5 5 15 5
40 memory place whose address: 5 18 16 0 14
Score(1,1), (1,2), (1,3), (1,4)
6 17 18 3 15
(2,1)……….(10,4)
7 8.5 18 10 20
8 9 20 3 20
Print*, score (4,3), 9 8.5 14 0 10
(7,4), (10,2), (11,4), 10 9 19 2 17
(2,5)
Output: ? Fig.: The two-dimensional array SCORE.
INTEGER A
DIMENSION A (3,3) [2 subscripts and 3 rows 3 columns]
Column-wise Ordering
9.4.2 Input/output using a DO loop
INTEGER X(5)
DO 10 I = 1, 5 To use only a part of
READ*, X(I) the declared array by
using the statements
10 CONTINUE
INTEGER X(2,3)
DO 10 J= 1, 3 data element of array
DO 20 I= 1, 2 X=0, 2, 4, 6, 8, 10
READ*, X(I, J)
20 CONTINUE
10 CONTINUE
Column wise X(1,1)=0, X(2,1)=2, X(1,2)=4, X(2,2)=6,
X(1,3)=8, X(2,3)=10
9.4.2 Input/output using a DO loop
2 dimensional array If the data entries for X to be read are
20 CONTINUE X(1,1) = 0
10 CONTINUE X(2,1) = 2
X(1,2) = 4
X(2,2) = 6
X(1,3) = 8
X(2,3) = 10
9.4.2 Input/output using a DO loop
2 dimensional array
X to be read row wise The result in the computer
memory would be?
INTEGER X(2,3)
X(1,1) = 0
DO 10 I = 1, 2 X(2,1) = 6
DO 20 J = 1, 3 X(1,2) = 2
X(2,2) = 8
READ*, X (I, J) X(1,3) = 4
20 CONTINUE X(2,3) = 10
10 CONTINUE
DO 10 I = 1,5
The READ statement to
READ*, X(I) be executed 5 times and
requires 5 lines of input
10 CONTINUE
Implied DO loop
READ*, (X (I), I = 1,5) 5 values of input data to be read into:
X(1), X(2), X(3), X(4) and X(5)
DO 200 I= 1,3
DO 300 J= 1,3
R(I, J)=P(I, J)+Q(I, J)
300 CONTINUE
200 CONTINUE
DO 200 I= 1,3
DO 300 J= 1,4
R(I, J)=P(I, J)+Q(I, J)
300 CONTINUE
200 CONTINUE
D=0
DO 70 L= 1,3
D=D+Q(I,L)*P(L,J)
70 CONTINUE
C(I,J)=D
60 CONTINUE
50 CONTINUE
PRINT*, 'RESULT OF MALTIPLICATION'
DO 80 I=1, 3
PRINT*, C(I, 1), C(I, 2), C(I, 3)
80 CONTINUE
STOP
END
5.Write a program which reads a 3 by 4 matrix M Column
wise and prints the elements row wise.
Solution:
DIMENSION M(3,4)
DO 20 J= 1,4
PRINT*, 'ENTER MARTIX M COLUMN WISE'
READ*, (M(I, J), I=1,3)
20 CONTINUE
DO 10 I=1,3
PRINT 30, (M(I, J), J=1,4)
30 FORMAT(1X, 4I8)
10 CONTINUE
STOP
END
Self study for practise