Download the HashCode.zip file and implement the details of the function called
ID: 3539572 • Letter: D
Question
Download the HashCode.zip file and implement the details of the function called
hashCode() that takes a given set of strings to produce a hash code. Assume the hash table has a size of
16 so that a hash code can be generated by taking the value of the sum of the characters modulo 16. Note
that the hash table size is defined in the global constant called HT_SIZE.
Also implement the details of the function called insertItem(), which take a hash index and the string
to store in the global hash table called hashTable. The hash table in this case is an array of strings of
size HT_SIZE.
/**
* HashCode.cpp - This function will calculate a hash code for a given
* string and insert the string into the hash table at
* the provide position.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int hashCode(const char *key, int length);
void insertHashTable(int hashcode, string item);
void printHashTable();
const int HT_SIZE = 16;
string hashTable[HT_SIZE];
int main(int argc, char **argv)
{
string s = "";
int hash = 0;
s = "James Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Patty Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Joe Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Amber Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Bart Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Barbara Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
printHashTable();
cout << " ** Press any key to continue ** ";
getchar();
return 0;
}
int hashCode(const char *key, int length)
{
// TODO: Implement the details of the hashCode function here.
return 0;
}
void insertHashTable(int hashcode, string item)
{
// TODO: Implement the details of the insertHashTable function.
// This function should insert item in the hashTable at the hashcode
// position.
}
void printHashTable()
{
cout << " Hash Table:" << endl;
for (int i=0; i < HT_SIZE; i++)
{
if (strlen(hashTable[i].c_str()) > 0 )
{
cout << i << ": " << hashTable[i] << endl;
}
}
return;
}
Explanation / Answer
#include "HashAlgo.h"
/*Download the HashCode.zip file and implement the details of the function called
hashCode() that takes a given set of strings to produce a hash code. Assume the hash table has a size of
16 so that a hash code can be generated by taking the value of the sum of the characters modulo 16. Note
that the hash table size is defined in the global constant called HT_SIZE.
Also implement the details of the function called insertItem(), which take a hash index and the string
to store in the global hash table called hashTable. The hash table in this case is an array of strings of
size HT_SIZE.
/**
* HashCode.cpp - This function will calculate a hash code for a given
* string and insert the string into the hash table at
* the provide position.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int hashCode(const char *key, int length);
void insertHashTable(int hashcode, string item);
void printHashTable();
const int HT_SIZE = 16;
string hashTable[HT_SIZE];
int main(int argc, char **argv)
{
string s = "";
int hash = 0;
s = "James Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Patty Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Joe Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Amber Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Bart Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
s = "Barbara Smith";
hash = hashCode(s.c_str(), s.length());
cout << s << ", hashCode=" << hash << endl;
insertHashTable(hash, s);
printHashTable();
cout << " ** Press any key to continue ** ";
getchar();
return 0;
}
int hashCode(const char *key, int length)
{
long ASCIISUM=0;
for(int i =0; i < length; i++)
ASCIISUM+= int(key[i]);
return (ASCIISUM%HT_SIZE);
}
void insertHashTable(int hashcode, string item)
{
hashTable[hashcode] =item;
}
void printHashTable()
{
cout << " Hash Table:" << endl;
for (int i=0; i < HT_SIZE; i++)
{
if (strlen(hashTable[i].c_str())> 0 )
{
cout << i << ": " << hashTable[i] << endl;
}
}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.