如何格式化&将指定顺序的两个数组的所需组件写入文本文件的每一行?

时间:2017-09-09 13:45:38

标签: fortran

这是我的代码:

Program Arrays

Implicit none

Integer::i
Integer,parameter,dimension(3,4)::Fir_array=0,Sec_array=1

Open(Unit=15,File='Output.txt',Status='Unknown',Action='Readwrite')

   Do concurrent(i=1:3)

      Write(15,'(1x,i0,".",4(2x,i0,1x,i0))') i,Fir_array(i,:),Sec_array(i,:)

   End Do

Close(Unit=15,Status='Keep')

End Program Arrays

Output.txt的内容是:

1.  0 0  0 0  1 1  1 1
2.  0 0  0 0  1 1  1 1
3.  0 0  0 0  1 1  1 1

我对此代码的意图是在Output.txt

中获取此内容
1.  0 1  0 1  0 1  0 1
2.  0 1  0 1  0 1  0 1
3.  0 1  0 1  0 1  0 1

如何使用do循环或暗示呢?

1 个答案:

答案 0 :(得分:0)

像往常一样,有不止一种方法可以解决这个问题,但我想到的第一件事就是将fir_arraysec_array的所需组件放入临时数组中,按所需顺序,然后打印。

! Add the following variables to your code:
integer, dimension(8) :: temp
integer :: d1

! Begin:
d1 = size(fir_array, dim=1)

do i = 1, d1
    temp([1,3,5,7]) = fir_array(i,:) !! If you're clever you can create a scheme to
    temp([2,4,6,8]) = sec_array(i,:) !! obtain the proper indices for arrays of any size.
    write(15, '(1x,i0,".",4(2x,i0,1x,i0))') i, temp
enddo

您可以获得所需的输出:

  1. 0 1 0 1 0 1 0 1
  2. 0 1 0 1 0 1 0 1
  3. 0 1 0 1 0 1 0 1