Question: In Fortran Write the determinant function without modifying the main program. Review main program to see how an allocatable variable can be deallocated and reallocated
In Fortran
Write the determinant function without modifying the main program. Review main program to see how an allocatable variable can be deallocated and reallocated with a different size. There should be an if(...)then: else if (...) then : endif(...) structure.
The correct answers are 13 and 6
Here is the code:
program MainProgram
implicit none integer :: n real, allocatable :: Matrix1(:,:) real :: detM, Determinant ! NOTE how the function name 'Determinant' has to be declared with a type. ! This is so the program knows what kind of variable it returns. ! Compute the determinant of a 2x2 matrix. n = 2 allocate(Matrix1(n,n)) Matrix1(1,1:2) = (/ 3.0, 4.0 /) Matrix1(2,1:2) = (/ -1.0, 3.0 /) print*,'2x2 Determinant is: ', Determinant(Matrix1,n) deallocate(Matrix1) ! Compute the determinant of a 3x3 matrix. n = 3 allocate(Matrix1(n,n)) Matrix1(1,1:3) = (/ 1.0, 4.0, 3.0 /) Matrix1(2,1:3) = (/ -1.0, 2.0, 1.0 /) Matrix1(3,1:3) = (/ 2.0, 2.0, 3.0 /) print*,'3x3 Determinant is: ', Determinant(Matrix1,n) deallocate(Matrix1) end program MainProgram
function Determinant(M, n) result(Det)
! Write this function so that it can compute the determinant of a 2x2 or ! 3x3 matric depending on the value of n.
end function Determinant
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
