CS 1313 Midterm Exam A Solutions
CS 1313 Midterm Exam A Solutions
1. Name 2 of each of the following kinds of hardware devices. (2 points for each)
a. input:
Some possible answers:
keyboard, mouse, joystick, scanner
b. output:
Some possible answers:
monitor, printer, speakers
c. secondary storage:
Some possible answers:
hard disk, floppy disk, CDROM, CD-RW, DVD, DVD-RAM, Zip disk, Jaz disk, Superdisk,
magnetic tape, paper tape, punch card
2. Describe the difference(s) between the following. Your answers should be in the form ``Roses are red, but
violets are blue.'' (4 points for each)
Answers:
a. The difference(s) between a compiler and an intepreter:
Answer: a compiler converts an entire program from a high level language into a machine language
executable all at once, before running, but an interpreter reads and performs one high level
command at a time.
b. The difference(s) between a compiler and an assembler:
Answer: a compiler converts a source code in a high level language into a machine language
executable, but an assembler converts a source code in a low-level, CPU-specific assembly
language into a machine language executable.
4. When is it acceptable to visit a CS1313 instructor or TA in their office? (2 points for each)
Answers:
a. During their scheduled office hours.
b. With an appointment made 24 hours in advance.
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 1/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
a. PROGRAM meloveprog
IMPLICIT NONE
PRINT *, "I love programming!"
END PROGRAM meloveprog
b. PROGRAM mevarassn
IMPLICIT NONE
INTEGER :: woop
woop = 10
PRINT *, 'woop = ', woop
END PROGRAM mevarassn
Answer: woop = 10
% cat mevarassn.f90
PROGRAM mevarassn
IMPLICIT NONE
INTEGER :: woop
woop = 10
PRINT *, 'woop = ', woop
END PROGRAM mevarassn
% f90 -o mevarassn mevarassn.f90
% mevarassn
woop = 10
c. PROGRAM mevarchg
IMPLICIT NONE
REAL :: wawa = 12.0
wawa = 16
PRINT *, 'wawa = ', wawa
END PROGRAM mevarchg
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 2/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
% mevarchg
wawa = 16.0000
d. PROGRAM meimpl
INTEGER :: boohoo = 5
booho = boohoo + 1
PRINT *, "boohoo = ", boohoo
END PROGRAM meimpl
Answer: boohoo = 5
% cat meimpl.f90
PROGRAM meimpl
INTEGER :: boohoo = 5
booho = boohoo + 1
PRINT *, "boohoo = ", boohoo
END PROGRAM meimpl
% f90 -o meimpl meimpl.f90
% meimpl
boohoo = 5
Notice that this program is missing the IMPLICIT NONE statement, and that in the assignment
statement the variable name is misspelled on the left hand side of the equal sign, so booho is
implicitly declared as a REAL and is assigned boohoo + 1 (which is 6), and therefore boohoo is
unchanged at 5.
a. PROGRAM meexpr1
IMPLICIT NONE
INTEGER :: a = 5, b = 10, c = 15
REAL :: z
z = a + b + c
PRINT *, z
END PROGRAM meexpr1
Answer: 30.0000
Breakdown:
z=a+b +c
z = 5 + 10 + 15
z = 15 + 15
z= 30
z = 30.0000
Notice that z is REAL, but a, b and c are INTEGER. So, the additions are performed as INTEGER
operations, but the final result is converted to REAL before being assigned to the REAL variable z.
% cat meexpr1.f90
PROGRAM meexpr1
IMPLICIT NONE
INTEGER :: a = 5, b = 10, c = 15
REAL :: z
z = a + b + c
PRINT *, z
END PROGRAM meexpr1
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 3/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
b. PROGRAM meexpr3
IMPLICIT NONE
INTEGER :: a = 2, b = 3, c = 4
REAL :: z
z = a + b / c
PRINT *, z
END PROGRAM meexpr3
Answer: 2.00000
% cat meexpr3.f90
PROGRAM meexpr3
IMPLICIT NONE
INTEGER :: a = 2, b = 3, c = 4
REAL :: z
z = a + b / c
PRINT *, z
END PROGRAM meexpr3
% f90 -o meexpr3 meexpr3.f90
% meexpr3
2.00000
Notice that the division is of two INTEGER variables, and INTEGER division truncates, and the
precedence order puts division before addition.
c. PROGRAM meexpr5
IMPLICIT NONE
LOGICAL :: p = .TRUE., q = .FALSE., r
r = (p .AND. q) .OR. (p .EQV. q)
PRINT *, r
END PROGRAM meexpr5
Answer: F
% cat meexpr5.f90
PROGRAM meexpr5
IMPLICIT NONE
LOGICAL :: p = .TRUE., q = .FALSE., r
r = (p .AND. q) .OR. (p .EQV. q)
PRINT *, r
END PROGRAM meexpr5
% f90 -o meexpr5 meexpr5.f90
% meexpr5
F
3. Advanced
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 4/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
a. 2, 3
Answer:
Lookie!
The first is less!
Get lost!
b. 22, 22
Answer:
Lookie!
Huh?
Get lost!
c. 5, 4
Answer:
Lookie!
The first is greater!
Get lost!
% cat meif.f90
PROGRAM meif
IMPLICIT NONE
INTEGER :: input1, input2
READ *, input1, input2
PRINT *, "Lookie!"
IF (input1 < input2) THEN
PRINT *, "The first is less!"
ELSE IF (input1 > input2) THEN
PRINT *, "The first is greater!"
ELSE
PRINT *, "Huh?"
END IF
PRINT *, "Get lost!"
END PROGRAM meif
% f90 -o meif meif.f90
% meif
2, 3
Lookie!
The first is less!
Get lost!
% meif
22, 22
Lookie!
Huh?
Get lost!
% meif
5, 4
Lookie!
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 5/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
b. (16 points)
PROGRAM medonest
IMPLICIT NONE
INTEGER :: i, j, aggregate = 0
DO j = 1, 2
DO i = 1, 3
aggregate = aggregate + (j * i)
END DO
END DO
PRINT *, aggregate
END PROGRAM medonest
Answer: 18
% cat medonest.f90
PROGRAM medonest
IMPLICIT NONE
INTEGER :: i, j, aggregate = 0
DO j = 1, 2
DO i = 1, 3
aggregate = aggregate + (j * i)
END DO
END DO
PRINT *, aggregate
END PROGRAM medonest
% f90 -o medonest medonest.f90
% medonest
18
c. (10 points)
PROGRAM mearraya
IMPLICIT NONE
INTEGER,DIMENSION(1:6) :: a = (/1, 1, 2, 3, 5, 8/), b
INTEGER :: i
DO i = 1, 6
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 1, 6)
END PROGRAM mearraya
Answer: 2 2 4 6 10 16
% cat mearraya.f90
PROGRAM mearraya
IMPLICIT NONE
INTEGER,DIMENSION(1:6) :: a = (/1, 1, 2, 3, 5, 8/), b
INTEGER :: i
DO i = 1, 6
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 1, 6)
END PROGRAM mearraya
% f90 -o mearraya mearraya.f90
% mearraya
2 2 4 6 10 16
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 6/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
Fill in the (lined) missing parts of the program in order to produce the output shown. Write each answer on the
appropriate line. Write only on the blank lines and nowhere else.
1. (9 points)
% cat mefbdoa.f90
PROGRAM mefbdoa
IMPLICIT NONE
INTEGER :: i
DO i = 1, 5
IF ( ___________________________ ) THEN
Answer:
% cat mefbdoa.f90
PROGRAM mefbdoa
IMPLICIT NONE
INTEGER :: i
DO i = 1, 5
IF (i == 1) THEN
PRINT *, "1st gear!"
ELSE IF (i == 2) THEN
PRINT *, "2nd gear!"
ELSE IF (i == 3) THEN
PRINT *, "3rd gear!"
ELSE
PRINT *, "Faster!"
END IF
END DO
END PROGRAM mefbdoa
% f90 -o mefbdoa mefbdoa.f90
% mefbdoa
1st gear!
2nd gear!
3rd gear!
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 7/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
Faster!
Faster!
2. (5 points)
% cat mefbarraya.f90
PROGRAM mefbarraya
IMPLICIT NONE
INTEGER :: i
INTEGER,DIMENSION(0:4) :: a = ______________________________________
INTEGER,DIMENSION(0:4) :: b
DO i = 0, 4
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 0, 4)
END PROGRAM mefbarraya
% f90 -o mefbarraya mefbarraya.f90
% mefbarraya
2 4 8 16 32
% cat mefbarraya.f90
PROGRAM mefbarraya
IMPLICIT NONE
INTEGER :: i
INTEGER,DIMENSION(0:4) :: a = (/ (2 ** i, i = 0, 4) /)
INTEGER,DIMENSION(0:4) :: b
DO i = 0, 4
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 0, 4)
END PROGRAM mefbarraya
% f90 -o mefbarraya mefbarraya.f90
% mefbarraya
2 4 8 16 32
3. (16 points)
% cat mefbtempa.f90
PROGRAM mefbtempa
IMPLICIT NONE
READ *, ____________________________________________________________
DO year = lb, ub
hi_temp_deg_C(year) = &
& (hi_temp_deg_F(year) - 32.0) * (5.0 / 9.0)
END DO
PRINT *, "Norman's high temperatures in degrees Celsius"
PRINT *, " for the years ", lb, " through ", ub, " were:"
PRINT *, ____________________________________________________________
Answer:
% cat mefbtempa.f90
PROGRAM mefbtempa
IMPLICIT NONE
INTEGER,PARAMETER :: lb = 1995, ub = 1999
REAL,DIMENSION(lb:ub) :: hi_temp_deg_F, hi_temp_deg_C
INTEGER :: year
PRINT *, "What were Norman's high temperatures in degrees Fahrenheit"
PRINT *, " for the years ", lb, " through ", ub, "?"
READ *, (hi_temp_deg_F(year), year = lb, ub)
DO year = lb, ub
hi_temp_deg_C(year) = &
& (hi_temp_deg_F(year) - 32.0) * (5.0 / 9.0)
END DO
PRINT *, "Norman's high temperatures in degrees Celsius"
PRINT *, " for the years ", lb, " through ", ub, " were:"
PRINT *, (hi_temp_deg_C(year), year = lb, ub)
END PROGRAM mefbtempa
% f90 -o mefbtempa mefbtempa.f90
% mefbtempa
What were Norman's high temperatures in degrees Fahrenheit
for the years 1995 through 1999 ?
105, 106, 107, 108, 109
Norman's high temperatures in degrees Celsius
for the years 1995 through 1999 were:
40.5556 41.1111 41.6667 42.2223 42.7778
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 9/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
hi_temp_deg_C(year) = &
& (hi_temp_deg_F(year) - 32.0) * (5.0 / 9.0)
END DO
PRINT *, "Norman's high temperatures in degrees Celsius"
PRINT *, " for the years ", lb, " through ", ub, " were:"
PRINT *, hi_temp_deg_C
END PROGRAM mefbtempa2
% f90 -o mefbtempa2 mefbtempa2.f90
% mefbtempa2
What were Norman's high temperatures in degrees Fahrenheit
for the years 1995 through 1999 ?
105, 106, 107, 108, 109
Norman's high temperatures in degrees Celsius
for the years 1995 through 1999 were:
40.55556 41.11111 41.66667 42.22223 42.77778
In mathemetics, a one-dimensional array (the kind we've been using in class) is often called a vector. The dot
product of two vectors of the same length is the sum of the pairwise products of their elements. That is, for two
vectors of length n
and
X . Y = x1 . y1 + x2 . y2 + ... + xn . yn
For this exam problem, you are not required to have comments, idiotproofing or introductory output (though you
should prompt the user for input), and you may use numeric literal constants in the body of your program. You
may assume that the maximum possible vector length will be 100, and that the vector elements can be non-
integers.
You'll get substantial partial credit for setting up the problem correctly (e.g., declarations, input, output), so if
you can't write the full program, do as much as you can.
You may attach an extra sheet of notebook paper to this exam or write on the back of this page if you want. (35
points)
Answer:
% cat dotproduct.f90
PROGRAM dotproduct
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 10/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
IMPLICIT NONE
REAL,DIMENSION(1:100) :: vector1, vector2
REAL :: dotprod = 0
INTEGER :: vector_length, i
PRINT *, "What is the length of the vectors?"
READ *, vector_length
PRINT *, "What are the ", vector_length, " elements of the first vector?"
READ *, (vector1(i), i = 1, vector_length)
PRINT *, "What are the ", vector_length, " elements of the second vector?"
READ *, (vector2(i), i = 1, vector_length)
DO i = 1, vector_length
dotprod = dotprod + vector1(i) * vector2(i)
END DO
PRINT *, "The dot product of the vectors is ", dotprod, "."
END PROGRAM dotproduct
% f90 -o dotproduct dotproduct.f90
% dotproduct
What is the length of the vectors?
5
What are the 5 elements of the first vector?
1 2 3 4 5
What are the 5 elements of the second vector?
2 4 6 8 10
The dot product of the vectors is 110.0000 .
% dotproduct
What is the length of the vectors?
3
What are the 3 elements of the first vector?
1 5 9
What are the 3 elements of the second vector?
2 6 10
The dot product of the vectors is 122.0000 .
% dotproduct
What is the length of the vectors?
7
What are the 7 elements of the first vector?
1 2 3 4 5 6 7
What are the 7 elements of the second vector?
1 2 3 4 5 6 7
The dot product of the vectors is 140.0000 .
Bonus Program
Write the program described below.
The nth Fibonacci number, for any positive integer n, is given as:
/ 1, if n = 1 or n = 2
f(n) = |
\ f(n - 1) + f(n - 2), otherwise
1, 1, 2, 3, 5, 8, 13
Write a program that takes as its input n (the Fibonacci number that the user wants) and outputs the nth
Fibonacci number.
You may use an array if you want, but you're not required to.
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 11/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
For this exam problem, you are not required to have comments, idiotproofing or introductory output (though you
should prompt the user for input), and you may use numeric literal constants in the body of your program.
You'll get substantial partial credit for setting up the problem correctly (e.g., declarations, input, output), so if
you can't write the full program, do as much as you can.
You may attach an extra sheet of notebook paper to this exam or write on the back of this page if you want. (25
points)
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 12/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 13/13