Subroutine with array element as argument

五迷三道 提交于 2019-12-20 03:30:26

问题


In my program subroutine stlstp passing work(2,1) as parameter to stlfts(...) subroutine. work(2,1) will be double value at that index, but how subroutine converting it as single dimension array x(n)?

When I print x value in stlfts(...) subroutine, it is printing elements of n size, example:

 STLFTS....X,,,   0.0000000000000000        1.4964418382246345E-317   1.48978578
58438612E-317   1.4964339331743010E-317   1.4964418382246345E-317   4.3367611401 ....

My understanding is, except 1st value, all other values in this x are garbage, but not sure how this assignment will will effect original reference? can someone please help me with this?

subroutine stlstp(y,n,np,ns,nt,nl,isdeg,itdeg,ildeg,nsjump,ntjump,nljump,ni,userw,rw,season,trend,work)

!     implicit none
! Arg
      integer n,np,ns,nt,nl,isdeg,itdeg,ildeg,nsjump,ntjump,nljump,ni
      logical userw
      double precision y(n),rw(n),season(n),trend(n),work(20,5)
! Var
      integer i,j

     do 80 j = 1,1
         do 1 i = 1,n
              work(i,1) = y(i)-trend(i)
 1       continue
         call stlss(y,n,np,ns,isdeg,nsjump,userw,rw,season)

         PRINT *, 'WORK 1,2 ....',work(1,2)
         PRINT *, 'WORK ....',work
         call stlfts(work(1,2),n+2*np,np,work(1,3),work(1,1))

Now stlfts subroutine code is:

  subroutine stlfts(x,n,np,trend,work)
    integer n, np
    double precision x(n), trend(n), work(n)
    PRINT *, 'STLFTS....X,,,',x 
    call stlma(x,    n,      np, trend)
  end

回答1:


Thanks, to IanH for the direction to the standard (Fortran 2008 12.5.2.11(1)), the answer was wrong in the statement about the standard conformance:

An actual argument represents an element sequence if it is an array expression, an array element designator, a default character scalar, or a scalar of type character with the C character kind (15.2.2). If the actual argument is an array expression, the element sequence consists of the elements in array element order. If the actual argument is an array element designator, the element sequence consists of that array element and each element that follows it in array element order.

This means this praxis is standard conforming and this is a legal way how to pass a part of an array starting from some element.

The situation can be explained to C programmers as the subroutine stlstp passing the address of the actual point in the original array.

The subroutine stlfts then interprets the address as an address of a part of the original array of length n. It will be the first n elements in the column major order starting from the element (1,2). This way it gets access to other elements of the original array WORK and the result will not be garbage.



来源:https://stackoverflow.com/questions/25000321/subroutine-with-array-element-as-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!