cs31finalsol

# cs31finalsol - UCLA Computer Science 31 Spring 2009 Final...

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

UCLA Computer Science 31 Spring 2009 Final Review Solution 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 += 4; ptr[1] = *str; ptr++; *(ptr + 1) = *(str + 1); ptr += 2; ptr[0] = '\0'; ptr -= 3; } int main() { char* word = "computer"; char* newWord; out(word, newWord); cout << newWord << endl; delete[] newWord; return 0; }

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

View Full Document
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 to set the m_capacity . Goldfish::Goldfish(int capacity) { if (capacity < 1) m_capacity = 3; else m_capacity = capacity; // record the capacity m_memory = new char[m_capacity]; // allocate memory forget(); }
(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.) void Goldfish::remember(char c) { if (m_amount == m_capacity) { for (int i = 0; i < m_capacity - 1; i++) m_memory[i] = m_memory[i+1]; m_amount--; } m_memory[m_amount] = c; m_amount++; } (c) Implement forget . Clear memory by filling dot(‘.’)s into m_memory . void Goldfish::forget() { for (int i = 0; i < m_amount; i++) m_memory[i] = ‘.’; m_amount = 0; } (d) Implement the destructor. Goldfish::~Goldfish() { delete[] m_memory; }

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

View Full Document
3. We’ll define Aquarium class, where Goldfish can live. int MAX_FISH = 20; class Aquarium { public: Aquarium(); bool addFish(int capacity); Goldfish* getFish(int n); void oracle(); ~Aquarium(); private: Goldfish* m_fish[MAX_FISH]; // pointers to fish int m_nfish; // number of fish }; (a) Define the constructor. Initially, there is no fish in the Aquarium . Aquarium::Aquarium()
This is the end of the preview. Sign up to access the rest of the document.

## cs31finalsol - UCLA Computer Science 31 Spring 2009 Final...

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

View Full Document
Ask a homework question - tutors are online