Question

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:

  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<iostream>, #include<string>, and #include<fstream>.

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:
I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...
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...

  • 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: 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...

  • 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...

  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • 0. Introduction. This involves designing a perfect hash function for a small set of strings. It...

    0. Introduction. This involves designing a perfect hash function for a small set of strings. It demonstrates that if the set of possible keys is small, then a perfect hash function need not be hard to design, or hard to understand. 1. Theory. A hash table is an array that associates keys with values. A hash function takes a key as its argument, and returns an index in the array. The object that appears at the index is the key’s...

  • Question 39 Language C++ Question 39 15 pts Implement a program that finds the shortest string...

    Question 39 Language C++ Question 39 15 pts Implement a program that finds the shortest string in a dynamically allocated array of strings. The program first asks the user for the number of names needed. Afterwards, the user is prompted to enter each name. The program then passes the array to a function. The function returns the index of the shortest element. The program then prints out the shortest element and the index where it is stored in the array....

  • 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)...

  • Insert elements into a hash table implemented using chain hashing, as an array of linked list...

    Insert elements into a hash table implemented using chain hashing, as an array of linked list in which each entry slot is as a linked list of key/value pairings that have the same hash (outcome value computed using certain hash function). You are allowed to use “Hash Function”, h(k) = k % x where, x is the value you will need to decide to use, that you find appropriate for this implementation. The main requirements are the following: 1. Input:...

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
ADVERTISEMENT