308-208 - Computers in Engineering - Previous Midterms

# Midterm Fall 1994

1. (10 minutes, 10 points)

Classify each of the following by placing each one under the appropriate column in the table. Assume no explicit declarations.
.GT.     0       .T.          NAME*7     -10.7
+308       TEST0   _LESS_F123S  I
.FALSE.  10,512  *NEW         13.0001    NOT
\$20.00   ZERO    73202        .EQUAL.    RESULTS
• Valid Fortran REAL Variable
• Valid Fortran INTEGER Variable
• Valid Fortran INTEGER Constant
• Valid Fortran REAL Constant
• Valid Fortran LOGICAL Operator
• Valid Fortran RELATIONAL Operator
• Not Valid in Fortran

2. (10 minutes, 12 points)

What will the following program print?

INTEGER a, b, c
a = 13.0/4
b = 1.5**2
c = 3/(1.0 - 7) * 3
d = 7/3*2
e = 2*3**2
f = b - 1.5
PRINT *,a,b,c,d,e,f
END

3. (10 minutes, 15 points)

What will the following program print?

INTEGER P,Q
P=3
DO 10 Q=1,27,6
IF ( (2*P+Q .EQ. 9) .OR. (Q/3 .GT. P))  THEN
IF (Q .LT. P) THEN
PRINT *, 'TILL'
P = P + 3
ELSE
PRINT *,'QUIT'
P = P + 1
ENDIF
ELSEIF ( Q.EQ. 1) THEN
PRINT *, 'YOU'
IF (MOD(Q,P) .LT. 1) THEN
PRINT *, 'QUIT'
ELSE
PRINT *,'TRY'
ENDIF
ELSE
PRINT *, 'OR'
ENDIF
10     CONTINUE
PRINT *, 'NOW'
STOP
END

Question No. 4. (25 Minutes , 25 Points )

a) Write a subroutine FSWAP that will swap two floating point numbers. The two floating point numbers are to be passed as parameters F1 and F2. Only write the subroutine, not the main program.

b) Assuming the following code:

REAL X(100)
INTEGER N
IF (N .EQ. 0) STOP

GO TO 10
END
/DATA
30
<< Data for 30 values of X inserted here  >>
41
<< Data for 41 values of X inserted here  >>
0
Write code that will swap the last element of array X with the first, the second-to-last with the second, and so on. Note that N can be even or odd. Call your FSWAP routine, from section 4.a, to swap the elements.

c) Assuming the following code:

REAL X(100, 100)
INTEGER N
C       N is the number of rows and number of columns in the matrix
IF (N .EQ. 0) STOP

GO TO 10
END
/DATA
30
<< Data for 30*30 = 900 values of X inserted here  >>
41
<< Data for 41*41 = 1681  values of X inserted here  >>
0
Write code that will transpose the array X. The transpose of a matrix reflects each element of the matrix around the diagonal. Assume that X is a square matrix. Again, call FSWAP to swap the elements.
Question 5. (35 Minutes, 38 Points)

Write a program which accepts data lines with simple arithmetic expressions involving +,*,and -, and evaluates them.

For example:

Input           Output

37 + 5          37 + 5 = 42
127 - 14        127 - 14 = 113
4 * 80          4 * 80 = 320
• Your input is a sequence of lines. Each line consists of the following: a number (starting in column 1), a single blank, an operator (+,*,or -), a single blank, and the second number, followed by blanks. There is one and only one blank between portions. A blank in column 1 indicates end of file.
• Here a "number" means an unsigned integer constant, in standard decimal notation; that is a sequence of characters, each of which is one of 0,1,....,9.
• You do not know the number of characters in advance, hence you cannot use FORTRAN I conversion in a FORMAT statement.
• Your output should include an input echo, the operands and operator found, and the answer.
• You need not check for illegal input.
Bonus marks for implementing one of these additional features... Additional features might include:
• Checking for illegal input
• Signed numbers
• Input a base other than 10.
• Input in Roman numarals!
• An arbitrary number of blanks between fields instead of just one.

End of Midterm