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

Info iconThis preview shows pages 1–3. 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: 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; } 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.) (c) Implement...
View Full Document

Page1 / 8

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

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