CPS 100, Fall 2002, Week #2

Answer the following questions and bring them to your recitation on Sept 9-11, 2002.

These questions refer to code in the program clublistS4.cpp.

Questions about clublistS4.cpp

  1. What happens when the following wacky lines are executed? (it wasn't compiled, so assume there are no syntax errors.) Is memory lost and where? Are there errors? Where does the program crash? tvector<Person *> Duke; Person * temp = new Person("Susan", 22); Duke.push_back(temp); Person * temp = new Person("Bob", 20); Duke.push_back(temp); Person * temp = new Person("Alison", 19); Duke.push_back(temp); Person * temp = new Person("Xiaobai", 20); Duke.push_back(temp); Duke[1] = new Person("Lars", 25); Duke[2] = Duke[0]; delete temp; Duke[0] = Duke[3]; delete Duke[0];
  2. Look at the functions Print and FindId in clublistS4.cpp. How is the tvector passed? Is this the best way to pass the tvector and why?

  3. Write a new function called PrintNameInIDRange that is given two integer parameters such that the first one is smaller than or equal to the second one, and it prints all the names whose ID is in this range. Note ONLY NAMES are printed, not IDs.

    For example, suppose the datafile is:

      Pridgen     42
      Garson      24
      Tippit      53
      Derryberry  99
      Mickle      17
    
    Then PrintNameinID(list,20,50) would output
      Garson
      Pridgen
    

    The header for this function is below.

    void PrintNameInIDRange(tvector<Person *> & list, int ID1, int ID2) // pre: ID1 <= ID2 // post: prints out the names of those with ID1 <= person's ID <= ID2
Susan Rodger
Last modified: Sat Sep 7 12:58:02 EDT 2002