0% found this document useful (0 votes)
105 views7 pages

Please Circle Your Section:: Key Solution (A)

This document contains instructions for a final exam in FORTRAN programming for the course ICS 101 at King Fahd University of Petroleum and Minerals. The exam contains 9 multiple choice and problem solving questions worth a total of 100 points. It provides the student's name, ID number, and section to fill out. The questions cover topics like FORTRAN formatting, array operations, file I/O, and conditional logic.

Uploaded by

Waleed Qattan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views7 pages

Please Circle Your Section:: Key Solution (A)

This document contains instructions for a final exam in FORTRAN programming for the course ICS 101 at King Fahd University of Petroleum and Minerals. The exam contains 9 multiple choice and problem solving questions worth a total of 100 points. It provides the student's name, ID number, and section to fill out. The questions cover topics like FORTRAN formatting, array operations, file I/O, and conditional logic.

Uploaded by

Waleed Qattan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

King Fahd University of Petroleum and Minerals

College of Computer Science and Engineering

Information and Computer Science Department


ICS 101 Computer programming in FORTRAN

Fall Semester 2009/2010 (091)

Final Exam (A) Wednesday, February 3, 2010


Time: 90 minutes

NAME Key Solution (A)

ID #

Please circle your section:

SM SM
AL-MULHEM 9:00-9:50 13:10-14:00
UT UT
MLAIH 9:00-9:50 11:00-11:50
UT UT UT
SECTION AL-HASHIM 8:00-8:50 10:00-10:50 13:10-14:00
SM UT UT
BAQAIS 11:00-11:50 7:00-7:50 14:10-15:00
SM SM SM SM
AL-YOUSEF 7:00-7:50 8:00-8:50 10:00-10:50 13:10-14:00

Question # Points Grade


1. 10
2. 10
3. 2
4. 8
5. 12
6. 10
7. 15
8. 15
9. 18
Total 100
Print*, “Good Luck”
Form (A)

Question 1 (10 POINTS):

What is the output of the following program?

B = 4.52
C = 8.958
D = 87.45
M = 724
PRINT 5, B, M, 'KFUPM'
5 FORMAT (1X, F5.3, I4, 2X, A)
PRINT 15, D, 'DHAHRAN', C
15 FORMAT ('0', F4.2, A9, F5.2)
PRINT 25, C, M, '1.999', B
25 FORMAT (' ', F6.4, 1X, I2, 1X, A3, F3.1)
END

1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
4 . 5 2 0 7 2 4 K F U P M

* * * * D H A H R A N 8 . 9 6
8 . 9 5 8 0 * * 1 . 9 4 . 5

Question 2 (10 POINTS):

What will be the values of array X after executing the following program?

INTEGER X(5,2)
OPEN (UNIT=10,FILE ='INPUT1.DAT',STATUS= 'OLD')
OPEN (UNIT=20,FILE ='INPUT2.DAT',STATUS= 'OLD')
READ (10,*)((X(I,J),J=1,2),I=2,4,2)
READ (20,*)((X(J,I),J=1,5,2),I=1,2)
DO 1 I = 1,5 X
1 PRINT*, (X(I,J),J=1,2)
END 14 19
INPUT1.DAT file INPUT2.DAT file
8 9
8 14
9 16
11 15 16 20
13 19
10 20
99 30
11 13

15 30
Question 3 (2 Points):

How many lines of 'ICS 101' does the following code print out?

DO 10 M=3,14,2
DO 10 N=12,1,-2
PRINT*, 'ICS 101' 36
10 CONTINUE
END
Form (A)

Question 4 (8 POINTS):

Assume that A is defined as:

INTEGER A(0:1,-2:1)

Assume also the storage of array A in the memory is as shown below:

Memory
7
10
9
6
1
3
1
4
What will be the output of the following code?

PRINT*,((A(K,J),J = -2,1,4),K = 0,1)


PRINT*,(A(1, J/4),J = 0,9,5)

7 10
3 4

Question 5 (12 POINTS):

What is the output of the following program?

Hint: Print question mark ? for any printed variable with un-initialized
(undefined) value.

INTEGER A(7), B(3), J, K


READ*,(A(K), K = 2, 7, 2)
READ*, B
DO 10 K = 1, 3
DO 20 J = 2, 7 , 2
IF (B(K).LT.A(J)) THEN
A(J-1) = B(K)
B(K) = A(J)
GOTO 10
ENDIF
20 CONTINUE
10 CONTINUE
PRINT*,(A(K), K = 1, 6, 2)
PRINT*,B
END

INPUT:
40 45 60 19
30 55 50

30 ? 50
40 60 60
Form (A)

Question 6 (10 POINTS)

[1] Consider the following subroutine


SUBROUTINE CHECK (N, X, Y)
INTEGER N
REAL X(10,10), Y
Y = 1
DO 5 K = N, 1, -1
5 Y = Y / X(K,K)
RETURN
END

Assume that R and T are declared in the main program as:

REAL R(10,10), T(10)

Which of the following CALL statements is correct?


a. CALL CHECK(10, R, T)
b. CALL CHECK(10, R, T(10))
c. CALL CHECK(10, R(10,10), T(10))
d. CALL CHECK(10, R(10,10), T)
e. None of the above

[2] Assume that A is a 2D array of size 3 by 5. Which of the


following blocks is EXACTLY equivalent to READ*, A statement?

