Exam 3

advertisement
CPSC 203 – 500 FINAL EXAM
Spring 2005
NO CALCULATOR IS ALLOWED!!
UIN:
Full Name:
Total
answer.
Score
Possible
Part One
30 pts
Part Two
20 pts
Part Three
15 pts
Part Four
15 pts
Part Five
20 pts
Points
100 ptsPART ONE (10*3 points). Some questions have MORE THAN ONE
Circle ALL the correct answers!
1. Which of the following are valid variable names for Fortran 90?
a. INCOME
b. TWO FOLD
c. TAX-RT
A, D
d. PI
e. 2X
1
2. Given the values of A = 28.0, B = 53.0, C = -18.0, D = 64.6, E = 37.5, and K = 45,
Which of the following equations are true?
A
a. SQRT(ABS(A-B)) == 5.0
c. NINT(D) == 64
b. ABS(E-D*2.0) == 54.2
d. MIN (A,B,C,D,E) == 64.6
3. Given the values of the following variables, which of the followings are FALSE?
A
I = 7, J = 10, K = 3, L = 18, M = 9, N =6
a. I .LT. K .AND. L .EQ. M + N
b. .NOT. (J .GT. L – N)
c. L – 5 .GT. N + M .OR. I .NE. J
d. I + J .GT. K * 5 .AND. L * 2 .GT. M
e. .NOT. (I*J .LT. K) .AND. (L *M .GT. N * K)
4. What is the length of the array specified by the following declaration: B
INTEGER, PARAMETER :: a = -10
INTEGER, PARAMETER :: b = 10
LOGICAL, DIMENSION (a:b-a*2) :: a
a. 40
b. 41
c. 50
5. With the following declaration, which is true?
d. 51
e. none of the above
A
REAL, ALLOCATABLE, DIMENSION(:)::A
A. The rank of the array A is 2.
1.It is known as a deferred shape array.
2.It is called dynamic memory allocation.
3.The size and rank of the array is specified during execution.
a. B, C
b. A, B, C
c. B, C, D
d. All of the above
e. None of the above
6. What is output by the following code? (a)
INTEGER:: i, j, output
Do i = 1, 3
Do j = 1, 3
If ( i == j) CYCLE
output = j ** i
write(*,*) output
2
End Do
End Do
a. 2 3 1 9 1 8
b. 1 1 2 8 3 9
c. 1 4 27
d. 1 2 3 1 4 9 1 8 27
7. Which of the following expression are TRUE (or legal) in Fortran? Assume the ASCII collating
sequence. (b)
(1) 'ABC' > 'abc'
(2) ACHAR(65) // ACHAR(67) // ACHAR (69) == 'ACE'
(3) ACHAR( IACHAR('j') +5) == 'o'
(4) '12345' == 12345
a. (1), (2) and (3)
b. (2) and (3)
c. (3) and (4)
d. All of above
8. What is output by the following code? (c)
PROGRAM test
IMPLICIT NONE
REAL, DIMENSION(3) :: data =(/5,10,15/)
REAL :: fun1
INTEGER :: i=size(data)
write (*,*) fun1(data,i)
END PROGRAM test
REAL FUNCTION fun1(x,a)
IMPLICIT NONE
INTEGER, INTENT(IN):: a
REAL, INTENT(IN), DIMENSION(a) :: x
INTEGER:: i
Do i=1, a
fun1 = fun1 + x(i)
End Do
END FUNCTION fun1
a. 10
b. 20
c. 30
d. 40
9. Given CONST= 3856.7569, VALUE= -187.325, BALNC= 62543.14 and the program segment
below, what is output ? (b represent blank) (c)
WRITE(*,1000) CONST, VALUE, BALNC
1000 FORMAT (3X, F7.2, 5X, F6.1, 5X, F6.0)
3
a. bbb3856.756bbbbb-187.32bbbbb62543.1
b. 3856.76bbbbb-187.3bbbbb62543.
10. An input data file input.dat contains the following values:
10 20 30 40
11 13 15 17
2 4 6 8
1 5 10 15
what is output by following code? (assume that file is opened on i/o unit 1 and arr is defined as
4x4 integer array) (a)
Do i=1,3
Read (1,*) ( arr (i,j), j=1,3 )
End do
write (*,*) ( ( arr(i,j), i=1,3 ), j=1,3 )
a. 10
b. 10
c. 10
d. 10
11
20
20
20
2 20 13 4 30 15 6
30 11 13 15 2 4 6
30 40 11 12 15 17 2 4 6 8
30 40 11 13 15 17 2
PART TWO (4*5 points). Short Answers.
11. ____________________ are common functions that are built directly into the Fortran language.
Intrinsic functions
12. Given real :: array(10,20,30,40)
What is the linear storage element for array(5,6,11,4)?
3x10x20x30 + 10x10x20 +5x10 +5 = 20055
13. When do we need to use ALLOCATA/DEALLOCATE for array ?
When real/virtual memory is limited and we need big arrays at different times during execution
of a program.
4
14. Write benefits of using external procedures such as subroutines or function subprograms.
Independent testing of a subtask
Reusable code
Isolation from unintended side effects PART THREE (15 points). Complete program.
15. The value for  can be determined by the series equation:
 = 4 * (1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + 1/13 ... )
