Prelim3_Fall2006

Prelim3_Fall2006 - Cornell net id ____________________ Name

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Cornell net id ____________________ Name ________________________________ 1 CS 100J Prelim 3 14 November 2006 This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few minutes looking at all questions before beginning so that you can see what is expected. Budget your time wisely. Use the back of the pages, if you need more space. We have a stapler at the front of the room, so you can tear the pages apart. You need not write loop invariants unless they are explicitly required in the question. Question 0 (2 points). Write your netid and your name, legibly, at the top of each page (Hint: do it now). Question 1 (20 points). Array b is in ascending order. Each value may occur many times, and, since b is sorted, the equal values are together. Here is an example: {3, 3, 5, 5, 5, 5, 7, 9, 9, 9}. In this example, the length of the longest segment of equal values is 4, since 5 occurs 4 times and the other values occur fewer times. Write a single loop (either a while-loop or a for-loop) that stores in x the length of the longest segment of equal values in array b. The post- condition is given below, as is the invariant. No credit will be given for a loop that does not use this invariant at all , so remember the four loopy questions and use them in developing the loop. We will attempt to give as much partial credit as possible. You may assume that b has at least one element, although it is not necessary. In the repetend, you have to decide when to increase x by 1. In thinking about this, ask yourself when extending the processed segment by 1 (to the left) gives a segment of x+1 equal values. 0 ___________ out of 02 1 ___________ out of 20 2 ___________ out of 22 3 ___________ out of 15 4 ___________ out of 20 5 ___________ out of 21 Total ________ out of 100 // Postcondition: b 0 b.length x = length of longest segment of equal values in this part of b // invariant: b 0 k b.length ? x = length of longest segment of equal values ?...
View Full 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.

Page1 / 6

Prelim3_Fall2006 - Cornell net id ____________________ Name

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

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