presentation on use of fortran

Upload: mani

Post on 04-Mar-2016

216 views

Category:

Documents


0 download

DESCRIPTION

This is the presentation on the use of FORTRAN and some basic concepts of fortran.

TRANSCRIPT

Fortran 77 Tutorial

Fortran77TutorialBy Shubham.Fundamentals of Program solving

Examples of AlgorithmsVelocity-time relationStep 1. Give initial velocity u,time t and acceleration a.Step 2. Find final velocity v using the relation v=u+a*tStep 3. Print final velocity.Step 4. End of the program.

Finding Maximum numberStep 1. Feed total number of points n.Step 2. Give first data point x.Step 3. initialize a variable max=x.Step 4. Start a loop over I varying from 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.Examples of flow chartsVelocity-time relation

Finding maximum of numbersStartRead u,t,a V=u+a*tPrint vEndStartRead n,xWrite maxRead yendMax=xMax=yDo 5 i=2,nIsy>max5Program writtingIntroduction to FORTRAN 77An abbreviation for FORmula TRANslationHas a sequential structure executed from top to bottom.1. INPUTSyntax read(*,*) variable1, variable2Exampleread(*,*) a,b,n,max,pi

Reads data fed from key boardAllows one to feed values of the variables in the natural format2. Processing of dataDifferent statements used in Program writing:Input statementType declaration statementsAssignment statementArithmetic operation statementsLibrary function statementsSelective 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 ifRepetitive structure(a) if-go to(b) do- continue loop(c) nested do loop(d) arrays and dimension

2.Type declaration Statements

Syntaxinteger list of variablesreal list of variablesExamplec "Program to find sum of two numbers" real sum, x, y write(*,*)'Enter the given numbers' read(*,*)x,y sum=x+y write(*,*)sum of given numbers =',sum stop end3.Assignment statementSyntaxVariable=valueExamplecProgram to find area of a circlereal pi, areawrite(*,*)enter radiusread(*,*) rpi=3.1432area=pi*r**2write(*,*)area of circle =,areastopend

4.Arithmetic operation statements

+Addition-Subtraction*Multiplication/Division**ExponentiationHierarchy Order

5.Library Functions Formula transformation11Example CProgram to find area of a trianglereal areawrite(*,*)enter sides of triangleread(*,*)a,b,cs=(a+b+c)/2.0area=sqrt(s*(s-a)*(s-b)*(s-c))write(*,*)area of the triangle = ,areastopend6.Selective structureGo To StatementArithmetic If StatementRelational ExpressionsLogical ExpressionsLogical If StatementBlock If StatementNested Block If StatementMulti-alternative Block If Statement(a) Go To StatementPerforms unconditional transfer of controlUsed to jump from one part of a program to anotherExecution will continue indefinitelyCtrl+C keys or Ctrl+Break keys for terminationSyntaxgo to n(where n is a statement label between 1 to 99999)Example20read(*,*)a,b,csum=a+b+cwrite(*,*)a,b,c,sumgo to 20stopend(b) Arithmetic If StatementSyntaxif(Arithmetic expression ) n1,n2,n3If the numeric value of the Arithmetic expression is less than 0,computer transfers the control to statement n1,if it is equal to 0,control is transferred to statement n2,if it is greater than 0,control is transferred to n3Example:cProgram to discuss nature of roots of a quadratic eqnwrite(*,*)enter coefficients of the equationread(*,*)a,b,cif(b**2-(4*a*c)) 30,40,5030write(*,*)roots are imaginarygo to 9040write(*,*)roots are real and equalgo to 9050write(*,*)roots are real and distinct90stopend

(c) Relational ExpressionsUsed for conditional controlExample of Relational ExpressionExample: c "Program to generate odd numbers" real l,u write(*,*)'enter lower limit and upper limit' read(*,*) l,u write(*,*)'List of odd numbers is:' do 20 i=l,u y=i/2.0 if(int(y).ne.y) write(*,*) i 20 continue stop end

(d) Logical ExpressionsIf conditions are complex,then relational expressions can be joined using the logical operators to implement them.

Example: (age.gt.5).and.(age.lt.60)(e) Logical If StatementThis statement checks the logical expression.Syntax if(logical expression) statement 1statement 2If the logical expression is TRUE,it executes the statement 1,then the statement 2. If it is FALSE,it executes statement 2 skipping the statement 1.Example:cFinding Discount on Salesread(*,*) salesdisc= 0.05if(sales.gt.100.and.sales.lt.500) disc= 0.1if(sales.ge.5000) disc= 0.15totdis= sales*discwrite(*,*)sales,totdisstopend(f) Block If StatementIt is used when execution of more than one statement depends upon certain conditions.Syntax:if(logical expression) thenstatement 1: :statement nelsestatement a: :statement mendif(g) Nested Block If StatementOne Block If statement may contain another Block If statement.The inner Block If statement should never cross the If-Then or Else Blocks.Exampleread(*,*) xif(x.lt.5) theny= sin(x)elseif(x.eq.5) theny= cos(x)elsey= tan(x)endifendifwrite(*,*) ystopend(h) Multi-alternative Block If statementA better form of nested blocks can be performed using multi-alternative block If statements as shown below:if(logical expression 1) thenstatement 1: :statement nelse if(logical expression 2) thenstatement a: :statement melsestatement A: :statement Mendif7. Repetitive Structure(a) If- Go To StatementsIt employs a counter variable in conjuction with the If and Go To statements.Examplecfactorial of a positive integerinteger factread(*,*) nm=1fact=110fact=fact*mm=m+1if(m.le.n) go to 10write(*,*)n, factstopend(b) Do-Continue Loop StatementsMore convenient wayExamplecdouble factorial read(*,*) nfact=1do 10 i=1,n,2fact=fact*i10continuewrite(*,*) n, factstopend

The no. of times a loop get executed =

(c) Nested Do LoopsOne Do loop may contain another Do loop.The inner Do loop can never cross the outer loop.ExamplecGenerating multiplication tables upto 10x10integer proddo 20 i=1,10 do 20 k=1,10prod=i*kwrite(*,*) i, x, k, = , prod20continuestopend3. OUTPUTResults of the processing part are displayed here.The various write statements written in the program are the output statements.Example:CProgram to find area of a trianglereal areawrite(*,*)enter sides of triangleread(*,*)a,b,cs=(a+b+c)/2.0area=sqrt(s*(s-a)*(s-b)*(s-c))write(*,*)area of the triangle = ,areastopend

274. TerminationThis brings to the end of execution of a program.STOP and END statements are the termination statements.

Thanks.