{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs31f08final - UCLA Computer Science 31 Fall 2008 Final...

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

View Full Document Right Arrow Icon
UCLA Computer Science 31 Fall 2008 Final Review Problems The solution will be uploaded at http://www.cs.ucla.edu/~schoi/cs31 after the discussion. 1. Fill in the line appropriately to complete the out function, such that it outputs “out” on the screen (without quotes). The line of code you write must not use any brackets, and should copy a character from “computer”. void out(char* str, char* &ptr) { ptr = new char[4]; *ptr = str[1]; str += 3; ptr[1] = *str; ptr++; ________________________ ptr += 2; ptr[0] = '\0'; ptr -= 3; } int main() { char* word = "computer"; char* newWord; out(word, newWord); cout << newWord << endl; delete[] newWord; return 0; }
Image of page 1

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

View Full Document Right Arrow Icon
2. Design the class Goldfish , which models a creature intelligent enough to remember at most capacity characters at a time. Class Goldfish { public: Goldfish(int capacity); ~Goldfish(); void remember(char c); void forget(); void printMemory() const; // prints the content of m_memory private: char* m_memory; // pointer to memory int m_amount; // # chars remembered int m_capacity; // # chars this fish can remember }; (a) Define the constructor. Dynamically allocate capacity ‐many characters to m_memory . Initialize m_memory with dot(‘.’)s. If capacity is not positive, then set it to 3 by default. Remember the m_capacity . (b) Implement remember . Store the character c into m_memory . If remembering a new character makes the total number of remembered characters exceed m_capacity , then discard the oldest character from m_memory to open up a free slot. (This is an example of LRU (Least Recently Used) replacement policy.)
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern