Prelim3From2003 - CS100J Prelim 3 18 November 2003 Uris Auditorium 7:30PM-9:00PM Name(PRINT LEGIBLY(last first NetID This exam has questions 0 1 2

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS100J Prelim 3 18 November 2003 Uris Auditorium 7:30PM--9: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 while-loop (not a for-loop) 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 first 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 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 while-loop (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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/01/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell University (Engineering School).

Page1 / 6

Prelim3From2003 - CS100J Prelim 3 18 November 2003 Uris Auditorium 7:30PM-9:00PM Name(PRINT LEGIBLY(last first NetID This exam has questions 0 1 2

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online