Question: The program I wrote was not acceptable. Please correct the program so that it meets the requirements. The issue: Program is incomplete...the last part (repeatedly

The program I wrote was not acceptable. Please correct the program so that it meets the requirements.

The issue: Program is incomplete...the last part (repeatedly query a user for a word and determine if it is in the stored hash) is missing.

Not acceptable as submitted

Prompt:

Write a C++ program that:

(1) defines and implements a hash class that constructs a 23 element array (may be implemented using a vector, a deque, or a list, if you prefer), storing strings, using the following hash function:

((first_letter) + (last_letter))%23

So, for example, the word "rocky" would be

('r') = 114 + ('y') = 121 = 235%23 = 5

In this example, an attempt would be made to store "rocky" in position 5. In the event of a collision, the word would be stored in the first available location, so if there is a collision in location 5, an attempt would be made to store the word in location 6. If there is a collision in location 6, try location 7 and so on.

(2) the driver program should:

a. query the user for fifteen words and store them using the hash technique described above.

b. print out the contents of each position of the array (or vector, deque, or whatever you used), showing vacant as well as filled positions. Remember, only 15 of the 23 positions will be filled.

c. repeatedly query the user for a target word, hash the word, check for its inclusion in the list of stored words, and report the result. Continue doing this task until the user signals to stop (establish a sentinel condition).

A solution that does not involve defining and using a hash class is not acceptable.

My program:

#include #include #include using namespace std; class hash { private: string data[23]; public: hash(){ for(int i = 0; i<23; i++){ data[i] = "-1"; } } void assign(string a){ int b = (int(a[0]) + int(a[a.size()-1])) % 23; if (data[b] == "-1"){ data[b] = a; } else { int c = b+1; while (data[c] != "-1" && c != b){ c = (c + 1) % 23; } if (c != b){ data[c] = a; } else { cout << "It is full "; } } } void print(){ cout << setw(10) << left << "Index" << " " << "Value" << endl; for (int i = 0; i<23; i++){ cout << setw(10) << left << i << " "; if (data[i] != "-1") cout << data[i] << endl; else cout << endl; } } }; int main(){ hash h; string s; while(true){ cout << "Enter a string or -1 to quit:"; cin >> s; if (s == "-1") break; h.assign(s); } h.print(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!