LAST Name ________________________

First Name _______________________

MUSIC Code HC______

McGill University

Computers in Engineering

308-208A

Mid-term

Tuesday October 7th, 1997

2.30 - 3.50 p.m.

NO CALCULATORS allowed. All the programs below are in FORTRAN 90.

If you have time at the end of this test please feel free to

comment on the course content, the text, Website, diskette, facilities, the instructor, TAs and how things could be improved.

Question 1. (11 minutes, 15 points)

What will the following program output? Write your answers in the box below

program main

implicit none

integer :: a,b,c

real :: d,e,f

a = 32/4

b = 5.5-5*4.0

c = mod(17,6)-mod(11,3)+mod(14,4)

d = 17/3*5

e = a

f = ((a/2*c)**2)**2

print *,a,b,c,d,e,f

stop

end program main

Question 2: (11 minutes, 15 points)

What would be the values of integer variables X, Y and Z after execution of these statements if the initial values of X and Y are (a) 2 and 7 (b) -3 and 3 (c) 25 and 8

PROGRAM QUESTION2

IMPLICIT NONE

INTEGER:: X,Y,Z

Z=X

IF (X == Y) THEN

X=X**2

Y=(X+Y)/2

ELSE IF (X < Y) THEN

Y=Y**2

Z=Y-X

ELSE

IF (X > 0) THEN

Z = X/Y

ENDIF

Y=200

ENDIF

PRINT *,X,Y,Z

stop

end program question2

Question 3: (12 minutes, 15 points)

What will the following program print, given the input data below.

Program Question3

implicit none

integer :: i , j(100), k(100), n

integer :: a, b

do i = 1,n

end do

do i = 1,n

end do

do i = 1, n-1

a=k(j(i))

b=k(j(i+1))

print *,i,a,b, (a+b)

end do

Stop

End Program Question3

Input data is:-

6

4 1 6 5 3 2

13 27 18 10 23 9

Question 4: (11 minutes, 15 points)

What does this program prints?

PROGRAM Q4

IMPLICIT NONE

CHARACTER (len=1) :: LETTER(6), OUTPUT(11)

INTEGER :: INDEX(6)

!

LETTER(1) = 'V'

LETTER(2) = 'I'

LETTER(3) = 'A'

LETTER(4) = 'T'

LETTER(5) = 'L'

LETTER(6) = ' '

INDEX(1) = 1

INDEX(2) = 1

INDEX(3) = 6

INDEX(4) = 4

INDEX(5) = 5

INDEX(6) = 3

!

DO I = 1,11

MORE = MOD(I,4)

IF (MOD(I,2) == 0 .AND. MORE == 0) THEN

J = 3

ELSEIF (MOD(I,2) == 0) THEN

J = 2

ELSE

J = INDEX(I/2+1)

ENDIF

OUTPUT(I) = LETTER(J)

END DO

PRINT 27,OUTPUT

27 FORMAT(' ',11A1)

STOP

END PROGRAM Q4

Question 5 (12 minutes 15 points)

What does this program print?

PROGRAM Q5

! NOTE FORTRAN 90 INTERFACE sections and all declarations

! omitted, to save space

! Assume default data types e.g. all variables are REAL

A = 12.6

B = A / 2

C = A + B / 2

X = 2.8

Y = 2.3

Z = X + (B-1)*Y

CALL SPRINT (A, B, Z)

CALL BELL (C, X, Y)

X = MOD(14, 5)

Y = Y * A

Z = Y - X

B = Z - 1.2

!

PRINT *, A, B, C

PRINT *, X, Y, Z

STOP

END PROGRAM Q5

!

SUBROUTINE SPRINT(U, V, X)

U = (V + X) / 2.0

RETURN

END SUBROUTINE SPRINT

!

SUBROUTINE BELL(A, Y, Z)

A = A + Y * Z

RETURN

END SUBROUTINE BELL

Question 6. (23 minutes, 25 points)

Write a FORTRAN 90 program, which prints all 3-digit numbers that equal the sum of the cubes of their individual decimal digits

(e.g. 153 = 1**3 + 5**3 + 3**3 and show your output this way).

Print a suitable heading at the beginning, and after the last triplet is printed, print the number of triplets and their total.

Possible output follows:-

Cube Triplets - 3 digits cubed add to initial value

1 1 = 0**3 + 0**3 + 1**3

2 153 = 1**3 + 5**3 + 3**3

3 370 = 3**3 + 7**3 + 0**3

4 371 = 3**3 + 7**3 + 1**3

5 407 = 4**3 + 0**3 + 7**3

5 terms and their total is 1302

Use the back of pages as necessary.