Tutorial
By Shubham.
1.
Algorith
m
2. Flow
Chart
3.
Program
Various
Various sequential
sequential elementary
elementary steps
steps of
of problem
problem
written
in
simple
English
written in simple English
Also
Also called
called pseudo-code
pseudo-code of
of the
the problem
problem
Graphical
Graphical representation
representation of
of the
the algorithmic
algorithmic
structure
structure of
of the
the problem
problem
Flow
Flow of
of logic
logic of
of the
the problem
problem
Various
Various sequential
sequential steps
steps written
written in
in High
High Level
Level
Language
Language e.g.
e.g. Fortran,
Fortran, C,
C, C++
C++
Script
Script usually
usually combination
combination of
of numeric
numeric and
and
alphabetic
keys
with
special
characters
alphabetic keys with special characters having
having
special
meanings
special meanings
Examples of
Algorithms
Velocity-time relation
Step 1. Give initial velocity u,time t
and acceleration a.
Step 2. Find final velocity v using the
relation v=u+a*t
Step 3. Print final velocity.
Step 4. End of the program.
2 to n in steps of 1.
Step 5. Give next data point y.
Step 6. If y>max, then set max=y.
Step 7. End of loop started at step 4.
Step 8. Print max.
Step 9. End of the program.
Read n,x
Read
u,t,a
Max=x
V=u+a*t
Print v
End
Do 5 i=2,n
Read y
Is
y>ma
x
Max=y
5
Write
max
Finding end
maximum of numbers
Program writting
Introduction to FORTRAN 77
An abbreviation for FORmula TRANslation
Has a sequential structure executed from top to bottom.
1. Input
2.Processing of
data
3. Output
4. Termination
1. INPUT
Syntax
Reads data
fed from key
board
Types of
variables
Intege
r i,j,k,l,m,n
Real
a-h,o-z
2. Processing of data
Different statements used in Program writing:
Input statement
Type declaration statements
Assignment statement
Arithmetic operation statements
Library function statements
Selective structure
(a) go to statement (b) Arithmetic if statement
(c)relational expressions (d) logical expressions
(e)logical if statement (f) block if statement
(g) nested block if (h) multi-alternative block if
7. Repetitive structure
(a) if-go to (b) do- continue loop
(c) nested do loop (d) arrays and dimension
1.
2.
3.
4.
5.
6.
3.Assignment statement
Syntax
Variable=value
Example
c Program to find area of a circle
real pi, area
write(*,*)enter radius
read(*,*) r
pi=3.1432
area=pi*r**2
write(*,*)area of circle =,area
stop
end
**
6.5+2.3*3.0/4.1**2
/,*
(left
to
right)
6.5+2.3*3.0/4.1**2
125/5*5+2
+ , - (left to right)
6.5+2.3*3.0-4.1**2
6*2-1+2*3
5.Library Functions
Formula transformation
Arithmatic
Functions
sqrt(x) real(x)
abs(x)
alog(x)
iabs(n)
alog10(x)
int(x)
exp(x)
nint(x)
mod(n,m)
anint(x)
amod(x,y)
float(n)
Trignometric
Functions
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
Hyperbolic
Functions
sinh(x)
cosh(x)
tanh(x)
Example
C
6.Selective
structure
(a) Go To Statement
(b) Arithmetic If Statement
(c) Relational Expressions
(d) Logical Expressions
(e) Logical If Statement
(f) Block If Statement
(g) Nested Block If Statement
(h) Multi-alternative Block If Statement
(a) Go To Statement
Performs unconditional transfer of control
Used to jump from one part of a program to another
Execution will continue indefinitely
Ctrl+C keys or Ctrl+Break keys for termination
Syntax
go to n
(where n is a statement label between 1 to 99999)
Example
20 read(*,*)a,b,c
sum=a+b+c
write(*,*)a,b,c,sum
go to 20
stop
end
(b) Arithmetic If
Statement
Syntax
Example: (age.gt.5).and.(age.lt.60)
Blocks.
Example
read(*,*) x
if(x.lt.5) then
y= sin(x)
else
if(x.eq.5) then
y= cos(x)
else
y= tan(x)
endif
endif
write(*,*) y
stop
end
7. Repetitive
Structure
and Go To statements.
Example
c factorial of a positive integer
integer fact
read(*,*) n
m=1
fact=1
10 fact=fact*m
m=m+1
if(m.le.n) go to 10
write(*,*)n, fact
stop
end
c double factorial
read(*,*) n
fact=1
do 10 i=1,n,2
fact=fact*i
10 continue
write(*,*) n, fact
stop
end
The no. of times a loop get executed =
3. OUTPUT
Results of the processing part are displayed here.
The various write statements written in the program
real area
read(*,*)a,b,c
s=(a+b+c)/2.0
area=sqrt(s*(s-a)*(s-b)*(s-c))
stop
end
4. Termination
This brings to the end of execution of a
program.
STOP and END statements are the termination
statements.
Thanks.