1). What is printed by the following code segment if all variables are of type INTEGER?
k = 0
m = 0
p = 0
DO
IF (p .GE. 10) EXIT
k = k + 1
m = m + p
print *, p, k, m
p = p + k
END DO
2). For the read statement:
CHARACTER*4
:: Alpha
REAL :: X
INTEGER :: I
read 29, Alpha, X, I
What data values are stored for the following FORMAT statements ? For each part, assume the user types in the following data line, beginning in column 1.
Test2413/1995
a). 29 FORMAT (A5, 1x, F2.1, 1x, I3)
b). 29 FORMAT (A3, 1x, F4.2, 3x, I1)
Questions 3-6 are multiple choice. Circle the letter corresponding to the choice that best answers the question.
3). In a subroutine that gets a value from the keyboard and communicates that value to the main program via an argument. To the subroutine, that argument used is considered:
a). an input argument
b). an output argument
c). an input/output argument
d). a local variable
e). an actual argument
4). In a subroutine that receives a value from the main program via an argument and then displays the argument's value on the screen. To the subroutine that argument is considered:
a). an input argument
b). an output argument
c). an input/output argument
d). a local variable
e). none of the above
5). What does the following function do?
INTEGER FUNCTION Fun (string)
! CHARACTER (*) :: list_of_identifiers
in a function or subroutine declaration indicates
!
that the formal argument will have the same length as the actual
argument
CHARACTER (*), INTENT(IN) :: string
! # denotes a blank character
CHARACTER(1),
PARAMETER :: blank = '#'
INTEGER :: k
k = LEN(string)
DO
IF (k .LE. 0) EXIT
IF (string(k:k) .EQ. blank) THEN
k = k-1
ELSE
k = -k
END IF
END DO
Fun = IABS(k)
END FUNCTION
Fun
a). It finds the location of the first non-blank
character in string.
b). It finds the location of the last non-blank
character in string.
c). It counts the non-blank characters in
string.
d). It finds the location of the first blank
in string.
e). It finds the location of the last blank
in string.
f). It counts the blanks in string.
6). What will be printed by the program given below (where # denotes a blank)?
PROGRAM six
CHARACTER *13
:: string, text
CHARACTER *2
:: target
INTEGER :: n
string = 'CONCATENATION'
target = 'AT'
n = INDEX(string,
target)
DO
IF (n .EQ. 0) EXIT
text = string( :n-1) // string(n+1: )
string = text
n = INDEX(string, target)
END DO
print *, string(10:11)
END PROGRAM six
a). ##
b). IO
c). N#
d). ON
e). TI
7). What will be printed by the following program? (Hint: Remember how formal arguments are associated with actual arguments.)
PROGRAM Mixup
INTEGER :: J,
L, M
M = 4
L = 0
J = 5
CALL Sub (M,
L, J)
print *, M,
L
CALL Sub (L,
J, M)
print *, L,
J, M
CONTAINS
SUBROUTINE Sub
(L, M, J)
INTEGER INTENT(INOUT)::
J, L, M
IF (L .EQ. 3)
THEN
M = 6
J = 4
ELSE
M = J + 3
L = L + 1
END IF
RETURN
END SUBROUTINE
Sub
END PROGRAM Mixup
8). Write a Fortran function of type LOGICAL called IsADigit defined by the following PRE and POST-conditions:
PRE: ch is of type CHARACTER *1 and is
assigned
POST: IsADigit returns .TRUE. if ch is one
of the digit
characters
'0' through '9'; otherwise, it returns
.FALSE.
9). Write a Fortran subroutine called Rectangle defined by the following PRE- and POST-conditions:
PRE: length and width are assigned, positive,
REAL values;
printit
is an assigned LOGICAL value.
POST: area is the area of the rectangle with
sides length and
width;
perimeter is the perimeter of the rectangle with
sides
length and width; if printit is .TRUE., the values
for area
and perimeter are displayed on the terminal.
10). For each of the following, write appropriate DECLARATIONS and STATEMENTS to create the specified one-dimensional array:
a). an array whose subscripts are the
integers from 1 to 100 and in which an array element has the value .true.
if the corresponding subscript is even, and .false. otherwise
b). an array whose subscripts are the
integers from -50 to +50 and in which an array element has the same
value as its subscript
11). Assume that the following declarations have been made
INTEGER :: Matrix(3,3), i, j
and that the following statements have been executed to initialize the array:
DO i =
1, 3
DO j = 1 , 3
Matrix(i,j) = (2 * i) + j
END DO
END DO
a). Fill in the values of Matrix in the following table. Note that the row and column numbers have been labeled.
+++++++++++++++++++++++++++++++
row 3 | | | |
| | | |
+++++++++++++++++++++++++++++++
| | | |
row 2 | | | |
+++++++++++++++++++++++++++++++
| | | |
row 1 | | | |
+++++++++++++++++++++++++++++++
col 1 col 2 col 3Show what will be printed out after executing each of the print statements given below.
b). print 5, Matrix
5 FORMAT (9I3)
c). print 5, ((Matrix(i,j), j = 1,3), i = 1,3)
d). print 5, ((Matrix(j,i), i = 1,3), j = 1,3)
e). print 5, ((Matrix(i,j), i = 1,3), j = 1,3)
12). In class we developed a subroutine ReadList that would allow the user of the telephone list program to read in the contents of the telephone list from a disk file. Now write the subroutine that goes the other way: the subroutine WriteList should ask the user for the name of a disk file to be created, and then should open that file. Once the file is open, the contents of the Names and Numbers arrays should be written to the file (only write the names and numbers that are currently active.) Here are the pre- and post-conditions:
PRE: Names, Numbers, and Active are parallel arrays with a
maximum
size of NumRecords. Names contains entries of
type CHARACTER
*20. Numbers contains entries of type
CHARACTER
*13. Active contains entries of type LOGICAL.
POST: Every Names(i) and Numbers(i) whose Active(i) = .TRUE.
has been
written to a disk file (one Name and Number on
each line
of the file).
13) Write a complete Fortran function that satisfies the following PRE- and POST-conditions.
PRE: A and B are both two-dimensional arrays of type INTEGER with m rows and n columns.
POST: The function returns .TRUE. if A(i,j)
= B(i,j) for every
1<=i<=m,
1<=j<=n. The function returns .FALSE. otherwise.
2 a Alpha = "est2"
X = 1.3
I = 199
b Alpha = "Tes#"
X = 24.13 I = 9
# denotes a blank space
3 b
4 a
5 b
6 d
7
5 8
9 8 5
8
LOGICAL FUNCTION IsADigit
(ch)
CHARACTER *1, INTENT(IN)
:: ch
IF (ch .GE. '0' .and. ch
.LE. '9') THEN
IsADigit
= .TRUE.
ELSE
IsADigit
= .FALSE.
END IF
END FUNCTION IsADigit
9
SUBROUTINE Rectangle (length,
width, printit)
REAL, INTENT(IN) :: length,
width
REAL :: area, perimeter
LOGICAL, INTENT(IN)
:: printit
area = length * width
perimeter = 2 * length +
2 * width
IF (printit) THEN
print
*, 'area = ', area, 'perimeter = ', perimeter
END IF
END SUBROUTINE Rectangle
10a
LOGICAL :: A(100)
INTEGER :: i
DO i = 1, 100
IF (MOD(i,2) .EQ. 0) THEN
A(i) = .TRUE.
ELSE
A(i) = .FALSE.
END IF
END DO
10b
INTEGER :: B(-50:50), i
DO i = -50, 50
B(i) = i
END DO
11 a.
+++++++++++++++++++++++++++++++
row 3 | | | |
| 7 | 8 | 9 |
+++++++++++++++++++++++++++++++
| | | |
row 2 | 5 | 6 | 7 |
+++++++++++++++++++++++++++++++
| | | |
row 1 | 3 | 4 | 5 |
+++++++++++++++++++++++++++++++
col 1 col 2 col 311b ANS: 3 5 7 4 6 8 5 7 9
12.
SUBROUTINE WriteList (Names,
Numbers, Active, NumRecords)
INTEGER, INTENT(IN) :: NumRecords
CHARACTER *20, INTENT(IN)
:: Names(NumRecords)
CHARACTER *13, INTENT(IN)
:: Numbers(NumRecords)
CHARACTER *20 :: Filename
INTEGER :: i
LOGICAL, INTENT(IN) :: Active(NumRecords)
PRINT *, 'Enter filename'
READ *, Filename
OPEN (UNIT=1, ACCESS = 'sequential', FILE = Filename, FORM = 'Formatted', STATUS = 'new')
DO i = 1, NumRecords
IF (Active(i))
THEN
WRITE (1,2) Names(i), Numbers(i)
2
FORMAT (A20, A13)
END IF
END DO
ENFILE(1)
CLOSE (1)
END SUBROUTINE WriteList
13
LOGICAL FUNCTION Equal (A,
B, m, n)
INTEGER, INTENT(IN) :: A(m,n),
B(m,n), m, n
INTEGER :: i, j
LOGICAL :: Answer
Answer = .TRUE.
DO i = 1, m
DO
j = 1, n
IF (A(i,j) .NE. B(i,j)) THEN
Answer = .FALSE.
END IF
END DO
END DO
Equal = Answer
END FUNCTION
Equal