1 / 17

Week 8

Week 8. More on arrays. If we wish to use an array as an argument to a procedure, it is needed to know the size or shape of the actual arguments used. On the other hand, as mentioned before a procedure does not need to know the details of the calling program unit

rmike
Télécharger la présentation

Week 8

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. Week 8 More on arrays...

  2. If we wish to use an array as an argument to a procedure, it is needed to know the size or shape of the actual arguments used. • On the other hand, as mentioned before • a procedure does not need to know the details of thecalling program unit • the program unit called, in turn, also does not need to know anything about the procedure except the information about its arguments which form part of the procedure’s interface.

  3. In fact, if an array is a procedure dummy argument, it must be declared in the procedure as an assumed-shape array. An assumed-shape array is a dummy argument array wose shape, as its mame implies, is not known but which assumes the shape as that of any actual argument that becomes associated with it.

  4. The array specification for an “assumed-shape array” can take 2 forms as can be seen below , where the 2nd form is equivalent to the 1st with a “lower bound = 1” and on the other hand, the upper bound in both cases will only be established on entry to the procedure and will be whatever value is necessary to ensure that the extent of dummy array is the same as that of the actual array argument : * ( lower bound : )

  5. Arrays and procedures • Explicit-shape array: no freedom! • Assumed-shape array • If an array is a procedure dummy argument, it must be declared in the procedure as an assumed-shape array!

  6. Calling program Procedure Arrays and procedures Explicit-shape Assumed-shape

  7. Arrays and procedures

  8. Example main program unit:real, dimension ( b : 40 ) : : a subprogram: real, dimension ( d : ) : : x x ( d ) ---------------------- a ( b ) x ( d + 1 ) ---------------------- a ( b + 1 ) x ( d + 2 ) ---------------------- a ( b + 2 ) x ( d + 3 ) ---------------------- a ( b + 3 ) . .

  9. Example main program unit: real, dimension ( 10 : 40 ) : : a, b . call array_example_l (a,b) subprogram: subroutine array_example_1 ( dummy_array_1, dummy_array_2) real, intent (inout), dimension : : dummy_array_1, dummy_array_2 . .

  10. Example main program unit real : : p (-5 : 5 ), q ( 100 ) . call array_example_2( p, q ) . . subprogram subroutine array_example_2 ( dummy_array_1, dummy_array_2) real, intent (inout), dimension : : dummy_array_1, dummy_array_2)

  11. Example integer , dimension ( 4 ) : : section = (/ 5, 1, 2, 3 /) real , dimension ( 9 ) : : a callarray_example_3 ( a ( 4 : 8 : 2 ), a ( section ) ) dummy_array_1 = (/ a (4), a (6), a (8) /) dummy_array_2 = (/ a (5), a (1), a (2), a (3) /) dummy_argument_2 (4) dummy_argument_2 (3) dummy_argument_2 (2) dummy_argument_2 (1)

  12. Example PROBLEM : Write a subroutine that will sort a set of names into alphabetic order. İnitial order 7 1 8 4 6 3 5 2 After 1st exc. 1 7 8 4 6 3 5 2 After 2nd exc. 1 2 8 4 6 3 5 7 After 3rd exc. 1 2 3 4 6 8 5 7 After 4th exc. 1 2 3 4 6 8 5 7 After 5th exc. 1 2 3 4 5 8 6 7 After 6th exc. 1 2 3 4 5 6 8 7 After 7th exc. 1 2 3 4 5 6 7 8

  13. Array-valued functions As is well known, arrays can be passed to procedures as arguments and a subroutine can return information by means of an array. It is convenient for a function to return its result in the form of an array of values rather than as a single scalar value ------------array-valued function, as can be seen below : function name ( ……………………..) result (arr) real, dimension ( dim ) : : arr . . end function name

  14. Array-valued functions • A function can return an array • Such a function is called anarray-valued function function name(…..) result(arr)real, dimension(dim) :: arr... endfunctionname Explicit-shape

  15. Example Consider a trivial subroutine that simply adds 2 arrays together, returning the result through a third dummy argument array. subroutine trivial_fun ( x, y ) result (sumxy) real, dimension ( : ) , intent (in) : : x, y real, dimension ( size (x) ) : : sumxy end subroutine trivial_fun

  16. Example Write a function that takes 2 real arrays as its arguments, returns an array in which each element is the maximum of the 2 corresponding elements in the input array

  17. function max_array ( array_1, array_2 ) result (maximum_array) ! this function returns the maximum of 2 arrays on an element-by-element basisdummy arguments real, dimension ( : ) , intent (in) : : array_1, array_2 ! result variable real, dimension ( size (array_1) ) : : maximum_array ! use the intrinsic function “max” to compare elements maximum_array = max ( array_1, array_2) ! or use a do-loop instead of the intrinsic function max to compare elements do i = 1, size (array_2) maximum_array ( i ) = max ( array_1 ( i ), array_2 ( i ) ) end do end function max_array

More Related