CS31 Introduction to Computer Science I Spring 2011 Final Practice TA Brian Choi 1 Assume the following variable declarations int foo 0 int ptr foo Which of the following statements will change the value of foo to 1 a ptr e a and b only c foo g b and d only b foo f a and d only d ptr e c and d only 2 What is the output of the following code segment if the input value is the last digit of your student ID int array 10 4 6 2 3 1 3 2 2 7 9 int index cin index Enter a digit here int p array index for int i 0 i 5 i int hops p p hops cout p endl input 0 1 2 3 4 5 6 7 8 9 output 6 3 6 9 2 7 4 2 1 7 Copyright Brian Choi 2011 All Rights Reserved 1 CS31 Introduction to Computer Science I Spring 2011 4 Your high school friend who didn t make it to UCLA and settled for a university called U C sent you the following message through Facebook Hey I wrote the following function countMatches which is supposed to compare two C strings and count the number of matching characters Two characters match if they are the same and if they appear in the same position in each string For example countMatches UCLA U C count should set count to 1 countMatches Baseball ballpark count should set count to 2 etc I m not supposed to create any local variable or square brackets so I ended up submitting the following code void countMatches const char str1 const char str2 int count count 0 while str1 0 str2 0 if str1 str2 count str1 str2 But it doesn t work Darn pointers I asked my friends here at U C and none of them know how to solve this Can you tell me what I did wrong like you always did in high school You are too busy preparing for your finals so you decided to copy and paste his message and just mark the corrections What is your message going to be Make the corrections above Copyright Brian Choi 2011 All Rights Reserved 2 CS31 Introduction to Computer Science I Spring 2011 char m memory int m amount int m capacity public private Pointer to memory of chars remembered of chars this fish can remember 5 Design the class Goldfish which models a creature that is intelligent enough to remember capacity characters at a time class Goldfish Goldfish int capacity Goldfish void remember char c void forget Clears m memory using dots void printMemory const Prints the content of m memory cid 3 a Define the constructor Dynamically allocate capacity many characters to cid 3 m memory cid 484 cid 3 Initialize cid 3 m memory with dot s If capacity is not positive then set it to 3 by default Remember to set 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 m amount 0 Initialize the amount of memory used forget Fill in dots cid 3 b Implement remember Store the character cid 3 c cid 3 into cid 3 m memory If you already have m capacity characters memorized then discard the oldest character in cid 3 m memory cid 3 to open up a free slot This is an cid 3 example of LRU Least Recently Used replacement 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 Copyright Brian Choi 2011 All Rights Reserved 3 CS31 Introduction to Computer Science I c Implement cid 3 forget Clear memory by filling dot s into cid 3 memory void Goldfish forget for int i 0 i m capacity i m amount 0 m memory i Spring 2011 d Implement the destructor Goldfish Goldfish delete m memory To clarify here is how a Goldfish object can be used int main Goldfish nemo 3 nemo remember a nemo printMemory nemo remember b nemo remember c nemo printMemory nemo remember d nemo printMemory nemo forget nemo printMemory return 0 prints a cid 3 prints abc prints bcd prints Copyright Brian Choi 2011 All Rights Reserved 4 CS31 Introduction to Computer Science I Spring 2011 6 We ll define Aquarium class where Goldfish can live const int MAX FISH 20 class Aquarium public Aquarium bool addFish int capacity Goldfish getFish int n void oracle Aquarium Goldfish m fish MAX FISH Pointers to fish int m nFish Number of Fish private cid 3 a Define the constructor Initially there is no fish in the Aquarium Aquarium Aquarium m nFish 0 OR Aquarium Aquarium m nFish 0 b Implement addFish which dynamically adds a new Goldfish into the Aquarium with the specified memory capacity If the fish cannot be added because the Aquarium already has MAX FISH many fish residing return false and don t add any Otherwise return true bool Aquarium addFish int capacity if m nFish MAX FISH return false m fish m nFish new Goldfish capacity m nFish return true Copyright Brian Choi 2011 All Rights Reserved 5 CS31 Introduction to Computer Science I Spring 2011 c Implement getFish which returns the pointer to the Goldfish at position n Return nullptr if there are fewer than n fish in the Aquarium or if n is an invalid position Goldfish Aquarium getFish int n if n 0 n m nFish return nullptr return m fish n d Implement the destructor Remove all dynamically allocated objects Aquarium Aquarium for int i 0 i m nFish i cid 3 Make sure you understand why you can t do delete m fish cid 3 delete m fish i e Implement oracle It prints the memory of ALL Goldfish in the Aquarium You can use the member function printMemory of Goldfish class for this purpose Once everything is printed reset i e clear all Goldfish s memories using forget function void Aquarium oracle for int i 0 i m nFish i m fish i printMemory m fish i forget Why not m fish i printMemory or m fish i forget That is why would using dots instead of arrows be wrong Copyright Brian Choi 2011 All Rights Reserved 6 CS31 Introduction to Computer Science I Spring 2011 7 Design BankAccount class which lets the account owner to deposit and withdraw money using a password The class you write should meet the following requirements 1 There should be no default constructor 2 There should be only one constructor which takes in the initial balance in dollars and the password The password is an integer most likely 4 digits but we won t enforce that limit here which means it can even be negative The initial amount however must not be negative if it is negative set it to 0 00 by default Your BankAccount class should be able to keep track of the balance up to the last …
View Full Document