You may work in your duet partnership on this assignment if you wish.
We have provided some starting files. Like other warmups, and unlike your project, the design is well specified so that you can just fill in the implementation.
Note that the code to enable the -s option is part of htbl-main.c. It uses a library function getopt, which is the standard command-line option utility for C programs. The application also supports a command-line option b (for "bad"); run with -b to use a bad hash function (instead of the good one whose details follow). Please note that sometimes the hash function works differently on different machines (i.e., computes a different hash code per string, given what unsigned long int means on that system), leading to different configurations of the hash table on identical input. Therefore, if your output differs from another's, it doesn't necessarily mean anything is wrong. You are welcome to share the output of this lab on piazza, since sharing the output alone is not giving anything away. If you do share, make sure you specify the platform (i.e. the operating system and version and CPU) on which you compiled and ran the code.$ make htbl ... $ ./htbl < ./cnets2016 ... $ ./htbl -s 99 < ./cnets2016 ... $ ./htbl -b -s 99 < ./cnets2016 ...
You need to write two functions to get from the strings stored in the hash table to the index in which you should store the item. First, you need to calculate a key from the string.
Follow this recipe for computing an unsigned long int key for a string:
1. Initialize an unsigned long int result to 17 (unsigned long res = 17;).
2. Loop over the characters in the string. For every character, set the result to be 37 times itself plus the ASCII numeric value of the character.
(This hashing algorithm recipe is from Effective Java by Joshua Bloch.)
For example, the hash code for the word FORK, given that the ASCII values of the characters in FORK are 70, 79, 82 and 75, is as follows:
res = 17 res = 37*17+70 = 699 res = 37*699+79 = 25942 res = 37*25942+82 = 959936 res = 37*959936+75 = 35517707 (As you can see, the numbers get big.)
This key gets stored with the item so that you don't need to go through this again for the same string. Then, you need a hash function that obtains the index from the key.
To identify the index of FORK in a hash table of size, say, 33, you compute the remainder of the key when divided by 33. In this case 35517707%33 is 5, so FORK would be inserted into and/or located in the bucket with index 5 (the sixth bucket in the table).