Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that: (1) defines and implements a hash class that construct

ID: 3715828 • Letter: W

Question

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.

?please make a program that was not used before so I can see the different ways of making such program and please use documentation as well

Explanation / Answer

#include <cstdio>

#include <cstdlib>

#include <iostream>

#include <string>

#include <vector>

using namespace std;

const int MAX = 23;

class HashClass

{

vector<string> storearray;

int sum, i, check;

string hashthing;

char fletter, lletter;

public:

HashClass::HashClass() : storearray(MAX)

{

for(i=0;i<23;i++)

storearray.at(i) = "0";

}

void hashit(char fletter, char lletter, string hashthing)

{

sum = (fletter+lletter)%23;

if (storearray.at(sum) == "0")

storearray.at(sum) = hashthing;

else if (storearray.at(sum) != "0")

{

do{

i = 0;

if (sum > 21)

sum = 0;

else

sum++;

if (storearray.at(sum) == "0")

{

storearray.at(sum) = hashthing;

i++;

}

}while (i != 1) ;

}

}

void hashcheck(char fletter, char lletter, string hashthing)

{

sum = (fletter+lletter)%23;

if (storearray.at(sum) == "0")

cout<<hashthing<<" is not in this list."<<endl;

else if (storearray.at(sum) != "0")

if (storearray.at(sum) == hashthing)

cout<<hashthing<<" was found at position #"<<sum+1<<" on list."<<endl;

else

{

check = 0;

do{

i = 0;

if (sum > 21 && check == 0)

{

sum = 0;

check = 1;

}

else

sum++;

if (sum > 21 && check == 1)

{

cout<<hashthing<<" is not in this list."<<endl;

sum = 0;

i++;

}

if (storearray.at(sum) == hashthing)

{

cout<<hashthing<<" was found at position #"<<sum+1<<" on list."<<endl;

i++;

}

}while (i != 1) ;

}

}

void display()

{

for(i=0;i<23;i++)

if (storearray.at(i) == "0")

cout<<i+1<<". "<<"empty"<< endl;

else

cout<<i+1<<". "<<storearray.at(i)<< endl;

}

};

int main()

{

HashClass hash;

char fletterter,lletterter;

int leng, i;

string word;

cout<<"Input 15 different words. ";

for (i=0;i<15;i++)

{

cout<<"#"<<i+1<<" ";

cin>>word;

fletterter = word.at(0);

leng=word.length();

lletterter = word.at(leng-1);

hash.hashit(fletterter,lletterter,word);

}

hash.display();

cout<<endl;

cout<<"Enter a word to see if it is in hast table."<<endl;

cout<<"For exit enter '0'."<<endl;

do{

i =0;

cout<<"Enter word: ";

cin>>word;

if (word != "0")

{

fletterter = word.at(0);

leng=word.length();

lletterter = word.at(leng-1);

hash.hashcheck(fletterter,lletterter,word);

}

else

i++;

}while (i != 1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote