program readformat integer::n1,n2,n3 real::x,y,z print*,"please enter 123456789"...
Post on 31-Dec-2015
243 Views
Preview:
TRANSCRIPT
program readformatinteger::n1,n2,n3real::x,y,zprint*,"Please enter 123456789"read"(3i3)",n1,n2,n3print*,"The format for read this data is 3i3"print*,"n1=",n1,"n2=",n2,"n3=",n3 print*,"Please enter 123456789" read "( t5,i2,t6,i2,t2,i4)",n1,n2,n3 print*,"it is read by the format of(t5,i2,t6,i2,t2,i4)n1,n2,n3" print*,"n1=",n1,"n2=",n2,"n3=",n3 x = 3.14159 y = -275.3024 z = 12.9999 print*," x = 3.14159" print*," y = -275.3024" print*," z = 12.9999" print"(f10.3,f10.3,f10.2)",x,y,zendprogram readformat
print “( i5,i5, i5, f6.2, f6.2, f6.2, f 6.2)”, x, y, z, d, e, f, g
or better
print “( 3 i 5, 4 f 6.2 )”, x, y, z, d, e, f, g
program test1integer ::a,b,c,dprint*,"Please enter 1234567890"!read "(i4,t1,i4,t1,i4,t1,i4)",a,b,c,dread"(4(t1,i3))",a,b,c,dprint*,"=",a," b=",b," c=",c," d=",d
print "(i4,a,i5,a,i5,a,f6.2,a,i5,a,i6)",a,& " minus",b," is=",a-b,"; ",c," minus ",d," is=",c-dend program test1
program sicaklikinteger, parameter::satir=365,sutun=27real,dimension(satir,sutun)::sicakreal,dimension(sutun)::ortsaatinteger::i,jopen(unit=1,file="sicakliklar.txt",status="old",action="read")do i=1,satir read(unit=1,fmt=*)sicak(i,1:sutun) print*,sicak(i,1:sutun)end dodo j=4,sutun ortsaat(j-3)=(sum(sicak(1:satir,j)))/satirend doopen(unit=2,file="sicaklik.ort",status="unknown", action="write")do i=1,sutun-3 write(unit=*,fmt="(a,i2,a,f4.1)")"saat ",i," nin ortalamasi=",ortsaat(i) write(unit=2,fmt="(a,i2,a,f4.1)")"saat ",i," nin ortalamasi=",ortsaat(i)enddoend program sicaklik
http://web.itu.edu.tr/~toros/bil106/sicakliklar.txthttp://web.itu.edu.tr/~toros/bil106/sicakliklar.txt
module constants implicit none real, parameter :: PI=3.14 real, parameter :: E=2.71828183 integer, parameter :: answer=42 real, parameter :: earthRadiusMeters=6.38e6end module constants
program test! Option #1: blanket "use constants"! use constants! Option #2: Specify EACH variable you wish to use. use constants, only : PI,E,answer,earthRadiusMeters implicit none
write(6,*) "Hello world. Here are some constants:" write(6,*) PI,E, & answer, earthRadiusMeters end program test
MODULEMODULE
http://stackoverflow.com/questions/1240510/how-do-you-use-fortran-90-module-data
Basic Building BlocksBasic Building Blocks
In all ways of life, the easiest way to solve most problems is to break them down into smaller sub_problems and then to deal with each of these in turn, further sub-dividing these subproblems as necessary.
Programs and modulesPrograms and modules
•Main program unit• program name
use statements...Specification statements...Executable statements...
end program name
MModulesodulesPrograms for solving complex problems should be designed in a modular fashion.
The problem should be divided into simpler subproblems, so that the subprograms can be written to solve each of them.
Every program must include exactly one main program and may also include one or more modules.
MModulesodules•Modules are a second type of program unit.
•The basic structure of a module is similar to the main program unit.
•The initial module statement of each module specifies the name of that module based on the F language rules.
•A module unit ends with an end module statement incuding its name.
•A module does not contain any executable statements.
•A module may contain any number of subprograms which are seperated from the other statements by a contain statement.
ProceduresProceduresA Special section of program which is, in some way, referred to whenever required, is known as a “procedure”.• Programs can be written by the programmer • By some other person who allows the programmer to use them• Can be a part of the F language itself (i.e. intrinsic procedures whose names arereserved words must always be written in lower case). • Subprograms can also be categorized as subroutines • Functions ( create only a single result; there are a lot of intrinsic functions available in F )
• Procedures - origin– “Write your own” (homemade)– Intrinsic (built-in, comes with F )
•sin(x), cos(x), abs(x), …
– Written by someone else (libraries)
• Procedures (subprograms) – form– Functions– Subroutines
ProceduresProcedures
name (argument_1, argument_2, ...)
Examples:
a + b * log (c)
-b + sqrt ( b * b – 4.0 * a * c)
ProceduresProcedures
• The F language does not accept procedures that are not in a module.
• Most features of Fortran 90/95 that are related to modules and module procedures are included.
• Statement functions are not in F.• Functions and subroutines are intended
to be included as module procedures.
SubprogramsSubprogramsFunctions : Functions may, of course, be provided by the user and they are normally implemented by means of an F subprogram which is physically placed within a module as is explained in Modules.
On the other hand, very important principle which applies to all procedures in F is that the main program and any subprograms need never be aware of the internal details of any other program unit or subprograms .
A function subprogram can be called by
· the main program
· another subroutine subprogram
· another function
FunctionsFunctions
• function name (d1, d2, …) result(result_name)
Specifications part..
Execution part• end function name• Variables
– Internal (local) variables– Result variable (keyword result)– Dummy argument (keyword intent(in))
attribute
• call name (arg1, arg2, …)• intent(in),intent(out),intent(inout)
SubroutinesSubroutines
ArgumentsArguments
• Actual arguments in the calling program• Dummy arguments in the subroutine or
function• The order and types of the actual
arguments must correspond exactly with the order and types of the corresponding dummy arguments
ObjectsObjects
• The PRIVATE and PUBLIC attributes specify the accessibility of entities in a module. (These attributes are also called accessibility attributes.)
• Local variables versus global variables• private versus public objects
ExampleExample
MAIN PROGRAM
FUNCTION SUBPROGRAM
program q11use calcreal :: Alpha, Beta, GammaBeta=5.Gamma=3.Alpha = Fkt ( Beta, Gamma )print*,Beta,Gamma,Alphaend program q11
module calcpublic::Fktcontainsfunction Fkt ( x, y ) result(Alpha)real, intent(in) :: x,yreal :: AlphaAlpha = x ** 2 - 2.5 * y + 3.7 * y ** 2end function Fktend module calc
Example:Example: Write a subprogram which calculates the cube root of a Write a subprogram which calculates the cube root of a positive real numberpositive real number
MAIN PROGRAM
program test_cube_root
use maths
real : : x
print *, “Type a positive real number”
read *, x
Print *, “ The cube root of “,x,” is “, cube_root(x)
a = b * cube_root(x) + d
end program test_cube_root
module maths
Public::cube_root
contains
function cube_root (x) result (root)
! a function to calculate the cube root of a positive real number
! Dummy arguments declaration
real, intent (in) : : x
! Result variable declaration
real:: root
! Local variable declaration
real::log_x
! Calculate cube root by using logs
log_x = log (x)
root = exp (log_x / 3.0)
function cube_root
end module maths
AttributesAttributes• intent (in): the dummy argument only provides
information to the procedure and is not allowed to change its value any way
• intent (out): the dummy argument only returns information from the procedure to the calling program
• intent (inout): the dummy argument provides
information in both directions
Examples of subprogramsExamples of subprograms
Write a program using either subroutine or function:
• read the edges of a rectangle,• write a subprogram which calculate area of rectangle
!this program is calculates area of a rectangle
!using subroutine
program area_calculation
use rec
real::a,b,al
print *, "enter two edges of the rectangle"
read *, a,b
call area (a,b,al)
print *, "a=",a
print*,"b=",b
print *, "area_of_rectangle=",al
endprogram area_calculation
module rec
public::area
contains
subroutine area(a,b,al)
real, intent(in)::a,b
real, intent (out)::al
al=a*b
return
endsubroutine area
end module rec
program subprogram
!this program is calculates area of a rectangle
program area_calculation
use rec
real::a,b,al
print *, "enter two edges of the rectangle"
read *, a,b
al=area (a,b)
print *, "a=",a
print*,"b=",b
print *, "area_of_rectangle=",al
endprogram area_calculation
module rec
public::area
contains
function area(a,b)result (al)
real, intent(in)::a,b
real::al
al=a*b
return
endfunction area
end module rec
program subprogram
Write a program using either subroutine or function:
read the three edges of a triangle,
write a subprogram which calculate area of triangle
! This program written by SALİH SARIHAN studying Electrical Engineering in ITU! This program calculate area of a triangle and control the rule of drawn a triangle.! this program writen by using subroutine! email:sarihans@itu.edu.tr
module al_triapublic::areacontains
subroutine area(a,b,c,al)real,intent(in)::a,b,creal,intent(out)::alreal::ss=(a+b+c)/2al=sqrt(s*(s-a)*(s-b)*(s-c))returnend subroutine area
end module al_tria
program triangle_areause al_triareal::a,b,c,alprint*,"Please enter edges of the triangle!"read*,a,b,ccall area(a,b,c,al)! s=(a+b+c)/2 and area_of_triangle=sqrt(s*(s-a)*(s-b)*(s-c)) if a triangle can be drawn,! it provides the { |a-b|<c<a+b } rule. Therefore if a triangle is drawnable, ! [s*(s-a)*(s-b)*(s-c)] be a pozitive integers not a negative integer or zero.! Finally, if a triangle can be drawn, area of triangle will be bigger than zero.
if (al>0) thenprint*,"a=",aprint*,"b=",bprint*,"c=",cprint*,"area of triangle is=",alelseprint*," This triangle can not be drawn!!!"print*," Please check the edges value of the triangle and enter correct values again!!!"end ifend program triangle_area
!This program written by Adem GÜNEL from ITU Meteorology Department module Arithmeticpublic::Addition,Subtraction,Multiplication,Division subroutine Addition(x,y,z) integer,intent(in)::x,y integer,intent(out)::zz=x+yend subroutine Addition subroutine Subtraction(x,y,z) integer,intent(in)::x,y integer,intent(out)::zz=x-yend subroutine Subtraction
subroutine Multiplication(x,y,z) integer,intent(in)::x,y integer,intent(out)::zz=x*yend subroutine Multiplication subroutine Division(x,y,z) integer,intent(in)::x,y integer,intent(out)::zz=x/yend subroutine Divisionend module Arithmetic
!This program written by Adem GÜNEL from ITU Meteorology Department program Arithmetic_Operationsuse Arithmeticinteger::a,y,z,xdo print*, " Which arithmetic operations do you want to?" print*, "Addition (1)” Print*, ”Subtraction (2)" print*, "Multiplication (3)" print*, "Division (4)" print*, "Exit (5)"read*,a select case(a) case(1) print*, "Please enter two numbers" read*,x,ycall Subtraction(x,y,z) print*,"Sum of ”,x,y, “ is= ",z case(2) print*, "Please enter two numbers" read*,x,y call Subtraction(x,y,z) print*,"Subtraction of ”,x,y, “ is= ",z
case(3) read*,x,ycall Multiplication(x,y,z) print*,"Multiplication of ”,x,y, “ is= ",z case(4) print*, "Please enter two numbers" read*,x,ycall Division(x,y,z) print*,"Division of ”,x,y, “ is= ",zcase(5) exit case default end selectenddoend program Arithmetic_Operations
program subprogram
program triangle_area
use ak
real :: a,b,c,al
print *,"a,b,c"
read *,a,b,c
al= alan(a,b,c)
print *,al
end program triangle_area
module ak
public :: alan
contains
function alan(a,b,c) result (al)
real, intent(in) :: a,b,c
real :: al
real :: s
s=(a+b+c)/2.0
al=sqrt(s*(s-a)*(s-b)*(s-c))
return
end function alan
end module ak
program distance_proguse distance_m!variable declarationreal::x1,y1,x2,y2,d1,d2,d12!read the first point (x1,y1)print *,"enter the first point as (x1,y1)"read *,x1,y1!read the second point (x2,y2)print *,"enter the first point as (x2,y2)"read *,x2,y2!call subroutine distance call distance(x1,y1,x2,y2,d12,d1,d2)!print resultsprint*,"the distance of the first point to origin is => d1=",d1print*,"the distance of the second point to origin is => d2=",d2print*,"the distance between the two points is => d12=",d12!end of the main programend program distance_prog
module distance_m public::distancecontainssubroutine distance(x1,y1,x2,y2,d12,d1,d2)!dummy argument declarationreal, intent(in)::x1,y1,x2,y2real, intent(out)::d12,d1,d2!calculate distance of the first point from origind1 = sqrt(x1**2 + y1**2)!calculate distance of the second point from origind2 = sqrt(x2**2 + y2**2)!calculate distance between the two points.d12 = sqrt((x2-x1)**2 + (y2-y1)**2)!exit subend subroutine distanceend module distance_m
Write a subroutine which, when supplied with the coordinates of two points (x1,y1) and (x2,y2), calculates the distance of each point from the origin and the distance between the points. Note: you can use these formulas d1= √x12+y2 2 . . . . . . . . . . . . . . . . . . d=√( x1− x2 )2+ ( y1− y2 )2
module testcubepublic::cube_rootcontainsfunction cube_root(x) result(root)! This function program calculates the cube root of any real number! Dummy argument and result declarations!Dummy argument declarationreal, intent(in)::x!Result variable declarationreal::root!Local variable declarationreal::log_x!eliminate the zero caseif(x==0.0) thenroot = 0.0!Calculate cube root by using logselse if (x<0.0) then! calculate the cube root by using logs negative argumentlog_x = log(-x)root = -exp(log_x/3.0)else!positive argumentlog_x = log(x)root = exp(log_x/3.0)end ifend function cube_rootend module testcube
Write a function, which will return the cube root of its argument.
3√(− x )=− 3√x
3√xStructure planFunction cube_root(x) İf x=0Return zeroElse if x<0Return –exp(log(-x)/3)Else Return exp(log(x)/3)
program e_4_1
use m1
real:: x1,y1,x2,y2,d_1,d_2,distance
print *,"Koordinatlari giriniz , x1,y1,x2,y2:"
read *, x1,y1,x2,y2
call s1(x1,y1,x2,y2,d_1,d_2,distance)
end program e_4_1
module m1
public:: s1
contains
subroutine s1(x1,y1,x2,y2,d1,d2,d)
real, intent (in) :: x1,y1,x2,y2
real, intent (out) :: d1,d2,d
d1=sqrt(x1*x1+y1*y1)
d2=sqrt(x2*x2+y2*y2)
d=sqrt((x2-x1)**2 + (y2-y1)**2)
print *,"Merkezden uzaklik (x1,y1) icin",d1
print *,"Merkezden uzaklik (x2,y2) icin",d2
print *,"Noktalar arasi uzaklik",d
end subroutine s1
end module m1
program distance_prog! variable declaration
real::x,y,b ! read the value of x
print *,"enter the values of x and b (both must be positive)"read *,x,b
! call function for the log.y = log_b_x(x,b)
! print resultsprint *,"the logarithm of number x=",x," to base b=",b," is ",y
! end of the main programend program exercise_2
! *****************************************************function log_b_x(x,b)
! dummy argument declarationreal, intent(in)::x,breal ::log_denominator,log_nominator
! calculate the nominator of the formulalog_nominator = log10(x)
! calculate the nominator of the formulalog_denominator = log10(b)
! calculate the resultlog_b_x = log_nominator / log_denominator
! exit subend function log_b_x
module cube_root_calc_1public :: cube_rootcontains function cube_root(x) result(root)
! Dummy argument declarationreal::x
! Result variable declarationreal::root
! Local variable declarationreal::log_x
! Calculate cube root by using logslog_x = log(x)root = exp(log_x/3.0)end function cube_rootend module cube_root_calc_1
! ********************************************** program test_cube_root
use cube_root_calc_1real :: xprint *," type real positive number"read *, xprint *,"the cube root of x=",x," is",cube_root(x)
end program test_cube_root
module various_roots_1public :: rootscontainssubroutine roots(x,square_root,cube_root,fourth_root,fifth_root)!subroutine to calculatebvarious rots of a positive real numbers! Supplied as the first argument, and returned them in the second to fifth arguments!Dummy argument declarationreal, intent(in)::x!Result variable declarationreal, intent(out)::square_root,cube_root,fourth_root,fifth_root!ocal variable declarationreal::log_x!Calculate square root using intrinsic sqrt()square_root = sqrt(x)!Calculate other root by using logslog_x = log(x)cube_root = exp(log_x/3.0)fourth_root = exp(log_x/4.0)fifth_root = exp(log_x/5.0)end subroutine rootsend module various_roots_1!**************************************************************
program various_rootsuse various_roots_1
!A program to show the use of the subroutinerootsreal :: x,root_2,root_3,root_4,root_5print *," type real positive number"read *, x
! obtain rootscall roots(x,root_2,root_3,root_4,root_5)print *,"the square root of x=",x," is",root_2print *,"the cube root of x=",x," is",root_3print *,"the fourth root of x=",x," is",root_4print *,"the fifth root of x=",x," is",root_5end program various_roots
module make_full_name_1contains
subroutine get_full_name(first_name,last_name,full_name)! Subroutine to join two names to form a full name with a single! space between the first and last names! Dummy argument declaration
character (len=*), intent(in) ::first_name,last_namecharacter (len=*), intent(out) ::full_name
! use adjustl to remove redundant leading blanks, and trim! to remove redundant blanks atr the end of first name
full_name = trim(adjustl(first_name)) // " " // c adjustl(last_name)
end subroutine get_full_name
end module make_full_name_1! ******************************************************** program make_full_name
use make_full_name_1! Variable declaration
character (len=80)::name_1,name_2,name! read the first name
print *,"enter the first name"read *,name_1
! read the surnameprint *,"enter the surname"read *,name_2
! combine two names by using the subroutine named get_fuul_name call get_full_name(name_1,name_2,name)
print *,"the combination of the two names is => ",nameend program make_full_name
module Natural_Constantsreal,parameter,public::pi=3.1415927,g= & 9.81, e=2.7182818end module Natural_Constants! ******************************** program module_example_1
use Natural_Constantsprint *,g,piend program module_example_1
! Example P.8. (Catherine Spade, 11 October 1999)! Temperature Converter with Procedures. program A08 use A08M implicit none real :: Fahr, Cels! start program A08 call Input( Fahr ) ! Subroutine referenceCels = Temp_C( Fahr ) ! Function referencecall Output( Fahr, Cels ) ! Subroutine referencestopend program A08
subroutine Input( F_Temp )
real, intent(out) :: F_Temp
! start subroutine Input
write (unit = *, fmt = *) " Please enter the Fahrenheit temperature. "
read (unit = *, fmt = *) F_Temp
return
end subroutine Inputfunction Temp_C( F_Temp ) result( Temp_C_R ) real, intent(in) :: F_Temp real :: Temp_C_R real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 ! start function Output Temp_C_R = (F_Temp - OFFSET) / T_SCALE return end function Temp_Csubroutine Output( F_Temp, Temp_C_R )
real, intent(in) :: F_Temp, Temp_C_R
! start subroutine Output
write (unit = *, fmt = *) F_Temp, " deg. F = ", Temp_C_R, " deg. C"
return
end subroutine Output
module A08M
implicit none
public :: Input, Temp_C, Output
Contains
End module A08M
! Example 2.14 program C14 use C14M implicit none! start program C14 call Input( ) call Calculate( ) call Output( ) stop end program C14
module C14M implicit none public :: Input, Calculate, Output real, private :: Fahr, Cels contains subroutine Input( ) ! start subroutine Input write (unit = *, fmt = *) " Enter the Fahrenheit temperature. " read (unit = *, fmt = *) Fahr return end subroutine Input subroutine Calculate( ) real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 ! start subroutine Calculate Cels = (Fahr - OFFSET) / T_SCALE return end subroutine Calculate subroutine Output( ) ! start subroutine Output write (unit = *, fmt = *) Fahr, " deg. F = ", Cels, " deg. C" return end subroutine Output end module C14M
Prepare a computer program which calculates and prints, the bottom surface area and the volume of a concrete cylinder block having the following initial values :
* radius r = 210 cm
* height h = 315 cm
program question4
real::r,h,bsa, vol
real, parameter::pi=3.14
r=210
h=315
bsa=pi*r*r
vol=bsa*h
print*,"bottom surface area=",bsa, "cm2"
print*,"volume=", vol, "cm3"
end program question4
Write a function which, when supplied with the coordinates of two points (x1, y1) and (x2, y2), calculates the
distance between the points by means of following formula.
d = [ (x2 - x1) 2 + (y2 - y1)
2 ] 1/2
!this program is calculates distance !between two points
program distance
use distance_m
real::x1,y1,x2,y2,d
print *, "enter coordinates of two points"
read *, x1,y1,x2,y2
d=dist(x1,y1,x2,y2)
print *, "x1=",x1, "y1=",y1
print*,"x2=",x2,"y2=",y2
print *, "distance=",d
endprogram distance
module distance_m
public::dist
contains
function dist(x1,y1,x2,y2)result (d)
real, intent(in)::x1,y1,x2,y2
real::d
d=sqrt((x2-x1)**2+(y2-y1)**2)
return
endfunction dist
end module distance_m
module mod1public :: fun1,fun2,fun3,sub1containsfunction fun1(x) result(a)real, intent(in) :: xreal :: aa=2*x**3+3*xend function fun1function fun2(x) result(b)real, intent(in) :: xreal :: bb=3*x**2+4end function fun2function fun3(x) result(c)real, intent(in) :: xreal :: cc= 7*x+5end function fun3subroutine sub1(a,b,c,z,fz)real, intent(in) :: a,b,c,zreal, intent(out) :: fzfz= a*z**2+b*2+cend subroutine sub1end module mod1
pragram calculationuse mod1real :: a,b,c,x,z,fzprint *, "enter a value for x"read *, xprint *, "enter a value for z"read *, za= fun1(x)b= fun2(x)c= fun3(x)call sub1(a,b,c,z,fz)print *, "fz is equal“, fzend program calculation
HOMEWORK
PROBLEM
Consider a geometrical body which consists of a steel-cylinder and a cone of lead having the same radius. Then, prepare a computer program having the structure described in below :
The main program will: read the input values (radius r=50cm, height of the cylinder, h1=300cm, height of the cone h2 = 150cm, density of steel, d1 = 7.85 t/m3, density of lead d2 = 11.40 t/m3 )
calculate the total volume of the given body
calculate the total weight of the given body
finally print,
the input values
the volume of the cylinder
the weight
the volume of the cone
the weight of the cone
the total volume of the given body
the total weight of the given body
using list directed output command including the above-given expressions for the input values and the results obtained.
A function sub-program will
calculate only the bottom surface area
A subroutine sub-program will calculate
the volume of the cylinder
the weight of the cylinder
Another subroutine sub-program will calculate
the volume of the cone
the weight of the cone
top related