Objectives and Overview: This lab provides an opportunity to exercise two important classes, the BitArray class and the String class. It also suggests how you can design your own class, the StringList class, that will be useful in solving a scheduling problem. This scheduling problem is a generalization of the INDEPENDENT SELECTION problem on page 51 of your text.
Because this lab requires a more sustained programming effort than the first two, you will have two weeks to complete it. During the first week, you should complete and hand in your answers to Parts 1 and 2, leaving the second week free to concentrate on Parts 3 and 4.
You are also encouraged to work in teams to complete the programming parts of this lab.
The program files BitMain.cpp, BitArray.cpp, and BitArray.h are provided in the Courses -> CS210 (Tucker) -> source folder, for your use. Make copies of these files on the desktop for yourself and build a C++ project named Lab3.µ, as shown below:
The program BitMain.cpp generates random numbers to estimate the number of items from among N alternatives that will be left unselected after each of N people selects K items at random. It uses the BitArray B to identify the bits that are selected by a random number generating function Randint, as discussed in class.
Time slot 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Selected? 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1
Modify the BitMain.cpp program so that it displays its output in this fashion. That is, it displays all N entries in the BitArray B, not just the number that are still empty.
The String class interface is shown on page 80 of your text. It is a very useful class for storing and manipulating character strings. The program Stringtest.cpp is provided on the CS210 (Tucker) server to provide a program that tests the functions in this class. This program also tests C++ functions for reading input data from text files rather than from the keyboard.
To run this program, build a project called Lab3.µ on the desktop. Now drag copies of the following files from the CS210 (Tucker) -> source folder your Lab3 project folder and add all of these, in addition to Stringtest.cpp itself, to your project.
Now drag a copy of the file "schedule" from the CS210 (Tucker) folder into your Lab3 project folder. This file contains the data for the time slots and student preferences that you created last week in class. Now make and run this program.
You have some experience with linked lists of integers. Here, we propose that a new class, called StringList, be defined for linked lists of Strings. The implementation strategy may be the same as for linked lists of ints, except that new class interface StringList.h and implementation StringList.cpp files need to be created.
The public members of this new class should include:
The private members of this class should include a declaration for the node structure, pointers to the First and Last elements in the linked list, and an integer variable N that contains the number of nodes in the list. This suggests the following private declarations:
These functions should be tested using some simple statements in a main program before you proceed to Part 4. In your main program, don't forget to start with the line
and be sure to add the StringList.h and StringList.cpp files to your project before running.
Here is a new problem. Suppose we want to write a program that develops and displays a list of the actual time slots, and with each time slot, the first names of all students who selected it. For instance, if Dara, Alexios, and Nick selected time slot MWF9:30, then the display should include the following line:
MWF8:30 Dara Alexios Nick
Since each time slot is a string, as is the first name of each student, we can see that the StringList is an appropriate data structre for storing the names of time slots and students efficiently. Moreover, since the number of students selecting different time slots will vary widely, a very flexible data structure is required. Here is a linked list of strings (a StringList) that represents the above sample information.
Now we need to develop this information for all the time slots, not just MWF8:30. This suggests an array of StringLists, each one beginning with a different time slot. The above StringList would appear in the 3rd entry in that array for the data file schedule, preceded by a StringList for the time slot MW8:00 and followed by a StringList for the time slot MWF9:30. That is, the following array declaration should be near the beginning of your program:
StringList * TimeSlots = new StringList[n];
where n is the total number of time slots in the input data file (22 in our case). Now we can refer to the ith time slot as TimeSlots[i-1] (remember, array indexing begins at 0), and insert a new String, say S, into it by saying TimeSlots[i-1].insert(S).
With this information, you should design and complete this program (call it lab3yourname.cpp), using the StringList, String, and Exception classes to facilitate the coding.
On February 9, turn in your answers to Parts 1 and 2 of this lab, including a hardcopy listing of your modified BitMain.cpp program.
On February 16, submit to the Drop Box the class interface, class implementation, and main program for Parts 3 and 4. Don't forget to rename theseprogram filesby affixing your name to them. For example, if I were submitting a program file, it would be called lab3atucker.cpp, not just lab3.cpp.
Also, hand in hardcopy listings of these program files with your name(s) on them. Each one should contain sufficient commentary documentation for me to understand its overall design and logic.