The deadline for this lab is Wednesday 12 November 2008, xx:xx:xx hours. Strictly no submissions will be accepted after the deadline.
Initially, the hash table's bins are all empty. Each bin can contain one object (or one word in this case). If a word maps to a bin that is empty, we can simply place the word in that bin. If a word maps to a bin that already contains an existing word, there is a collision.
Given the infinite number of keys possible and the finite number of bins, it is very likely that more than one key will map to the same bin. This is called a “collision”, and we want to collect certain statistics about collisions.
Given a word k consisting of characters ci in ASCII (take each character's ASCII value), your hashing function f is defined as:
Here N is the number of bins, and modulo is the modulus operator. In your program, let N=50.
Assume that the text file is always called “test.txt” and that this file always exists. (It has been copied into your directory by the “pesetup” command.)
In this question you will write a program that will read, word-by-word, from a text file called “test.txt”. Simulate the insertion of each word in the order read from the file into the hash table. Once the entire file is read, print out the number of collisions, average number of collisions per bin, number of bins with collisions, and percentage of bins with collisions. Your program will produce an output similar to the following:
Number of collision for hash table = 37 collisions Average number of collisions per bin = 0.74 Number of bins with collisions = 18 Percentage of bins with collisions = 36.00%
You may assume that the text file contains only letters, spaces, and newlines. There are no punctuation nor digits in the text file.
You may use this skeleton if you wish.
#include <stdio.h> #define TESTFILE "test.txt" #define N 50 /* Number of bins */ /* Hashing prototype */ /* First parameter is the key, second parameter is the # of bins. Returns a bin number 0..N-1 */ int hash(char[], int); int main() {
} int hash(char str[], int n) { /* Hashing function. Returns a value between 0 and n-1 depending on the contents of str */ } |
Remember to submit your program frequently using the submit hashtable.c command, and check your submission using the check command.
All the best!