TE-241 COMPUTER PROGRAMMING Second Midterm Examination Lecturer: Dr. Ahmet ERKLİĞ Date : 02-01-2009 Student Name: Duration: 90 mins. Q1. (25%) What is the output of the following FORTRAN programs? PROGRAM Q1A REAL:: X, Y OPEN(UNIT=1,FILE='EXAM.DAT', STATUS='NEW') WRITE(UNIT=1, FMT='(I2,F5.2,I2)') 41,& 8.316, 26 CLOSE(UNIT=1) PROGRAM Q1B INTEGER::I, Y I = 1 CALL X(I) PRINT *, Y(I) END PROGRAM Q1B OPEN(UNIT=2,FILE='EXAM.DAT', STATUS='OLD') READ(UNIT=2, FMT=*) X, Y PRINT '(1X,F7.3)', X + Y CLOSE(UNIT=2) END PROGRAM Q1A SUBROUTINE X(J) INTEGER::J, Y J = Y(4) + Y(J) CALL Z(J) END SUBROUTINE X INTEGER FUNCTION Y(K) INTEGER:: K Y = K - 1 END FUNCTION Y SUBROUTINE Z(I) INTEGER::I, J J = 2 I = I + J END SUBROUTINE Z Q2. (25%) Write a function named SUM_FROM_TO that takes two integer arguments, called FIRST and LAST and returns as its value the sum of all the integers between FIRST and LAST inclusive. Thus, for example, PRINT*, SUM_FROM_TO(4,7) PRINT*, SUM_FROM_TO(-3,1) PRINT*, SUM_FROM_TO(7,4) PRINT*, SUM_FROM_TO(9,9) ! ! ! ! ! ! ! will print 22 because 4+5+6+7 = 22 will print -5 because (-3)+(-2)+(-1)+0+1 = -5 will print 22 because 7+6+5+4 = 22 will print 9 Note that, as the third example shows, the arguments to the function are not necessarily in ascending order. Also, as in example 4, the arguments to the function maybe equal. Q3. (25%) Write a subroutine Bankamatik(Euros) that performs the following task: A request for money from a cash dispenser (bankamatik) is passed to the subroutine via the argument Euros. The subroutine calculates and then outputs the number of one, five, ten, and twenty Euro coins/notes such that the least number of coins/notes are given. For example, if argument Euros = 37, then the result of the subroutine is the output: The number of twenties is 1 The number of tens is 1 The number of fives is 1 The number of ones is 2 What is the output of your subroutine for: Euros = 127 ? Q4. (25%) Write a FORTRAN program that can read (10 x 10) matrix from an input file and calculate the average of all the data in each row and in each column of the matrix. INTEGER FUNCTION SUM_FROM_TO(FIRST, LAST) INTEGER FIRST, LAST, I SUM_FROM_TO = 0 IF (FIRST .LT. LAST) THEN DO I = FIRST, LAST SUM_FROM_TO = SUM_FROM_TO + I ENDDO ELSE IF (FIRST .GT. LAST) THEN DO I = FIRST, LAST, -1 SUM_FROM_TO = SUM_FROM_TO + I ENDDO ELSE SUM_FROM_TO = FIRST ENDIF