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

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

问题描述

在我的程序子例程 stlstp 中,将 work(2,1) 作为参数传递给 stlfts(...) 子例程.work(2,1) 将是该索引处的双值,但子程序如何将其转换为一维数组 x(n)?

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)?

当我在 stlfts(...) 子程序中打印 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 ....

我的理解是,除了第一个值,这个 x 中的所有其他值都是垃圾,但不确定这个赋值将如何影响原始引用?有人可以帮我解决这个问题吗?

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))

现在stlfts子程序代码为:

  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

推荐答案

感谢 IanH 对标准的指导(Fortran 2008 12.5.2.11(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:

如果是数组,则实参表示元素序列表达式,数组元素指示符,默认字符标量,或具有 C 字符类型 (15.2.2) 的字符类型的标量.如果实际参数是一个数组表达式,元素序列由数组元素顺序的元素组成.如果实际参数是一个数组元素指示符,元素序列包括该数组元素和数组中跟随它的每个元素元素顺序.

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.

这种情况可以向 C 程序员解释为子程序 stlstp 传递原始数组中实际点的地址.

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

子例程stlfts 然后将该地址解释为长度为n 的原始数组的一部分的地址.它将是从元素 (1,2) 开始的列主要顺序中的前 n 个元素.这样它就可以访问原始数组 WORK 的其他元素,结果不会是垃圾.

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天全站免登陆