Quadratic Hash issues

I am to create a hash table where I am in taking strings and converting them to ascii and totaling them. I am then to take the values and hash them ( value = (11*asciiTotal)%19.

So the issue I am having is when theres a collision, USING quadratic probing, and then using h fcn to help me insert new words to the array.... (hope that made sense). What is happening, is that when I insert a new word, it just goes to the first open spot in the array, which shouldnt be happening.. ANY HELP IS APPRECIATED!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280




//prototypes
int wordTotal(string word);
void insertWord(string word, string table[]);
void isFull(string table[]);
int searchWord(string word, string table[]);
void menu(string table[], string word);
void displayArray(string table[]);
void remove(string table[], string word);
bool collisionCheck(string table[], int value);
void h(string table[], string word[]);

int main(){

	string hashTable[19];
	int asciiTotal = 0;
	string word; // string for the "key"; user input 
	string table[19]; // array for incoming words from txt document
	string hold[19];
	ifstream inFile; // instream name
	string fileName; // string to enter filename from user
	int n = 0; // iterative integer
	int index = 0;
	int value = 0;

	for (int i = 0; i < 19; i++){

		table[i] = "";

	}



	do {// Loop to enter file name
		inFile.clear();
		cout << "Please enter file name." << endl;
		cin >> fileName;
		cout << endl << endl;

		inFile.open("data.txt");
	}// End of Do-While Loop


	while (inFile.fail()); // If input of text file failes, repeat enter file

	while (!inFile.eof()) // reads data until end of file
	{

		getline(inFile, table[n]); // reads in word data		


		n++; // incrementing through table
		

	}
	
	h(hashTable, table);

	menu(hashTable, word); // User Menu
	
	cout << endl << endl; // creating space between the users choice input and the menu options

	inFile.close(); // closing inFile

	
}

// counts the total ascii values for each key


void menu(string table[], string word){

	int choice = 0;
	int index = 0;

	while (choice != 5){
		cout << "Please choose from the following options: " << endl;
		cout << "1: To add a word " << endl;
		cout << "2: To delete a word " << endl;
		cout << "3: To search for a word " << endl;
		cout << "4: To display contents of table " << endl;
		cout << "5: To exit" << endl;

		cin >> choice;



		if (choice == 1){
			cin.ignore(999, '\n');

			insertWord(word, table);

		}

		else if (choice == 2){
			cin.ignore(999, '\n');


			remove(table, word);


		}

		else if (choice == 3){
			cin.ignore(999, '\n');

			index = searchWord(word, table);
			if (index == -1)
			{

				cout << "Failure, word not found in list! " << endl;

				displayArray(table);
			}

			else
				cout << "The index of the word is: " << index << endl << endl;
		}

		else if (choice == 4){


			displayArray(table);
		}



	}

	cout << endl << endl;
}

int searchWord(string word, string table[]){

	cout << "Enter word to be searched: " << endl;

	getline(cin, word);

	system("cls"); // clearing screen


	for (int index = 0; index < 19; index++){ // searches through table size

		if (word == table[index]) // checking to see if word entered is equal to anything inside the table

			return index;

	}

	return -1;

} //done

void displayArray(string table[]){

	for (int i = 0; i < 19; i++){

		cout << "Location " << i << ": " << table[i] << endl;


	}

	cout << endl;

}

int wordTotal(string word){


	int asciiTotal = 0;

	for (int i = 0; i < word.length(); i++){ //loops through length of key

		asciiTotal = asciiTotal + (int)word[i]; //adds ascii values through casting, asccii totals become the key values


	}
	cout << asciiTotal % 19 << endl;
	return asciiTotal;
}

void insertWord(string word, string table[]){

	int asciiTotal = 0;

	cout << " Please enter word to be entered. " << endl;
	getline(cin, word);

	wordTotal(word); // calling word to turn input into ascii

	isFull(table); //checking to make sure it is not full

	for (int i = 0; i < 19; i++){

		if (table[i] == "" || table[i] == "0"){ // loop to insert word

			table[i] = word;

			i = 19; // stopping iteration
		}

	}


}

void isFull(string table[]){

	bool empty = true;

	for (int i = 0; i < 19; i++){

		empty = empty && table[i] != "";

	}

	if (empty)
		cout << "Array not full. " << endl;
	else cout << "Array is full. " << endl;
}

void remove(string table[], string word){



	cout << " Enter your word to be removed: " << endl;
	getline(cin, word);

	for (int i = 0; i < 19; i++){

		if (word == table[i])
			table[i] = "";

	}

}

bool collisionCheck(string table[], int value){


	if (table[value] == "0")

		return true;

	else return false;

}

void h(string table[], string word[]){


	bool used[19] = { false };
	for (int i = 0; i<19; i++)
	{
		bool found = false;
		int hash = wordTotal(word[i]) % 19;
		int tempValue = 1;
		do{
			if (used[hash] == false)
			{
				found = true;
				table[hash] = word[i];
				used[hash] = true;

			}
			else
			{
				hash = (hash += tempValue) % 19;
				tempValue += 2;
			}

		} while (found == false);
	}

	
}
As of this second, every time I run it, it crashes . My friend and I are both trying to tackle this assignment. The instructions given are : A hash table is a data structure in which the location of a record is determined directly
as a function of the record itself rather than by a sequence of trial-and-error comparisons
and is used when fast searching is needed.
When a record is to be inserted into a hash table, a hash function h is applied to the
record’s key in order to determine where the record is to be placed in the table. A common
example, a slight modification of which is to be used in this project, is:
h( record ) = key % tableSize where the table size is usually taken to be a
prime number (to best distribute the records in the table). If the keys are not numeric,
numeric codes, such as some function of the ASCII codes involved, are used.
1) Design a hash table class* for processing hash tables with the following
specifications:
— the records involved simply contain English words (or short phrases) up to 50
characters in length.
— the tableSize is to be 19. An array implementation should be used.
— the class* must provide operations for inserting a “word” into the table, deleting a
word from the table, searching for a given word (if the word is found, report the
position in which it was found; otherwise report the failure), and displaying a list
of the contents of the entire table (including “empty” locations).
— the hash function to be used is:
h( word) = 11 * (sum of the ASCII codes of the characters) % tableSize
— For collision resolution, you may use your choice of either quadratic probing or
double hashing. If double hashing is used, calculate the increment with:
incr( word ) = 11 * (sum of the ASCII codes of the characters) / tableSize
Hi,

After I added the required includes (Always remember to include these)

It doesn't compile: I used cpp.sh with all 3 warnings on. Always compile with a high level of warnings, with gcc use at least -Wall -Wextra -pedantic-errors

In function 'int main()': 
20:6: warning: unused variable 'asciiTotal' [-Wunused-variable] 
27:6: warning: unused variable 'index' [-Wunused-variable] 
28:6: warning: unused variable 'value' [-Wunused-variable] 
In function 'int wordTotal(std::string)': 
176:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 
In function 'void insertWord(std::string, std::string*)': 
188:6: warning: unused variable 'asciiTotal' [-Wunused-variable] 
In function 'void h(std::string*, std::string*)': 
272:36: warning: operation on 'hash' may be undefined [-Wsequence-point]


Line 176: use std::size_t type rather than int
Line 272: Always initialise your variables to something, preferably at declaration once you have a sensible value to assign to it.

Hope this helps :+)
Topic archived. No new replies allowed.