Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with C++ program - calling a function while reading from a text file. My homework assignment: Design a hash table to hold strings

Please help with C++ program - calling a function while reading from a text file.

My homework assignment:

Design a hash table to hold strings with maximum length 32. You should use quadratic probing as the collision-resolution scheme. You have to implement the following functions in C++: hashIndex A hash function which maps a given key which is a string (its maximum length is 32) to an integer value between 0 and HASH_TABLE_SIZE-1. Make sure that your hash function uses all characters of the string in the computation.

Make sure that your hash table size is a prime number bigger than 1000. insert Inserts the given key into the hash table, and returns its location. If the key is already in the table, it returns the location of the key. main Reads a text file (sample.txt), and inserts all of the words in that file into the hash table. You assume that each word is separated with one or more white space character. After the insertion of all words, your main function should print the load factor of that hash table, and average number probes for successful searches for that hash table (Note: you are not going to use a formula to calculate the average number probes. In order to calculate the average number of probes for successful searches, your program should find the total number of probes during the insertions and divide it by the number of words.)

Since I will test your programs using Microsoft Visual Studio, make sure that your program works under Visual Studio.

Code:

************************* Main.cpp *********************

#include

#include

#include

#include

#include

using namespace std;

class HashNode

{

public:

vector value;

int key;

//Constructor of hashnode

HashNode(vector value, int K)

{

this->value = value;

this->key = K;

}

};

class Hash

{

public:

Hash()

{

this->Size = 32;

for (int i = 0; i

{

hashTable[i].key = 0;

}

}

Hash(unsigned int HashSize, unsigned int(*hashFunction)(string in))

{

this->Size = HashSize;

for (unsigned int i = 0; i

{

hashTable[i].key = 0;

}

}

int hashFunction(string in)

{

if (in[0] >= 'a' && in[0]

return in[0] - 'a';

else

return 0;

}

~Hash()

{

}

void Insert(string in)

{

int index = hashFunction(in);

if (hashTable[index].key == 0)

{

hashTable[index].key = 1;

vector v;

v.push_back(in);

hashTable[index].value = v;

}

else

{

hashTable[index].key += 1;

vector v = hashTable[index].value;

v.push_back(in);

hashTable[index].value = v;

}

}

unsigned int GetCount(string in)

{

int index = hashFunction(in);

int count = 0;

if (hashTable[index].key != 0)

{

hashTable[index].key += 1;

vector v = hashTable[index].value;

for (unsigned int i = 0; i

{

if (v[i] == in)

count++;

}

}

return count;

}

void PrintHashEntries()

{

for (unsigned int index = 0; index

{

if (hashTable[index].key != 0)

{

cout

vector v = hashTable[index].value;

for (unsigned int i = 0; i

{

cout

}

cout

}

}

}

void PrintHashStats()

{

int used = 0;

int maxCount = INT_MIN;

int sum = 0;

int uniqueWordCount = 0, TotalWordCount = 0;

for (unsigned int index = 0; index

{

if (hashTable[index].key != 0)

used++;

if (hashTable[index].key > maxCount)

maxCount = hashTable[index].key;

sum += hashTable[index].key;

uniqueWordCount += hashTable[index].key;

cout

vector v = hashTable[index].value;

TotalWordCount += v.size();

}

cout

cout

cout

cout

cout

}

private:

unsigned int Size = 1019;

vector hashTable;

};

int main()

{

/* Please help call the functions below while using reading from a text file

*

* Would to call PrintHashStats() function after reading in the text file

*

* Hash hs;

* hs.PrintHashStats();

*

* will compile and read without the above call

*/

string hashTable[1019];

int hashKey = 0, i = 1;

ifstream myfile("keywords.txt");

if (myfile.is_open())

{

while (getline(myfile, hashTable[hashKey]))

{

cout

hashKey++;

}

while (i == 1)

{

cout

cin >> hashKey;

cout

cout

cout

cout

cin >> i;

cout

}

myfile.close();

}

else cout

system("pause");

return 0;

}

********************************* keywords.txt **********************

drivers

will

take

processes

request

from

device

and

execute

them

on

the

device

somethingIsPrinting

king

working

image text in transcribed

DReferences Microsoft Visual C+Runtime Library DExternal Depende Header Files Resource Files Source Files Debug Assertion Failed! Program: CAWINDOWSSYSTEM32MSVCP140D.dll File: cprogram files (x86)microsoft visual studio 2017enterpriselvctoolsimsvc 14.13.26128 includelvect or Line: 1806 keywords.txt DMain.cpp Expression: vector subscript out of range For information on how your program can cause an assertion failure, see the Visual C++documentation on asserts. Press Retry to debug the application) Abort Retry gnre DReferences Microsoft Visual C+Runtime Library DExternal Depende Header Files Resource Files Source Files Debug Assertion Failed! Program: CAWINDOWSSYSTEM32MSVCP140D.dll File: cprogram files (x86)microsoft visual studio 2017enterpriselvctoolsimsvc 14.13.26128 includelvect or Line: 1806 keywords.txt DMain.cpp Expression: vector subscript out of range For information on how your program can cause an assertion failure, see the Visual C++documentation on asserts. Press Retry to debug the application) Abort Retry gnre

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Explain the importance of shelf registration.

Answered: 1 week ago

Question

Conduct a cost-benefit analysis for a training program.

Answered: 1 week ago