Computer Science 117 Summer 2002, Session III
Test, July 26, 2002
The entire test is 80 points.
-
Assume that M and N are integer variables with the values -3 and 5,
respectively, X and Y are real variables with the values -3.57
and 4.78, respectively. Find the type and value of each expression.
Do not request permission to use a calculator. It's not necessary,
nor even helpful. [5 points each]
-
NINT(X+Y+1/2)
INTEGER, 1 (1/2 has the value 0)
-
SQRT(Y) < N
LOGICAL, true
-
3.5 > REAL(7/2)
LOGICAL, true
-
MOD(M+12,N)
INTEGER, 4
-
Write a logical expression to express the given condition. You may
assume that X, Y are variables of type REAL, M, N are variables of type
INTEGER, and P, Q are variables of type LOGICAL.
[5 points each]
-
At least one of the variables X, Y, and Z, has a positive value.
X > 0 .OR. Y > 0 .OR. Z > 0
-
Either P is true, or Q is false.
P .OR. .NOT. Q
-
0 < M < N
0 < M .AND. M < N
-
Either N is zero, or the fraction M/N is greater than 1/2.
(Warning: evaluation of this expression must never result in a run-time error.)
N == 0 .OR. N > 0 .AND. M > 2*N .OR. N < 0 .AND.
M < 2*N
-
Write a block of Fortran 90 code which prints out all the integers from
1 to N in reverse order. You may assume that I and N
are variables of type INTEGER.
Do not write any specification statements or any input statements.
[10 points]
DO I = N,1,-1
PRINT*,I
END DO
-
Write a block of Fortran 90 code which sets a variable Sum of type
INTEGER equal to the sum of the squares of all even positive integers which
do not exceed the value of a variable N of type INTEGER. Use I as
the index of your loop; you may assume that I is a variable of type
INTEGER.
Do not write any specification statements or any input or output statements.
[10 points]
SUM = 0
DO I = 2,N,2
SUM = SUM + I*I
END DO
-
Write a block of Fortran 90 code which sets a variable X of type REAL
equal to the square root of the value of a variable N of type INTEGER,
provided the value of N is positive.
If the value of N is not positive, X should be set to 0.
Do not write any specification statements or any input or output statements.
[10 points]
IF (N > 0) THEN
X = SQRT(REAL(N))
ELSE
X = 0.0
END IF
-
Consider the following block of FORTRAN 90 code, where I and N are both
variables of type integer:
I = 1
DO WHILE (I < N)
PRINT*,I
I = 2*I
END DO
What will the output be if the value of N is 1000? [5 points]
1
2
4
8
16
32
64
128
256
512
-
Recall Project 2, where you computed the area of triangle, given the
lengths of the three sides a, b, c, using the formula
sqrt(s(s-a)(s-b)(s-c)), where s is the semi-perimeter.
In the program that you wrote for this project, what was the identifier
you used for the semi-perimeter? (I don't want to know what identifier
you think you should have used, but the one that you actually used.)