Computer Science 61B - Spring 1996 - Clancy - Midterm 3

# Computer Science 61B - Spring 1996 - Clancy - Midterm 3 -...

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

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

View Full Document
CS 61B, Midterm #3 and solutions, Spring 1996 Anywhere you are directed to write a function, your solution may include auxiliary functions. You need not rewrite a function that appears in Deitel and Deitel, in Carrano, or in any of the CS 61B handouts; merely cite the page in the textbook or the handout in which the function appears. A few students are taking this exam next week. Do not discuss this exam with them, or post any news about the exam until next Wednesday. Relax--this exam is not worth having heart failure about. Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem 1 (3 points, 8 minutes) A debugged version of the code for heap verification and insertion used in lab assignment 11 appears below. Buestions in parts a and b refer to this code. heaps.h class Heap ( public: Heap (ifstream&); bool IsHeap (); void Insert (int); private: int size; Vector<int> values; }; heaps.cc Heap::Heap (ifstream& valuesIn): size(0), values(some positive integer) { int value; while (valuesIn >> value) { values[size] = value; size++; }; file:///C|/Documents%20and%20Settings/Jason%20Raft...20Spring%201996%20-%20Clancy%20-%20Midterm%203.htm (2 of 16)1/27/2007 6:33:03 PM
CS 61B, Midterm #3 and solutions, Spring 1996 assert (IsHeap ()); } bool Heap::IsHeap () { for (int k=O; k<size/2; k++) { if (values[k] < values[2*k+ll) { return false; } if (k < (size-1)/2 && values[k] < values[2*k+21] { return false; } } return true; } void Heap::Insert (int newValue) { values[size] = newValue; size++; for (int k=size-l; k>0; k = (k-1)/2) { if (values[k] < values[(k-1)/2]) { break; } int temp = values[k]; values[k] = values[(k-1)/2]; values[(k-1)/2] = temp; } } Part a Suppose that a heap is declared with the statements ifstream fileIn ("data"); Heap h (fileIn); and the data file contains the values 6 3 5 1 2 4 file:///C|/Documents%20and%20Settings/Jason%20Raft...20Spring%201996%20-%20Clancy%20-%20Midterm%203.htm (3 of 16)1/27/2007 6:33:03 PM

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

View Full Document
CS 61B, Midterm #3 and solutions, Spring 1996
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