Write a program to calculate the value of  using the formula above until the term 1/99 is reached.
PROGRAM test
IMPLICIT NONE
REAL:: pi, sum=1
INTEGER:: i
DO i = 1, 49
sum = sum + (-1)**i * 1.0/((i*2)+1)
END DO
write (*,*) 'pi=', 4* sum
END PROGRAM test
5
PART FOUR (15 points). Complete program.
16. Write a complete binary search program that does 1) to 5). Also answer to the question 6).
1) Reads integer numbers from input.dat file which contains 11 integers (see below).
2) Stores the numbers into array ARR(11).
3) Receives any number, NUM from user input (use READ statement).
4) Searches array ARR whether the NUM is contained at the array or not (use binary search) .
5) If the input number NUM is in the list ARR then write the index of array and steps, if not write
“not exist”.
6) Let's assume the searching number is 7, then what will be the index and how many steps are
needed to find the number ?
input.dat:
1 3 5 7 8 9 11 13 15 17 19
PROGRAM bsearch
IMPLICIT NONE
INTEGER :: num, i=1, j=11, k, steps = 0
INTEGER, DIMENSION(11) :: ARR
OPEN (UNIT=99, FILE='input.dat', STATUS='OLD', ACTION='READ')
READ(99,*) (arr(i), i=1,11)
WRITE(*,*) 'Input a number to search: '
READ(*,*) num
DO
steps = steps+1
k = (i+j)/2
IF(num == arr(k))then
WRITE(*,*) 'Index = ', k
Exit
end if
IF (num < arr(k)) THEN
j=k
ELSE
i=k
END IF
IF(i+1 == j) then
if(arr(j) == num) then
WRITE(*,*) 'Index = ', j
else if(arr(i) == num) then
WRITE(*,*) 'Index = ', i
else
WRITE(*,*) 'The number', num, 'is not in the array.'
end if
Exit
end if
6
END DO
WRITE(*,*) 'Total comparisons: ', steps
CLOSE(99)
END PROGRAM bsearch
(6) ANSWER : index = 4, steps = 3PART FIVE (20 points). Complete program.
Write a complete program that performs the following:
10. OPEN 3 files, DATA1.DAT, DATA2.DAT, and OUTPUT.DAT, using unit numbers 100, 200, and 300,
respectively. You must specify the STATUS and ACTION for OPEN statement.
11. The input data files, DATA1.DAT and DATA2.DAT, consist of a student name per record in columns 1130. Each file is in alphabetical order by name. The number of records for each file is unknown, but is less
than 1000 records.
12. Read the input data files and merge to a new file, OUTPUT.DAT, in alphabetical order. At the end of the
output file print the number of records processed.
13. If the output file already exists, the program should overwrite the file.
14. CLOSE all files before ending the program.
PROGRAM part5
IMPLICIT NONE
INTEGER:: num=1, err1=0, err2=0
CHARACTER(len=20):: name1, name2
OPEN (UNIT=100, FILE='DATA1.DAT', STATUS='OLD', ACTION='READ')
OPEN (UNIT=200, FILE='DATA2.DAT', STATUS='OLD', ACTION='READ')
OPEN (UNIT=300, FILE='OUTPUT.DAT', STATUS='REPLACE', ACTION='WRITE')
READ (100,10, IOSTAT=err1) name1
READ (200,10, IOSTAT=err2) name2
10 FORMAT (11T, A20)
DO WHILE (err1==0 .OR. err2==0)
IF (err1==0 .AND. (name1<name2 .OR. err2/=0)) THEN
WRITE (300, *) num, name1
READ (100,10, IOSTAT=err1) name1
ELSE IF (err2==0) THEN
WRITE (300, *) num, name2
READ (200,10, IOSTAT=err2) name2
END IF
num = num+1
END DO
WRITE (300, *) ‘The number of records processed: ’, num
CLOSE (UNIT=100)
CLOSE (UNIT=200)
CLOSE (UNIT=300)
END PROGRAM part5
7
Download