# Print the Upper Triangular Part of a Square Matrix ### Problem Statement

Suppose we have an input of the following form. The first line gives the number of rows (and columns) of a square matrix. Each of the subsequent input lines contains the values of a row of the matrix in the form of (10I5). Note that if a matrix has fewer than ten rows (and columns), there will be less than 10 numbers on an input line.
```         1    1    2    2    3
....5....0....5....0....5....0
6
35   70   52   83   98   68
12   76   35   24   9    27
46   49   53   57   15   29
66   87   25   40   93   21
100  65   77   88   4    51
83   12   19   54   7    100
```

Write a Fortran program that reads in this matrix and prints the upper triangular part. The upper triangular part of a square matrix consists of all entries on and above the diagonal.

```         1    1    2    2    3    3
....5....0....5....0....5....0....5
Input Matrix:
35   70   52   83   98   68
12   76   35   24    9   27
46   49   53   57   15   29
66   87   25   40   93   21
100   65   77   88    4   51
83   12   19   54    7  100

Upper Triangular Part:
35   70   52   83   98   68
76   35   24    9   27
53   57   15   29
40   93   21
4   51
100
``` ### Solution

```PROGRAM  UpperTriangularMatrix
IMPLICIT   NONE
INTEGER, PARAMETER                :: SIZE = 10
INTEGER, DIMENSION(1:SIZE,1:SIZE) :: Matrix
INTEGER                           :: Number
INTEGER                           :: Position
INTEGER                           :: i, j
CHARACTER(LEN=100)                :: Format

DO i = 1, Number
READ(*,"(10I5)")  (Matrix(i,j), j = 1, Number)
END DO

WRITE(*,"(1X,A)")  "Input Matrix:"
DO i = 1, Number
WRITE(*,"(1X,10I5)")  (Matrix(i,j), j = 1, Number)
END DO

WRITE(*,"(/1X,A)") "Upper Triangular Part:"
Position = 2
DO i = 1, Number
WRITE(Format,"(A,I2.2,A)")  "(T", Position, ", 10I5)"
WRITE(*,Format)  (Matrix(i,j), j = i, Number)
Position = Position + 5
END DO
END PROGRAM  UpperTriangularMatrix
```
Click here to download this program. ### Program Input and Output

If the input data consist of the following:
```         1    1    2    2    3
....5....0....5....0....5....0
6
35   70   52   83   98   68
12   76   35   24   9    27
46   49   53   57   15   29
66   87   25   40   93   21
100  65   77   88   4    51
83   12   19   54   7    100

```
The output of the program is:
```         1    1    2    2    3    3
....5....0....5....0....5....0....5
Input Matrix:
35   70   52   83   98   68
12   76   35   24    9   27
46   49   53   57   15   29
66   87   25   40   93   21
100   65   77   88    4   51
83   12   19   54    7  100

Upper Triangular Part:
35   70   52   83   98   68
76   35   24    9   27
53   57   15   29
40   93   21
4   51
100
``` ### Discussion

Similar to the previous example, this one also looks easy. Without the help of internal files, it could be very difficult.
• If you look at the output, you will see that each entry is printed with I5. If the number of rows (and columns) is 6, then the first row has 6 numbers and one leading space, the second row has 6 leading spaces and 5 numbers, the third row has 11 leading spaces and four numbers and so on. In general, if each entry is printed with I5, then the first positions for the numbers are 2, 7, 12, 17, 22, 27 and so on. Note that we start with position 2 since we need position 1 for printer control.
• Based on this observation, the first row is printed with (T2,10I5), the second row is printed with (T7,10I5), the third row is printed with (T12,10I5), the fourth row is printed with (T17,10I5) and so on. The position for Tc starts with 2 and has a step size 5. Therefore, a generic format looks like the following:
```(T??, 10I5)
```
If we could find some way to fill in ??, we will have a good format. Note that not all I5s in 10I5 will be used. If a WRITE has fewer than 10 data values, the unused I5s will be ignored. See Scanning a Format: I for the details.
• Let us try to construct a format in the following way:
```CHARACTER(LEN=100)  :: Format

WRITE(Format,"(A,I2.2,A)")  "(T", Position, ", 10I5)"
```
Here, Position is an INTEGER variable whose initial value and step size are 2 and 5, respectively. Therefore, if the value of Position is 7, the above WRITE generates the following into CHARACTER variable Format:
```(T07, 10I5)
^^
||
++----- printed with I2.2
```
Note that we use I2.2 rather than I2 because we do not want to have a space between T and 7.
• The remaining part is easy and will not be discussed.