a. b.

READ*,((A(K,J),K=1,5),J=1,3) READ*,((A(J,K),J=1,3),K=1,5)

d.
c.
DO 20 J = 1, 5
READ*, (A(K,J),K=1,3)
READ*,((A(K,J),J=1,5),K=1,3)
20 CONTINUE

5A
[3] To convert R  x  to FORTRAN statement, we write:
3B
a. R = ABS(x – (5*A)/(3*B))
b. R = CALL ABS(x – (5*A)/(3*B))
c. R = CALL (ABS(x – (5*A)/(3*B)))
d. R = CALL SUBROUTINE ABS(x – (5*A)/(3*B))
e. None of the above, because FORTRAN doesn’t have an intrinsic
function ABS

[4] Assume that you would like to open the file EXAM.DAT for writing,
and you don’t know if this file exists or not. In case you do NOT
want to overwrite the contents of the file if it already exists,
then what is the correct way to open this file?

a. OPEN(UNIT=3,FILE='EXAM.DAT',STATUS='OLD')
b. OPEN(UNIT=3,FILE='EXAM.DAT',STATUS='NEW')
c. OPEN(UNIT=3,FILE='EXAM.DAT',STATUS='UNKNOWN')
d. REWIND('EXAM.DAT')
e. REWIND(3)
Form (A)

Question 7 (15 POINTS):

Assume the following declarations

INTEGER X(6,9), SUM, MAX


INTEGER R, C

Answer the following questions based on the above declarations.

Note: Use R to represent row indices and C to represent column indices.

Complete the missing part to read all the elements of


array X row-wise from single input data line

READ*,(((X(R,C),C = 1,9),R = 1,6)


Complete the missing parts to print one column of X per line

DO 10 C = 1,9
PRINT*,(X(R,C),R = 1,6)
10 CONTINUE
Complete the missing parts to obtain the sum of the elements
of row 4

SUM = 0
DO 20 C = 1,9
SUM = SUM + X(4,C)
20 CONTINUE
Complete the missing parts to obtain the maximum element
value in column 2

MAX = X(1,2)
DO 30 R = 2,6
IF (X(R,2).GT. MAX) MAX = X(R,2)
30 CONTINUE
Form (A)

Question 8 (15 POINTS):

Given a data file INPUT.DAT that contains an unknown number of lines,


each line has a student ID, numeric grade NGRADE (out of 100) and letter
grade LGRADE. Write a program that reads the data from the above file and
writes the ID, NGRADE and LGRADE of the student having the lowest NGRADE
in the range of C+ (i.e. at the cutting edge for C+) to the file
OUTPUT.DAT

An example of the input file INPUT.DAT is as follows:

28000 93 A+
27000 78 B+
26000 50 D
. . .
. . .
. . .

Note: Assume that only one student is at the cutting edge of C+. Don’t
forget to close all opened files after you are done.

INTEGER ID, MINID


REAL NGRADE, MIN
CHARACTER LGRADE*2
OPEN(UNIT = 10, FILE = 'INPUT.DAT', STATUS = 'OLD')
OPEN(UNIT = 20, FILE = 'OUTPUT.DAT', STATUS = 'NEW')
MIN = 101
30 READ(10,*,END = 50) ID, NGRADE, LGRADE
IF (LGRADE.EQ.'C+'.AND.NGRADE.LT.MIN) THEN
MIN = NGRADE
MINID = ID
ENDIF
GOTO 30
50 WRITE(20,*) MINID,MIN,'C+'
CLOSE(10)
CLOSE(20)
END
Form (A)

Question 9 (18 POINTS):

Write a FORTRAN program that reads a 2-D integer array MAT of size 5x8
row-wise. It then reads an integer value M. The program should test all
elements in MAT on whether they are dividable by M or not. Every time the
program finds an element in MAT that is dividable by M, it stores the row
and column indices of the corresponding location into array LOC which is
declared of size 40x2. After finishing the search, the program should
print the number of elements in MAT that are dividable by M, and the row
and column indices of the locations of those elements. Output should be:

NO# OF ELEMENTS DIVIDABLE BY M IS: XX


LOCATIONS IN ARRAY MAT ARE:
ROW1 COLUMN1
ROW2 COLUMN2
. . . . . . .
ROWXX COLUMNXX

Where XX represents the number of elements in MAT that are dividable


by M. For example, if MAT(2,4) is the third element in MAT that is found
to be dividable by M, then LOC(3,1) = 2 and LOC(3,2) = 4.

NOTE: Declare (define) all used variables.

INTEGER MAT(5,8), LOC(40,2), M, COUNT


PRINT*, 'ENTER MAT FOLLOWED BY M'
READ*, (MAT(I,J),J = 1,8), I = 1,5), M
COUNT = 0
DO 10 I = 1,5
DO 10 J = 1,8
IF(MOD(MAT(I,J),M).EQ.0) THEN
COUNT = COUNT + 1
LOC(COUNT,1) = I
LOC(COUNT,2) = J
ENDIF
10 CONTINUE
PRINT*,'NO# OF ELEMENTS DIVIDABLE BY M IS:', COUNT
PRINT*,'LOCATIONS IN ARRAY MAT ARE'
DO 20 I = 1,COUNT
PRINT*,LOC(I,1), LOC(I,2)
20 CONTINUE
END

You might also like