|
****JavaScript based drop down DHTML menu generated by NavStudio. (OpenCube Inc. - http://www.opencube.com)****
|
||||||||||||||
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello all,=0D
=0D I had worked as a C programmer for a number of years in the past, and as a = first impression, the use of pointers in Fortran 90 seems quite different t= o me (I am new to pointers in Fortran 90 and I have started learning about = this topic essentially to teach it to my students). I will appreciate any c= omments & criticism regarding the following. (Please remember that I am new= to this particular aspect of Fortran 90 and be merciful :-) )=0D =0D My understanding is, an "array pointer" (or, a "pointer array") and an "arr= ay of pointers" are two different things. For example, consider the followi= ng program segment:=0D =0D REAL, DIMENSION(10), TARGET :: a=0D REAL, DIMENSION(:), POINTER :: p=0D ...=0D p=3D>a=0D =0D This associates p with array a. Here p is an "array pointer." One could thu= s write p(1) instead of a(1), p(2) instead of a(2), etc. =0D =0D One can also ALLOCATE and DEALLOCATE array pointers. As a matter of fact, i= t appears to me that array pointers are very similar to allocatable arrays.= Unlike allocatable arrays, however, it is not an error to ALLOCATE an arra= y pointer that is currently associated with a target, e.g.=0D =0D ALLOCATE(p(20))=0D =0D The effect is to set the pointer to point to the new object just allocated,= and break the connection with the previous target.=0D =0D An "array of pointers", on the other hand, is actually impossible to define= in Fortran 90. Such an array can be defined and used only indirectly using= a derived data type.=0D =0D For example, if an array of pointers to real values is required, the follow= ing data type can be defined:=0D =0D TYPE pointer_to_real=0D REAL, POINTER :: ptr=0D END TYPE pointer_to_real=0D =0D We can then declare an array of this derived type:=0D =0D TYPE(pointer_to_real), DIMENSION(100) :: px=0D =0D or, perhaps=0D =0D TYPE(pointer_to_real), ALLOCATABLE, DIMENSION(:) :: px=0D =0D It is now possible to refer to the i-th pointer by writing px(i)%ptr.=0D =0D As a simple example of application of this, I would like to include an impl= ementation of "bubble sort method." This is an inefficient sorting method, = but I think it always serves well in illustrating several language features= . (Press et al. in their acclaimed book Numerical Recipes state "If you kno= w what bubble sort is, wipe it from your mind; if you don't know, make a po= int of never finding out!")=0D =0D MODULE sort_routine=0D TYPE pointer_to_real=0D REAL, POINTER :: ptr=0D END TYPE pointer_to_real=0D CONTAINS=0D SUBROUTINE bubble_sort(n, p_arr)=0D IMPLICIT NONE=0D INTEGER, INTENT(IN) :: n=0D TYPE(pointer_to_real), INTENT(INOUT), DIMENSION(:) :: p_arr=0D TYPE(pointer_to_real) :: temp=0D LOGICAL sorted=0D INTEGER i, j=0D DO i=3D1,n-1=0D sorted=3D.TRUE.=0D DO j=3D1,n-i=0D IF(p_arr(j)%ptr > p_arr(j+1)%ptr)THEN=0D temp =3D p_arr(j)=0D p_arr(j) =3D p_arr(j+1)=0D p_arr(j+1) =3D temp=0D sorted=3D.FALSE=0D ENDIF=0D END DO=0D IF(sorted) RETURN=0D END DO=0D END SUBROUTINE=0D END MODULE=0D =0D PROGRAM order_demo=0D USE sort_routine=0D IMPLICIT NONE=0D REAL, ALLOCATABLE, DIMENSION(:), TARGET :: x=0D TYPE(pointer_to_real), ALLOCATABLE, DIMENSION(:) :: px=0D INTEGER n, i=0D PRINT*, 'Enter n:'=0D READ*, n=0D ALLOCATE(x(n),px(n))=0D PRINT '(1X,A,I2,A)', 'Enter ', n, ' values:'=0D READ*, x=0D DO i=3D1,n=0D px(i)%ptr =3D> x(i)=0D END DO=0D CALL bubble_sort(n, px)=0D PRINT '(/,1X,"Original list:")'=0D DO i=3D1,n=0D PRINT*, x(i) =0D END DO=0D PRINT '(/,1X,"Sorted list:")'=0D DO i=3D1,n=0D PRINT*, px(i)%ptr =0D END DO=0D DEALLOCATE(x, px)=0D END PROGRAM=0D =0D =0D Kruger in his book ("Efficient FORTRAN Programming", 1990) states:=0D =0D "Standard FORTRAN [77] does not have data structures such as records, or ex= plicit pointers, but this should not exclude their use in programs. All the= important data structures-arrays, records, lists, stacks, queues, and so o= n,-can be implemented in FORTRAN [77], admittedly sometimes not as clearly = as in other languages, but implemented nonetheless."=0D =0D Here is a FORTRAN 77 program that employs an array of pointers (KEY):=0D =0D PROGRAM ORDER=0D IMPLICIT NONE=0D INTEGER NMAX, I, N=0D PARAMETER(NMAX =3D 50)=0D REAL A(NMAX)=0D INTEGER KEY(NMAX)=0D PRINT*, 'Enter N, A(1), ..., A(N): '=0D READ*, N, (A(I), I =3D 1, N)=0D DO 1 I =3D 1, N=0D KEY(I) =3D I=0D 1 CONTINUE=0D CALL BUBBLE(N, A, KEY)=0D PRINT*, ' '=0D PRINT*, 'The values in ascending order are: '=0D PRINT*, (A(KEY(I)), I =3D 1, N)=0D END=0D =0D SUBROUTINE BUBBLE(N, ARR, KEY)=0D IMPLICIT NONE=0D INTEGER N=0D REAL ARR(N)=0D INTEGER KEY(N)=0D INTEGER TEMP, I, J=0D LOGICAL SORTED=0D DO 1 I =3D 1, N-1=0D SORTED =3D .TRUE.=0D DO 2 J =3D 1, N-I=0D IF(ARR(KEY(J)).GT.ARR(KEY(J+1)))THEN=0D TEMP =3D KEY(J+1)=0D KEY(J+1) =3D KEY(J)=0D KEY(J) =3D TEMP=0D SORTED =3D .FALSE.=0D ENDIF=0D 2 CONTINUE=0D IF(SORTED) RETURN=0D 1 CONTINUE=0D END=0D =0D =0D It appears to me that the FORTRAN 77 implementation is simpler (easier to w= rite and understand) than the Fortran 90 version (of course, the FORTRAN 77= version is also a valid Fortran 90 program. You know what I mean). What do= you think? =0D =0D Best wishes to all,=0D =0D Omer Akgiray=0D Dept. of Environmental Engineering=0D Marmara University, Istanbul=0D |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: [LF] arrays versus pointers | Lahey Support | Archive | 0 | 08-15-2003 04:28 PM |
| Re: [LF] arrays versus pointers | Lahey Support | Archive | 0 | 08-15-2003 04:28 PM |
| Re: [LF] arrays versus pointers | Lahey Support | Archive | 0 | 08-15-2003 04:28 PM |
| Re: [LF] arrays versus pointers | Lahey Support | Archive | 0 | 08-15-2003 04:28 PM |
| Re: [LF] arrays versus pointers | Lahey Support | Archive | 0 | 08-15-2003 04:28 PM |