CSCE 150 Exam I 10/25/2000
Name: SS#:
(1) (30 points) Write a Fortran 90 FUNCTION which calculates N!. Call the function MyFunction. The input for the function is a positive integer N and the output is N!.
Solution:
FUNCTION MyFunction(N)
IMPLICIT
NONE
INTEGER
:: MyFunction, I
INTEGER,
INTENT(IN) :: N
MyFunction=1
DO
I=1, N
MyFunction=MyFunction*I
END DO
END FUNCTION MyFunction
(2) (45 points) Find the output of the following segments:
(a) In the following SUM is a variable of type INTEGER.
---------------------
SUM=0
DO J=1,3
SUM=SUM+J*J
END DO
PRINT *, “Sum= “, SUM
--------------------
Output: Sum= 14
(b) In the following X and Y are variables of type REAL.
-----------------
X=2.3
IF (X<1.0) THEN
Y=100*X
ELSE IF (X>2.5) THEN
Y=10*X
ELSE
Y=2*X
END IF
PRINT *, “The value of Y is: “, Y
------------------
Output: The value of Y is: 4.6
(c) Find the value of MyProd(3).
----------------
FUNCTION MyProd(K)
IMPLICIT NONE
INTEGER, INTENT(IN) :: K
INTEGER :: MyProd, I
MyProd=1
DO I=1, K
MyProd=MyProd*2
END DO
END FUNCTION MyProd
------------------
MyProd(3)=8
(3) (25 points) Find all the errors in the following program:
PROGRAM MyProgram
IMPLICIT NoNe
INTEGER : L, M, N !Error here; should be INTEGER :: L, M, N
N=MOD(7:N) !Error here
for two reasons.
N=2
IF (N=3) THEN !Error here; should be (N==3)
PRINT *, “There is an error in this statement”
END IF
M=My_Function(2)
CONTAINS
FUNCTION My_Function(R)
INTEGER, INTENT(IN) :: R
!Error here; should have INTEGER :: My_Function
IF (R<2) THEN
My_Function=R
ELSE
R=2*R !Error here; cannot change the value of R.
My_Function=R
END IF
END My_Function !Error; should be END FUNCTION My_Function
PRINT *, “M= “, M !Error; cannot have this statement here.
END MyProgram !Error; should be END PROGRAM My_Program