Question

Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...

Please complete the following task: Create a C++ hash table program for generating a hash from a string.

Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function).

Requirements:

  1. Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index is produced from the hash function.
  2. Read each line of the input file (words.txt).
  3. Create a hash from each word using the hash function.
  4. Modulus the results of your hash function by 45,402 to get an index, and increment the appropriate bucket (array entry).
  5. After the entire file is processed, calculate and print the percentage on hashes that resulted in collisions (# of collisions / total strings hashed *100)
  • The number of collisions can be obtained from the array at the end of the program. Any values >1 in the array indicate that a collision occurred on that index. For example, if theArray[102] = 3, that means two collisions occurred on index 102 because only one value should have been mapped there by a perfect hash function.
  1. Write the contents of the array to a file named hashresults.csv, then import it into a spreadsheet program and graph the results.

Please use only #include, #include, and #include.

Link to the word.txt file contents: (file:///C:/words.html)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have answered the similar question before. The same program I am attaching with necessary changes. Below is the program which solves your question completely. Since you haven't provided any words.txt I have taken a file which contains of 30 words and solved using that but this works for any size. To change the size, just to the program and on the top you will see a line like "#define SIZE 30". There remove 30 and keep the size whatever you want.

INPUT:

words - Notepad File Edit Format View Help this is some text some another text see the blank line in the middle

CODE:

############################

#include<iostream>
#include<fstream>
using namespace std;

#define SIZE 30

long hashArray[SIZE];

long getHash(string s)
{
long h=0, temp;
for(int i=0;i<s.size();i++)
{
temp = s[i];
h = (h*10+temp)%SIZE;
}
return h;
}

int main()
{
ifstream in_file("words.txt");
string s;
long collisions=0, h;
while(in_file>>s)
{
h = getHash(s);
hashArray[h]++;
if(hashArray[h]>1) collisions++;
}
cout<<"Total size of hash array: "<<SIZE;
cout<<"\n\nNumber of collisions: "<<collisions;
cout<<"\n\nPercentage of collisions: "<<(double)(collisions*100)/SIZE<<"\n\n";

//exporting to csv file
ofstream out_file("hashresults.csv");
for(long i=0;i<SIZE;i++)
{
out_file<<i<<','<<hashArray[i]<<'\n';
}
}

##############################

OUTPUT:

Total size of hash array: 30 Number of collisions: 14 Percentage of collisions: 46.6667 Process returned e (exe) execution ti

Add a comment
Know the answer?
Add Answer to:
Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • in java, Hash 8 randomly generated int values (in the range [ 0 - 99 ]...

    in java, Hash 8 randomly generated int values (in the range [ 0 - 99 ] inclusive). The random number generator is initially seeded to value 97. Each generated value is stored in a hash table size 11. The first hash function, h1(key) is the division modulo sizeof(table). I.e., h1(key) = key % sizeof(table) h1(key) = key % 11. Any collisions will NOT be stored in the hash table. You will ignore collisions. The hash table is a random access...

  • LINUX C programming Please make sure the output is correct Please do read data from file,...

    LINUX C programming Please make sure the output is correct Please do read data from file, the example below is just a example Third: Hash table (20 points) In this part, you will implement a hash table containing integers. The hash table has 10,000 buckets. An important part of a hash table is collision resolution. In this assignment, we want you to use chaining with a linked list to handle a collision. This means that if there is a collision...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT