Assignment H

This assignment is about computing the determinant of a square matrix. We will do this by transforming the matrix into upper triangular form, after which the determinant is product of the numbers on the main diagonal (upper left to lower right).

We will run this with several different input files, each of which provides the dimension of the matrix and the values in it.


Input File

The first line in the input file contains one small integer N such as 4. This is the size of the matrix (N by N). We will always have N < 11.

After that, we have N lines, each containing 4 REAL values separated by spaces. Each line is one row of the matrix.

We will test this with 3 input files which can find on the web site. You may want to test it with your own data as well. We will read the input files with input redirection.

What to do

Declare a 2-dimensional array of size 10 x 10. (We will end up using the upper left portion of it.)

Open the file and read the first line, providing the number N.

Read the rest of the file and store the numbers in the array. This can be done with two DO loops, or with a DO loop and an implied DO loop, or even with two implied DO loops nested.

Now what do we do? We work with one column at a time, left to right.

  1. Set SIGN = +1.
  2. Search the first column for a nonzero entry. If there are none, the determinant is 0 and we can stop. If you find such an entry, other than in the first row, switch its row with the first row and set SIGN = -1 * SIGN.
  3. Subtract multiples of the (current) first row from each other row so the first number in each of those rows becomes 0.
  4. Now repeat the last two steps referring to the second column (and leaving the first row alone).
  5. Now repeat steps 2 and 3 referring to the third column (and leaving the first two rows alone).
  6. By now the pattern should be clear. Continue until all numbers below the main diagonal are 0.
  7. The determinant is SIGN * the product of the numbers on the main diagonal.

The role of SIGN is this: if two rows of a matrix are switched, the determinant is multiplied by -1.

If an row or column if 0, the determinant is 0.

Print out the original matrix, the upper triangular form of the matrix and a line stating its determinant.


Notes

Another way to manage the array is to use what is called an "allocatable array", in which the dimensions can be specified at run time.

Your program should be well structured and properly documented. You may want to use more subroutines as well. Do not use any GO TOs. Do use IMPLICIT NONE.

You need to compile and link your program as usual.