1 / 35

Basic Building Blocks week4

Basic Building Blocks week4. 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. Seven Golden Rules. Always plan ahead

jesuscook
Download Presentation

Basic Building Blocks week4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Building Blocksweek4 In all ways of life, the easiest way to solve most problems is tobreak them down into smaller sub_problems and then to dealwith each of these in turn, further sub-dividing these subproblemsas necessary.

  2. Seven Golden Rules • Always plan ahead • Develop in stages • Modularize • Keep it simple • Test throughly • Document all programs • Enjoy your programming

  3. Programs and modules • Main program unit programname use statements . . . Specification statements . . . Executable statements . . . end programname

  4. 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.

  5. 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 ofthatmodule based on the F language rules. • A module unit ends with an end module statement incuding itsname. • A module does not contain any executable statements. • A module may contain any number of subprograms which areseperatedfrom the other statements by a contain statement.

  6. Module program unit modulename use statements . . . Specification statements . contains (Procedure definitions) subprogram_1 subprogram_2 . . subprogram_n end modulename

  7. Procedures A special section of program which is, in some way, referred to whenever required, isknown 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 are reserved wordsmust always be written in lower case). Subprograms can also be categorized as subroutines ( there are 5 intrinsic subroutines ) functions ( create only a single result ; there are 97 intrinsic functions available inF)

  8. Procedures • Divide and rule! Main program Procedure 1 Procedure 3 Procedure 2

  9. Procedures • 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

  10. Procedures • 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 3 Procedure 2

  11. Procedures name (argument_1, argument_2, ...) Examples: • a + b * log (c) • -b + sqrt ( b * b – 4.0 * a * c)

  12. Procedures • A) PROBLEM : A farmer has a triangular field which he wishes to sow with wheat. • Write a program that readsthe 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

  13. 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

  14. 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

  15. Functions • functionname (d1, d2, …)result(result_name) Specifications part . . Execution part • end functionname • Variables • Internal (local) variables • Result variable (keyword result) • Dummy argument (keyword intent(in)) attribute

  16. Functions 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_root

  17. 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

  18. Subroutines • call name (arg1, arg2, …) • intent(in), intent(out), intent(inout)

  19. 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

  20. Objects • Local variables vs. global variables • privatevs.public objects

  21. 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

  22. Example FUNCTION SUBPROGRAM functionFkt ( x, y ) real : : Fkt real : : x, y Fkt = x ** 2 - 2.5 * y + 3.7 * y ** 2 x = 0.0 end function Fkt MAIN PROGRAM program …….. real : : Alpha, Beta, Gamma . . Alpha= Fkt ( Beta, Gamma ) . . end program ……….

  23. 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

  24. module maths Public::cube_root contains functioncube_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

  25. 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

  26. 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

  27. program subprogram !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

  28. 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

  29. Write a program using either subroutine or function: • read the three edges of a triangle, • write a subprogram which calculate area of triangle

  30. program subprogram !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

  31. program subprogram 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 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

  32. 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

  33. 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

  34. 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

  35. 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

More Related