0

I'm currently learning how to write Matrix Arrays to output Text Files in Fortran 95. The problem I'm facing is that, the Matrix Array of 2 Rows by 3 columns I'm working on, is not formatting to what I desire in the Output Text File. I believe, I'm missing One or Two Lines of Codes or failing to add a few codes to the current Lines of Codes I have. Below are My Lines of Codes, Current Output Data and Desired Output Data. The Goal is to get the "Desired Output Data". Kindly show me My mistake(s), what codes/line(s) of codes I'm missing and where I should add the codes/line(s) of codes. Every answer is welcomed and appreciated. Thank you Stackovites.

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

Current Output Data

  1
  2                                        
  3                                      
  4                                         
  5
  6

Desired Output Data

1  3  5
2  4  6
barbsan
  • 3,418
  • 11
  • 21
  • 28
Code Energy
  • 1
  • 1
  • 4
  • At every line, you assign a value to `B(2,3)` (and so don't use the array) and write a single element at a time `B(2,3)`. You can write strides of 3 integers with your format but don't actually give it three integers. Group your writes in two lines with the proper vector elements. – Pierre de Buyl Mar 20 '19 at 09:12
  • If you are learning to program in Fortran, do *not* learn `DATA` and try to use format strings instead of the labeled `FORMAT` statement. Do learn proper indentation so that the structure is apparent. – Vladimir F Героям слава Mar 20 '19 at 10:06

2 Answers2

0

B(2,3) refers only to one particular element of the array. Namely the element with index 2 in the first dimension and index 3 in the other dimension. To refer to a different element use B(i,j) where i and j are integers with the desired index. To refer to the whole array use just B or alternatively B(:,:) for an array section that encompasses the whole array.

So to set the values

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

and to print them use one of the methods showed in countless duplicates (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 -- search for more, there will be better ones...) on this site

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

I've seen My mistakes. The instructions I gave the Fortran Compiler, was the result I got in My Output Text File. I was declaring two-2 Rows of (1,2) and (3,4,5,6); instead of declaring Three-3 Columns of (1,2); (3,4) and (5,6). Below is the correct Lines of Codes to get the Desired Output Data.

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
Code Energy
  • 1
  • 1
  • 4