Welcome to AE Resources
Converted document Converted document

FORTRAN 90+: FORMATTING EXAMPLE

It is recommended to look at the numerical methods codes where the use of formatting is demonstrated. Some additional examples using code snippet are included here.
EXAMPLE
The following is an example of how to format a matrix so that it prints out in a clear manner. It is important to remember here that the WRITE function reads a matrix vertically. This means that the second position of an array called ’E’ it reads will be E(2, 1). This means the programmer must add two more steps before writing a matrix, or use a DO loop. Both are shown below using a 2x4 matrix.
[1] INTEGER, DIMENSION(1:2,1:4) ::e 
[2] INTEGER, DIMENSION(1:4,1:2) ::f 
[3] INTEGER::i,j 
[4] e(1,1:4)=(/1,2,3,4/) 
[5] e(2,1:4)=(/5,6,7,8/) 
[6] f=TRANSPOSE(e) 
[7] WRITE(*,"(2(4(I3)/))") f 
[8] DO i=1,2 
[9] WRITE(*,"(4(I3))") (e(i,j),j=1,4) 
[10] END DO
Which results in
1 2 3 4
5 6 7 8
     
1 2 3 4
5 6 7 8
The first part of the code creates the matrix. Line [6] transposes the matrix. This is because the WRITE function walks through the matrix from top to bottom instead of from left to right. This means that any attempt to write the matrix with out first transposing it the first output would yield
1 5
2 6
3 7
4 8
Lines [8]-[10] use a DO loop to walk through the matrix in the correct order. This is a more appropriate method. The other method was shown only to demonstrate the order that the WRITE function reads the matrix.