1
CS100J
Prelim 3
18 November 2003
Uris Auditorium
7:30PM9:00PM
Name (PRINT LEGIBLY!)_____________________________ NetID________
(last, first)
This exam has questions 0, 1, 2, 3, 4, 5 worth a total of 100 points.
Question 0. 2 points.
Write your name and netid, legibly, at the top of every page.
Question 1. 18 points.
Assume
int
array m is sorted (in ascending order). Write a whileloop (not a forloop)
with initialization to perform a binary search for a value x in m. The postcondition of the loop should be
ONE of the following. (You choose which one. The ﬁrst was used in class; the second, which uses virtual
values m[1] and m[m.length], is used in the text.)
R0: m[0.
.k] <= x < m[k+1.
.m.length–1]
R1: m[k] <= x < m[k+1] (assuming m[– 1] = –
∞
, m[m.length] =
∞
)
You must state the invariant, because you are supposed to develop the loop using it. The invariant and the
Java code should be essentially what we did in class or as done in the text. We do not want a complete
method, but just the sequence of statements to truthify the postcondition.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Name (PRINT LEGIBLY!)_____________________________ NetID________
Question 2. 20 points.
Consider two sorted (in ascending order)
int
arrays b and c. We want to merge
these arrays into a single sorted array d. For example, given
b = {1, 3, 3, 8} and c = {2, 3, 4, 4, 7}, d is {1, 2, 3, 3, 3, 4, 4, 7, 8}
Assume these variables are already declared and that the length of d is the sum of the lengths of b and c. Note
that an array can have length 0.
Write a single whileloop (with initialization) to merge b and c into d. The precondition is that arrays b and
c are sorted. The postcondition R is:
The invariant that you are to use consists of the following three pieces:
Write
only one
loop. Don’t change the invariant, and don’t add extra variables; use only b, c, d, h, and k.
Arrays b and c should not be changed.Your answer depends entirely on how well you deal with the four
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '07
 FAN/VANLOAN
 Celsius, 0 degrees, 100 degrees, 32 degrees Fahrenheit, Uris Auditorium

Click to edit the document details