UCLA Fall 2011 CS 31 Projects and Solutions

UCLA Fall 2011 CS 31 Projects and Solutions -...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
 
Background image of page 1

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

View Full DocumentRight Arrow Icon
Final Practice  TA: Brian Choi (schoi@cs.ucla.edu)  Section  Webpage:  http://www.cs.ucla.edu/~schoi/cs31 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++; (b)  foo++; (c)  (*foo)++;  (d)  (*ptr)++; (e) (a) and (b) only (f) (a) and (d) only (g) (b) and (d) only (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; 3. Write  delete  statements that correctly delete the following dynamically allocated entities.  int *p1 = new int[10];int *p2[15];for (int i = 0; i < 15; i++) p2[i] = new int[5];int **p3 = new int*[5];for (int i = 0; i < 5; i++) p3[i] = new int;int *p4 = new int;int *temp = p4;p4 = p1;p1 = temp; 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.) 
Background image of page 2
5. Design the class  Goldfish , which models a creature that is intelligent enough to remember  capacity  characters at a time.  class Goldfish { public: Goldfish(int capacity); ~Goldfish(); void remember(char c); void forget(); // Clears m_memory using dots('.') void printMemory() const; // Prints the content of m_memory private: char *m_memory; // Pointer to memory. int m_amount; // # of chars remembered.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/06/2012 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 12

UCLA Fall 2011 CS 31 Projects and Solutions -...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online