【问题标题】:How to use dimension x(n) in main program in Fortran? [duplicate]如何在 Fortran 主程序中使用维度 x(n)? [复制]
【发布时间】:2022-01-25 06:19:55
【问题描述】:

我想在 Fortran 中使用维度制作 x(1),x(2),x(3),...,x(n)

例子:

n=10 
dimension x(n) 
do n=1,10
print*,x(n)
enddo
stop
end

但据我所知是不允许的...
我得到这个错误:

规范表达式中不允许符号 N 声明中的错误,不再对 main 进行处理

我怎样才能使x(1),x(2),x(3),...,x(n)

【问题讨论】:

    标签: arrays fortran dimensions


    【解决方案1】:

    最好是拿一本好的教科书来学习 Fortran,看看 Fortran 语言有哪些可能性。

    有多种方法,例如使用可分配的:

    integer, parameter :: n=10 
    double precision :: x(:)
    integer :: i
    allocate(x(n))
    
    do i=1,n
      x(i) = i
    enddo
    
    do i=1,n
      print*,x(i)
    enddo
    
    end
    

    或使用固定数组:

    integer, parameter :: n=10 
    double precision :: x(n
    integer :: i
    
    
    do i=1,n
      x(i) = i
    enddo
    
    do i=1,n
      print*,x(i)
    enddo
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      相关资源
      最近更新 更多