CSCE 150 Quiz4 Oct 4, 00
Name: SS#:
Do the first question and then do either the second question or the third.
(1) Find the output of the following Fortran 90 program. Assume that there are no errors in the program.
PROGRAM MyProgram
IMPLICIT NONE
INTEGER :: M
M=MyFunction(15)
PRINT *, “The result is: “, M
CONTAINS
FUNCTION MyFunction(k)
INTEGER :: MyFunction
INTEGER, INTENT(IN) :: k
MyFunction=8
IF (k >10) THEN
MyFunction=MyFunction+2*k
ELSE
MyFunction=999
END IF
END FUNCTION MyFunction
END PROGRAM MyProgram
Answer: the output is:
The result is: 38
(2) Write a Fortran 90 function (call it MyFunction3) which has one argument only called k (where k is a positive integer), and which finds the sum of all integers which are greater than or equal to 1 and less than or equal to k.
Solution:
FUNCTION MyFunction3(k)
IMPLICIT
NONE
INTEGER
:: MyFunction3, I
INTEGER,
INTENT(IN) :: k
MyFunction3=0
DO
I=1, k
MyFunction3=MyFunction3+I
END DO
END FUNCTION MyFunction3
(3) Find the errors in the following Fortran 90 program:
PROGRAM MyProgram2
IMPLICIT NONE
INTEGER :: N
N=6
PRINT *, “The result is: ”,
MyFunction2(N)
CONTAIMS !
Error: should be CONTAINS not CONTAIMS
FUNCTION MyFunction2(N)
INTEGER, INTENT(IN) :: N
INTEGER :: MyFunction2(N), I
! Error above: Should be MyFunction2 not MyFunction2(N)
MyFunction2=1
DO I=1, N
IF (MOD(I,2)==0) THEN
MyFunction2=2*MyFunction2
END IF
END DO
N=N+1 ! Error: can’t
change the value of N.
END FUNCTION MyFunction2(N)
! Error above: Should be MyFunction2 not MyFunction2(N)
PRINT *, “Good Job”
!Error above: can’t have the above statement in this place.
END PROGRAM MyProgram2