Numerical Computations
Fortran and C/C++ for Numerical Computation
Suppose you are writing a Fortran Subroutine to implement the Gauss Elimination method to solve a set of linear simultaneous equations. The main program and the Gauss subroutine would look as follows:
C234567890
PARAMETER (MAXSZ = 1000)
REAL A(MAXSZ*MAXSZ), X(MAXSZ), B(MAXSZ)
WRITE(*,*) 'Enter number of equations'
READ(*,*) M
IF (M .GT. MAXSZ)
WRITE(*,*) 'Increase MAXSZ. Program aborted'
ENDIF
WRITE(*,*) 'Enter coefficient matrix row by row'
CALL READMAT(M, M, A)
WRITE(*,*) 'Enter right hand side vector'
CALL READMAT(M, 1, B)
CALL GAUSS(M, A, B, X)
WRITE(*,*) 'Solution Vector'
CALL PRINTMAT(M, 1, X)
STOP
END
C--- Subroutine to read a matrix of M rows and N columns, row by row
SUBROUTINE READMAT(M, N, A)
REAL A(M, N)
DO I = 1, M
READ(*,*) (A(I,J), J = 1, N)
END DO
RETURN
END
C--- Subroutine to solve a set of linear simultaneous equations
SUBROUTINE GAUSS(M, A, B, X)
REAL A(M, M), B(M, 1), X(M, 1)
......
RETURN
END
There are several points worth noting
A one dimensioned array in the main program may be viewed as a two-dimensioned (or multi-dimensioned) array inside a subroutine, as long as you understand how it is to be done.
When passing an array to a subroutine, its size (number of rows and columns) must also be sent as arguments to the subroutine. Subroutine does not automatically know the size of the array argument it receives.