将3列的2行矩阵数组写入Fortran 95中的输出文本文件

问题描述 投票:0回答:2

我目前正在学习如何编写矩阵阵列来输出Fortran 95中的文本文件。我面临的问题是,我正在研究的2行3列矩阵数组,并不符合我的要求。输出文本文件。我相信,我错过了一行或两行代码,或者没有在当前的代码行中添加一些代码。下面是我的代码行,当前输出数据和期望输出数据。目标是获得“期望的输出数据”。请告诉我我的错误,我错过了哪些代码/代码行以及我应该在哪里添加代码/代码行。每个答案都受到欢迎和赞赏。谢谢Stackovites。

代码行:

Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,3,5,2,4,6/
Do k= 1,2
  B(2,3)= k
!Write to File at Machine 8 and show the formatting in Label 11
Write(8,11) B(2,3)
11 format(3i3)
    End Do
  Do j= 3,6
    B(2,3)= j
!Write to File at Machine 8 and show the formatting in Label 12
Write(8,12) B(2,3)
12 format(3i3)
End Do
End Program Format2

电流输出数据

  1
  2                                        
  3                                      
  4                                         
  5
  6

期望的输出数据

1  3  5
2  4  6
fortran gfortran fortran90 intel-fortran fortran95
2个回答
0
投票

B(2,3)仅指数组中的一个特定元素。即第一维中索引为2且另一维中索引为3的元素。要引用不同的元素,请使用B(i,j),其中ij是具有所需索引的整数。要引用整个数组,只需使用BB(:,:)作为包含整个数组的数组部分。

所以要设置值

do j = 1, 3
  do i = 1, 2
    B(i,j) = i + (j-1) * 2
  end do
end do

并打印它们使用在这个网站上无数重复显示的方法之一(Print a Fortran 2D array as a matrix Write matrix with Fortran How to write the formatted matrix in a lines with fortran77? Output as a matrix in fortran - 搜索更多,会有更好的...)

do i = 1, 2
   write(8,'(999i3)') B(i,:)
end do

-1
投票

我见过我的错误。我给Fortran编译器的说明是我在My Output Text File中得到的结果。我宣布两(2)行(1,2)和(3,4,5,6);而不是宣布(1,2)的三三列; (3,4)和(5,6)。下面是获取所需输出数据的正确代码行。

Lines of Codes:

Program Format2

!illustrates formatting Your Output

Implicit None

Integer:: B(2,3)

Integer:: k,j

!Open Output Text File at Machine 8

Open(8,file="formatoutput2.txt")

Data B/1,2,3,4,5,6/

!(a)Declare The 1st Two-2 Values You want for k Two-2 Rows, that is (1 and 2)

!(b)Note, doing (a), automatically declares the values in Column 1, that is (1 and 2)

Do k= 1,2

  B(2,3)= k

    End Do

!(c)Next, Declare, the Four Values You want in Column 2 and Column 3. That is [3,4,5,6]

!(d) Based on (c), We want 3 and 4 in "Column 2"; while "Column 3", gets 5 and 6.

Do j= 3,6

 B(2,3)= j

 End Do

!Write to File at Machine 8 and show the formatting in Label 13

Write(8,13) ((B(k,j),j= 1,3),k= 1,2)

13 format(3i3)

End Program Format2

The Above Lines of Codes, gives the Desired Output below:

1   3   5

2   4   6
© www.soinside.com 2019 - 2024. All rights reserved.