Prelim3_Fall2006

# Prelim3_Fall2006 - Cornell net id Name CS 100J Prelim 3 14...

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

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 in this part of b

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

View Full Document
Cornell net id ____________________ Name ________________________________ 2 Question 2 (22 points). will soon be the time to buy textbooks for next semester. Gries is considering
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern