Lab 11: CPS08: Pointers, Searching, Animals You should work on this assignment on mentor. It is on the acpub system as well, but you should get used to working on mentor during lab. It would be prudent, however, to back things up by ftp-ing files to your acpub account. If you can't log into mentor, get help NOW! *******Logging in to mentor From an xterm window type telnet mentor.cs.duke.edu to connect to the machine mentor. Change into your cps08 directory and create a lab11 subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). cp ~ola/cps08/lab11/* . If you type ls you should see the following files: Makefile, dophone.cc, cdread.cc, and animals.cc. You need to give your machine permission for mentor to connect to it. To do this type in an xterm on the DEC station NOT in a mentor xterm the following: xhost +mentor.cs.duke.edu. Do NOT put a space after the + sign. You should then be able to type emacs to get an emacs window. You want to use the ampersand which leaves you with control of your xterm (so you can run programs from it). If this doesn't work, check with a TA since all students should have working mentor accounts. *********Lab First make a link to several text data files. To do this type ln -s /u/ola/misc/data data Then you'll be able to use the directory data to access several big text and data files. ******Searching Phone lists Use the emacs commands C-x C-f to find/load the file dophone.cc. Then compile this command using C-x c and type make dophone in the minibuffer. You'll be prompted for the name of a file. Enter data/phone.dat and the program will load a list of 5,961 1-800 phone numbers in North Carolina. You'll then be prompted for a number of searches. Enter 1500 searches. This will cause the program to search for the first number, the second, the third, etc., using two different searching methods: sequential and binary search. Run the program again and make 2500 searches. How long does it take for 2500 searches using sequential search? ________________? Using binary search ___________________? The for loops in main that control the searches run from 0 to limit. Change these loops so that they run from count downto limit. Run the program again for 2,500 searches. Do the times change (significantly) from what you noted before? How do you account for this? ********************Pointer Stuff Now load the file cdread.cc, compile it, and run it. This reads the same kind of information about CD's that was used in program 5. All the struct and class definitions are included in one file rather than in a .h and .cc file. When prompted for the name of an input file, enter data/minicd.dat. This file contains information for 2006 CD's. The lines for printing the CD's are currently commented out. The program reads CD information, and, during the reading, stores each CD into the proper position according to title. This is done by ``sliding'' CD info to the right until the correct location for the CD is found. Selection sort is used to sort the CD's by group name. How long does it take to read the file of 2006 CD's ___________? How long does it take to sort the file ____________? Can you think of a reason for this difference? From a mentor> prompt, copy the file cdread.cc to a file cdread2.cc. To do this use the Unix cp command: cp cdread.cc cdread2.cc Now load the file cdread2.cc into emacs. You'll change the definition of the !Vector! defined in the !Collection! struct so that is stores pointers to CDinfo objects rather than CDinfo objects themselves. Make this change to the definition: Vector list; // array of CD information by adding an asterisk as shown so that list is a vector of pointers. Try to compile the program. You'll get an error message like the one below. cdread2.cc:89: request for member `title' in `this->Vector::operator [](int)((k - 1))', which is of non-aggregate type `CDinfo *' To get to the line with the error on it, type C-x ` (that's the back-quote mark). You can also type C-x g and then the number of a line, to go to a specific line. This means that CDinfo * is a non-aggregate or non-struct type. Rather than use a dot to access a field of the struct, you must use an arrow -> to get to the struct that is pointed to by list[k-1] ; 0 < k && title < list[k]->title; Make this change. Type C-x ` to go to the next error (something about cannot convert CDinfo...). Here you'll need to create memory for the pointer to reference. Add the word new as shown below. list[k] = new CDinfo(atoi(num),title,atof(price),group); Type !C-x `! to go to the next error ( request for member `group' ...). You'll need to change the . used to access the group field to an arrow !->! for the pointer to work. Type !C-x `! to go to the next error. This error is due to the improper definition of !temp!. Change the definition so that !temp! is a pointer: CDinfo * temp; Then recompile the program. It should compile successfully; if not, ask for help or make the changes needed to make it compile. Run the program using data/minicd.dat as data. How long does it take to read the data now ______________? to sort the data ______________? (If you print the CD information it won't print properly. You'll need to change the list[k] in the Collection::Print function to *list[k] --- don't worry about this for now.) Try to provide a reason for the change in time when pointers are used. ***********Extra Credit: Animals Load the program animals.cc into emacs, compile it and run it. The program will print something different each time it is run (it uses the Dice class). This program uses the concept of inheritance to create new kinds of students. There is a default, normal student and a geeky student. To earn extra credit, create a new class that represents some other kind of student. Name the class appropriately and model it after the Geek class shown in the program. The member function Behavior should ``act differently'' for the new student, as should the function Fun. You'll need to add another if statement in main to account for the new kind of student --- based on the roll of the dice object you'll want to create an object (that will be pointed to) of your new class type. ********Submission To turn in programs: When your modified programs works, you can submit it, along with a README describing how long it took you and what your impressions of the lab are. Submit works on the DEC stations and on mentor. You can type make submit to submit the appropriate files, or you can type the command below (this doesn't include words.cc). submit08 lab11 README cdread2.cc animals.cc The README file should include your name, how long you worked on the program, and anyone you received significant help from. You should also include any comments you have about the lab. You should receive a message telling you that the programs were submitted correctly.