Playing with Pointers

 

  1. Example

    Compile and run the program pointplay.cpp. Copy the output into your README file and explain each line of output.
     

  2. Modifying the shuffle

    In the program shuffle.cpp change all three occurrences of tvector<Track> to the following to use a vector of Track pointers. tvector<Track *>

    Several changes must now be made in the rest of the code since pointers are used. Make the program compile and execute.
     

  3. Join the Club

    The program clublist.cpp is supposed to read in names and ids from a file and put them into an vector of pointers, but it currently does not do much. We would like to be able to print the list sorted by names and also sorted by ids. We will first focus on the names and getting them into the array. Later we will focus on reading them in sorted, and then sorting them by ids.

    Modify the program as follows:

    1. Modify the function InsertPerson to insert a new person into the array. Persons should be added to the array in the order they are read in. This function is passed a name and ID number. You should create memory using "new", and add this person to the vector list using push_back. Note that the tvector contains pointers, so you will add a pointer to the tvector (the pointer points to the new memory).

      Using the data file clublist.data your club vector should look like this:

      Compile and run to make sure this part works before moving on.

    2. Modify the function FindId to look through the list (using sequential search) for a person with the given ID number and return the index into the vector of the pointer to the person with the given ID, or -1 if the given ID is not found.

      Compile and run to make sure this part works before moving on.

    3. Modify the function InsertPerson again, this time to put items in sorted order by name as they are read in. That is, when you read an item in, find the location it should go in and then shift the other pointers down.

      Using the data file clublist.data your club vector should look like this (note it is sorted by name, the 0 pointer points to the first in alphabetical order):

      Compile and run to make sure this part works before moving on.

    4. Finally, we want to be able to have the list in sorted order by name and also by ID. We could have two arrays of type Person, one sorted by name and one sorted by ID, but that would take up a lot of extra space because each vector would be have another copy of each Person. Instead, we will have two vectors of pointers to Persons, and they will share the Person nodes.

      Using the data file clublist.data your club and clubID vectors should look like this (note club is sorted by names and clubID is sorted by IDs):

      You'll have to make several changes to do this.

       

      Modify the main function to include a second tvector of type Person * named clubID. Pass this tvector to ReadInput, which passes it on to InsertPerson.
      Modify InsertPerson to also insert into the clubID array. Note that the memory of the person and id can be shared, so you are really only inserting a pointer.
      In the main function, print the list sorted by name with a comment indicating this, and also print the list sorted by id, again with a comment indicating this.