My question is:
Open a file in your main. Your file has one word in each line. I'm supposed to read a file sum up the ascii values of each word, the sum mod the hash size will be my key and i'm supposed to take care of the collisions with linear probing. for linear probing I need to create a linear probing class. I have two questions:
a) I do not understand this error message.
"undefined reference to `LinearProbing::HashFunction(std::string, int, int)"
I do not understand why it says that.
b) Have I done the Insert method right? My logic is that if the spot of a given key is empty, insert the word in that key or index. If not empty, go one step down and insert the word there.
Any input on this matter would help me greatly!! and Please no sarcastic remarks! Thank you!!!
What I have tried:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int HASHSIZE=100;
string HashTable[100];
class LinearProbing{
int HashFunction(string,int,int);
void InsertWord(string);
int SearchWord(string);
};
int main(){
string word;
string line;
fstream file;
int index;
cout<<"Enter a word you would like to search: ";
cin>>word;
file.open("text.txt");
while(file){
getline(file,line);
}
file.close();
}
int HashFunction(string key, int index,int HASHSIZE){
int sum=0;
for(int i=0;i<index;i++){
sum=sum+key[i];
}
return sum%HASHSIZE;
}
void LinearProbing::InsertWord(string word){
int key= LinearProbing::HashFunction(word,word.length(),HASHSIZE);
for (int i = 0; i < HASHSIZE; i++){
if (HashTable[key].empty()==true){
HashTable[key] = word;
return;
}
else{
HashTable[key+1] = word;
}
}
}
int LinearProbing::SearchWord(string word){
int key=LinearProbing::HashFunction(word,word.length(),HASHSIZE);
for (int i = 0; i < HASHSIZE; i++){
if (HashTable[(key + i)%HASHSIZE] == word){
cout<<"The word you're looking for is in "<<key+i<<"position";
}
}
cout << "Value not contained in table";
return 0;
}