子程序与数组元素作为参数 [英] Subroutine with array element as argument

查看:199
本文介绍了子程序与数组元素作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划子程序 stlstp 通过工作(2,1)作为参数传递给 stlfts(...)子程序。 工作(2,1)将是指数在双重价值,但如何子程序将其转化为一维数组 X(N)

当我打印 X stlfts(...)子程序的价值,这是<$印刷的元素ç$ C> N 尺寸,例如:

  STLFTS ....点¯x,,, 0.0000000000000000 1.4964418382246345E-317 1.48978578
58438612E-317 1.4964339331743010E-317 1.4964418382246345E-317 4.3367611401 ....

我的理解是,除了第一个值,所有其他值在这个 X 都是垃圾,但不知道这个任务将如何会影响原来的参考?有人可以帮我这个?

 子程序stlstp(Y,N,NP,NS,NT,NL,isdeg,itdeg,ildeg,nsjump,ntjump,nljump,妮,userw,RW,季节,潮流,做工)!隐无
!精氨酸
      整数n,NP,NS,NT,NL,isdeg,itdeg,ildeg,nsjump,ntjump,nljump,妮
      逻辑userw
      双precision Y(N),RW(N),季节(N),趋势(N),工作(20,5)
!瓦尔
      整数I,J     做80 J = 1,1
         做1 I = 1,N
              工作(I,1)= Y(I) - 趋势(ⅰ)
 1继续
         调用stlss(Y,N,NP,NS,isdeg,nsjump,userw,RW,季节)         PRINT *,'WORK 1,2 ......,工作(1,2)
         PRINT *,'工作......,工作
         电话stlfts(工作(1,2),N + 2 * NP,NP,工作(1,3),工作(1,1))

现在 stlfts 子程序code是:

 子程序stlfts(X,N,NP,趋势,工作)
    整数n,NP
    双precision X(N),趋势(N),工作(N)
    PRINT *,'STLFTS ....点¯x,,,',X
    调用stlma(X,N,NP,趋势)
  结束


解决方案

感谢,向为IanH方向的标准(2008年的Fortran 12.5.2.11(1)),得到的答案是错误的有关标准一致性的声明:


  

再$ P $的实际参数psents元素序列,如果它是一个数组
  前pression,的数组元素代号,默认字符标量,
  或用C字样(15.2.2)字符类型的标量。如果
  实际参数是一个数组前pression,元素序
  在于数组元素的顺序元素。 如果实际
  参数是一个数组元素代号,该元素序列由
  它后面的数组的数组元素和每个元素的
  元素顺序。


这意味着这是实践符合标准的,这是一个合法的方式如何通过一些元素开始阵列的一部分。

这种情况可以 stlstp 通过实际点的地址原始数组中解释C程序员的子程序。

子程序 stlfts 然后跨$ P $点的地址长度的原始数组的一部分的地址 N 。这将是第一个在列优先顺序 N 元素从元素开始(1,2)。这样,它获取访问原始数组的其他元素工作,结果不会是垃圾。

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

解决方案

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.

这篇关于子程序与数组元素作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