【问题标题】:Loop in Fortran from a list从列表中循环 Fortran
【发布时间】:2012-03-20 16:35:49
【问题描述】:

我使用 Fortran,我想知道是否可以制作类似的东西

  do i = array
    write (*,*) i
  end do

其中数组是不一定有序的整数列表。

【问题讨论】:

    标签: loops for-loop fortran


    【解决方案1】:

    我会引入第二个索引来遍历数组的元素:

    program test
    
      implicit none
    
      integer, dimension(6)  :: A
      integer, dimension(10) :: B
      integer                :: i, j
    
      A = (/ 1, 3, 4, 5, 8, 9 /)
      B = (/ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 /)
    
      do j = 1, size(A)
         i = A(j)
         write(*,*) i, B(i)
      end do
    
    end program test
    

    【讨论】:

      【解决方案2】:

      你的意思是你想写一个名为other_array的数组的一些元素,但不是全部,并且i应该依次取任意值?换句话说你不想打印

      do i = 1, size(other_array,1)
          write(*,*) other_array(i)
      end do
      

      但是类似

      array = [1,3,4,2,3,7,8,8,12]
      write(*,*) another_array(array)
      

      哪个会写入array 中指定的another_array 的元素?这称为数组下标。我还没有测试过这个,我现在要出去所以不会。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-24
        • 1970-01-01
        • 2014-02-25
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 2011-07-27
        • 1970-01-01
        相关资源
        最近更新 更多