What You Need to Do
You have some freedom on this assignment.
First, create a structure (named Student) that will describe each of Genghis Khent’s students. The structure contains the following member variables:
- An ID consisting of 4 digits. The data type should be a string or c-string instead of an int.
- A name, which may have embedded spaces (like “Genghis Khent”). The data type is a string or c-string.
Second, create a text file which lists students (say 25) in the format ID number, space, name. For example:
6666 Mini Me
2333 Help Me
7722 Yogi Beria
Note: There should be no duplicate IDs. However, so there will be some collisions, for about 12 of the 25 students, have 2 students have the same last 2 digits of the ID (e.g., no ID will duplicate, but the last 2 digits of the ID will).
Third, create a hash table class (using .h and .cpp files preferably) whose one member variable points to an array of 100 student structures. The placement of a student structure within the array is based on the last 2 digits of a student’s ID. Thus, a student with an ID ending in 45 will go in the 45th subscript () of the array, unless there’s a collision. The hash table class also would include the following member functions:
Constructor – Assigns to each element of the array the value NULL for the ID and the name. (Those values are how you determine if a slot is “empty”.
Destructor – If you’re using dynamic memory allocation. Otherwise probably not necessary.
insert – Has 2 parameters, representing ID and name, which are assigned to the member variables of the structure instance Assigns a student’s information (ID and name) to the proper element of the array. Important: Uses hashing to determine the proper array element, and resolves collisions.
retrieve – Has 1 parameter, the ID, and returns the student’s name, or NULL if no student with that ID exists. Important: Uses hashing to find that student in the array, and deals with collisions.
Fourth, create a driver or test file. This code will:
- Create an instance of the hash table class.
- Load the hash table from the text file, using the insert member
- Display a menu:
- Find a student by ID. If chosen, the user is prompted to enter a 4 digit ID (no input validation necessary). Using the retrieve member function, the program either will output the name of the student associated with that ID, or report that no student has that ID.
- If chosen, the program will end.