Download presentation
Presentation is loading. Please wait.
Published byToby Simpson Modified over 8 years ago
2
Basic 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.
3
Seven Golden Rules Always plan ahead Develop in stages Modularize Keep it simple Test throughly Document all programs Enjoy your programming
4
Programs and modules Main program unit program name use statements... Specification statements... Executable statements... end program name
5
Modules Modules Programs 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.
6
Modules Modules 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.
7
Module program unit module name use statements... Specification statements. contains (Procedure definitions) subprogram_1 subprogram_2.. subprogram_n end module name
8
Procedures ASpecial section of program which is, in some way, referred to whenever required, is known as a “procedure”. Pprograms · 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 are reserved words must always be written in lower case). Ssubprograms can also be categorized as subroutines ( there are 5 intrinsic subroutines ) functions ( create only a single result; there are 97 intrinsic functions available in F )
9
Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3 Procedures
10
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 Procedures
11
The main program and any subprogram need never be aware of the internal details of any other program or subprogram! Main program Procedure 1 Procedure 2 Procedure 3 Procedures
12
name (argument_1, argument_2,...) Examples: a + b * log (c) -b + sqrt ( b * b – 4.0 * a * c) Procedures
13
Procedures A) PROBLEM : A farmer has a triangular field which he wishes to sow with wheat. Write a program that reads the lenghts of the 3 sides of the field (in meters) and the sowing density (in grams per square meters) Print the number of 10 kilo bags of wheat he must purchase in order to sow the whole field. B.) ANALYSIS : STRUCTURE PLAN of the PROBLEM read lenghts of the sides of the field ( a, b, c ) and calculate the area of the field area = ( s (s-a)(s-b)(s-c) ) ½ 2s = a + b + c read the sowing density calculate the quantity of wheat seed required calculate the number of 10 kilo bags this represents
14
C) SOLUTION program wheat_sowing ! This program calculate quantity of wheat required to sow a triangular field ! Variable declarations real : : a, b, c,s, area, density, quantity integer : : num_bags ! read the lengths of the sides of the field print *, “type the lengths of the 3 sides of the field in metres : “ read *, a, b, c ! calculate the area of the field s = 0.5 * ( a + b + c ) area = sqrt ( s * (s - a)*(s - b)*(s - c) ) ! read sowing density print *, “ What is the sowing density (gms/sq.m) ?” read *, density ! calculate quantity of wheat and the number of 10 kilo bags ! round up more than 1 kg quantity = density * area num_bags = 0.0001 * quantity + 0.9 ! print results print *, “the area of the field is “, area,” sq. metres” print *, “and “, num_bags,” 10 kilo bags will be required” end program wheat_sowing
15
Subprograms Functions : 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
16
Functions 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
17
function cube_root result(root) ! A function to calculate the cube root of ! a positive real number ! Dummy argument 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) end function cube_rootFunctions
18
Subroutines subroutine roots (x, square_root, cube_root, fourth_root, & fifth_root) ! Subroutine to calculate various roots of positive real ! Number supplied as the first argument, and return them in ! the second to fifth arguments ! Dummy argument declarations real, intent(in) :: x real, intent(out) :: square_root, cube_root, & fourth_root, fifth_root ! Local variable declaration real :: log_x ! Calculate square root using intrinsic sqrt square_root = sqrt(x) ! Calculate other roots by using logs log_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 roots
19
call name (arg1, arg2, …) intent(in), intent(out), intent(inout)Subroutines
20
Arguments 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
21
Objects Local variables versus. global variables private versus. public objects
22
Saving the values of local objects Local entities within a procedure are not accessible from outside that procedure Once an exit has been made, they cease to exist If you want their values to ‘survive’ between calls, use real, save :: list of real variables real, save::a, b=1.23, c Integer, save::count=0
23
Example MAIN PROGRAM program …….. real : : Alpha, Beta, Gamma. Alpha = Fkt ( Beta, Gamma ). end program ………. FUNCTION SUBPROGRAM function Fkt ( x, y ) real : : Fkt real : : x, y Fkt = x ** 2 - 2.5 * y + 3.7 * y ** 2 x = 0.0 end function Fkt
24
Example: Write a subprogram which calculates the cube root of a positive 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
25
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 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
26
Attributes 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
27
Examples of subprograms Write a program using either subroutine or function: read the edges of a rectangle, write a subprogram which calculate area of rectangle
28
!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 programsubprogram
29
!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 programsubprogram
30
Write a program using either subroutine or function: n read the three edges of a triangle, n write a subprogram which calculate area of triangle
31
!this program calculate area of a triangle !using subroutine program triangle_area use ak real :: a,b,c,al print *,"a,b,c" read *,a,b,c call alan(a,b,c,al) print *,al end program triangle_area module ak public :: alan contains subroutine alan(a,b,c,al) real, intent (in)::a,b,c real, intent (out)::al real :: s s=(a+b+c)/2 al=sqrt(s*(s-a)*(s-b)*(s-c)) return end subroutine alan end module ak programsubprogram
32
programsubprogram 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
33
program exercise_1 !variable declaration real::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 results print*,"the distance of the first point to origin is => d1=",d1 print*,"the distance of the second point to origin is => d2=",d2 print*,"the distance between the two points is => d12=",d12 !end of the main program end program exercise_1 ! *********************************************** subroutine distance(x1,y1,x2,y2,d12,d1,d2) !dummy argument declaration real, intent(in)::x1,y1,x2,y2 real, intent(out)::d12,d1,d2 !calculate distance of the first point from origin d1 = sqrt(x1**2 + y1**2) !calculate distance of the second point from origin d2 = sqrt(x2**2 + y2**2) !calculate distance between the two points. d12 = sqrt((x2-x1)**2 + (y2-y1)**2) !exit sub end subroutine distance program exercise_1 !variable declaration real::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 results print*,"the distance of the first point to origin is => d1=",d1 print*,"the distance of the second point to origin is => d2=",d2 print*,"the distance between the two points is => d12=",d12 !end of the main program end program exercise_1 ! *********************************************** subroutine distance(x1,y1,x2,y2,d12,d1,d2) !dummy argument declaration real, intent(in)::x1,y1,x2,y2 real, intent(out)::d12,d1,d2 !calculate distance of the first point from origin d1 = sqrt(x1**2 + y1**2) !calculate distance of the second point from origin d2 = sqrt(x2**2 + y2**2) !calculate distance between the two points. d12 = sqrt((x2-x1)**2 + (y2-y1)**2) !exit sub end subroutine distance
34
program exercise_2 !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 results print *,"the logarithm of number x=",x," to base b=",b," is ",y !end of the main program end program exercise_2 ! ***************************************************** function log_b_x(x,b) !dummy argument declaration real, intent(in)::x,b real ::log_denominator,log_nominator !calculate the nominator of the formula log_nominator = log10(x) !calculate the nominator of the formula log_denominator = log10(b) !calculate the result log_b_x = log_nominator / log_denominator !exit sub end function log_b_x program exercise_2 !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 results print *,"the logarithm of number x=",x," to base b=",b," is ",y !end of the main program end program exercise_2 ! ***************************************************** function log_b_x(x,b) !dummy argument declaration real, intent(in)::x,b real ::log_denominator,log_nominator !calculate the nominator of the formula log_nominator = log10(x) !calculate the nominator of the formula log_denominator = log10(b) !calculate the result log_b_x = log_nominator / log_denominator !exit sub end function log_b_x
35
module cube_root_calc_1 public :: cube_root contains function cube_root(x) result(root) !Dummy argument declaration real::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) end function cube_root end module cube_root_calc_1 ! ********************************************** program test_cube_root use cube_root_calc_1 real :: x print *," type real positive number" read *, x print *,"the cube root of x=",x," is",cube_root(x) end program test_cube_root
36
module various_roots_1 public :: roots contains subroutine 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 declaration real, intent(in)::x !Result variable declaration real, intent(out)::square_root,cube_root,fourth_root,fifth_root !Local variable declaration real::log_x !Calculate square root using intrinsic sqrt() square_root = sqrt(x) !Calculate other root by using logs log_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 roots end module various_roots_1 !******************************************************************* program various_roots use various_roots_1 !A program to show the use of the subroutine roots real :: x,root_2,root_3,root_4,root_5 print *," type real positive number" read *, x !obtain roots call roots(x,root_2,root_3,root_4,root_5) print *,"the square root of x=",x," is",root_2 print *,"the cube root of x=",x," is",root_3 print *,"the fourth root of x=",x," is",root_4 print *,"the fifth root of x=",x," is",root_5 end program various_roots
37
module make_full_name_1 contains 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_name character (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 surname print *,"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 => ",name end program make_full_name
38
module Natural_Constants real,parameter,public::pi=3.1415927,g= & 9.81, e=2.7182818 end module Natural_Constants !******************************** program module_example_1 use Natural_Constants print *,g,pi end program module_example_1
39
! 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 reference Cels = Temp_C( Fahr ) ! Function reference call Output( Fahr, Cels ) ! Subroutine reference stop end program A08 module A08M implicit none public :: Input, Temp_C, Output contains subroutine Input( F_Temp ) function Temp_C( F_Temp ) result( Temp_C_R ) subroutine Output( F_Temp, Temp_C_R ) end module A08M
40
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 Input function 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_C subroutine 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
41
! 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
42
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
43
!24 October 2001 quiz1 question4 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
44
Write a function which, when supplied with the coordinates of two points (x 1, y 1 ) and (x 2, y 2 ), calculates the distance between the points by means of following formula. d = [ (x 2 - x 1 ) 2 + (y 2 - y 1 ) 2 ] 1/2
45
!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
46
HOMEWORK 2 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 :
47
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/m 3, density of lead d2 = 11.40 t/m 3 ) 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.
48
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.