Question: part 2: Now write the function int countCollisions (int M, vector inputs). It takes the size of the hash table as input and a vector
part 2:
Now write the function int countCollisions (int M, vector inputs). It takes the size of the hash table as input and a vector of strings. It goes over all the input strings, performs the above simpleHashFunctionover it and counts the total number of collisions at each index in the hash table (index ranges from 0 to M1M1). In the main function, there is a code to test how the number of collisions change on chaging the size of the hash table. We vary the size from 1 to 11. I wrote part 2 but it seems to not work and Im not sure whats wrong with the code
//Hash.cpp
#include
#include
#include "Hash.h"
using namespace std;
int hashFunction(string s, int M) {
// Your Code Here
//hash function to sum up the ASCII characters of the letters of the string
int sum_ASCII = 0;
for(int i=0;i
sum_ASCII += s[i];
}
return (sum_ASCII%M);
}
int countCollisions (int M, vector inputs) {
int collisions = 0;
int hashValue;
// Your Code Here
// Your Code Here
vector hashTable(M,0); // Intializes hashTable of size M with 0 values
for(int i=0;i
hashValue = hashFunction(inputs[i],M); //Calculate hashValue of String inputs[i]
if(hashTable[hashValue] > 1) //if hashValue already exists in hashTable then increment collisions
collisions++;
hashTable[hashValue]++; // keep updating hashTable with count of occurence
}
return collisions;
}
//Hash.h
#ifndef HASH_H #define HASH_H
#include #include #include "Hash.h"
int hashFunction(std::string s, int M);
int countCollisions (int M, std::vector inputs);
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
