Search code examples
fortrangfortranfortran90intel-fortranfortran95

Writing a Matrix Array of 2 Rows by 3 Columns, To an Output Text File in Fortran 95


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

Solution

  • 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