A program that calculates a hash code for a given string




Download the HashCode.cpp file and implement the details of the function called hashCode(). The function takes a given string and will need to add the ASCII value for each character in the string to produce a hash key. Assume the hash table has a size of 84 so that a hash code can be generated by taking the value of the sum of the characters modulo 84.

Hint: You only need to provide the details for the areas of the code that are marked with TODO comments. .All of the other areas of code, including the data strings, and function signatures will not change. For the 2 calculateHash() function, a for-loop needs to be created similar to the one in the dumpTable() function, except your for-loop will be user to add the value of each character in the string based on the overall length of the string. You can use the length() or size() string function to determine the length of the passed in string called key. Before the loop, declare an integer variable (e.g. value). Then within the loop, and add the ASCII value of each character to this integer:

// index is the index from the for-loop
value = value + static_cast(key[index]);

After the loop return the modulus of the value variable (or the name that you have given your integer).

For the insertTable() function, use an if-statement to check whether or not location in the hash table is occupied. This will be similar to the check that is performed in the dumpTable() function, except you will use the hashCode variable instead of the index variable. Print an error message if the position in the hash table is occupied and do not insert the string into the hash table.

Output: The output for the program after the function is implemented should appear as follows:

Chakotay hashCode=64

Janeway hashCode=47

Neelix hashCode=25

Seven of Nine hashCode=8

Tuvok hashCode=33

Dax hashCode=33

Error: Dax collides at hashCode=33 not inserting

O’Brien hashCode=26

Quark hashCode=12

Dr. Bashier hashCode=38

Kira hashCode=55

B’Elanna hashCode=24

Picard hashCode=7

Riker hashCode=5

Data hashCode=42

La Forge hashCode=32

Worf hashCode=78

Dr. Crusher hashCode=68

Reed hashCode=48

Travis hashCode=45

Hoshi hashCode=3

Dr. Phlox hashCode=27

Kirk hashCode=65

Spock hashCode=8

Error: Spock collides at hashCode=8 not inserting

Dr. Pulaski hashCode=65

Error: Dr. Pulaski collides at hashCode=65 not inserting

Wesley hashCode=45

Error: Wesley collides at hashCode=45 not inserting

Troi hashCode=78

Error: Troi collides at hashCode=78 not inserting

Tasha hashCode=77

Sisko hashCode=17

Odo hashCode=38

Error: Odo collides at hashCode=38 not inserting

Bones hashCode=83

Scotty hashCode=58

Chekov hashCode=20

Uhura hashCode=13

Sulu hashCode=5

Error: Sulu collides at hashCode=5 not inserting

Nurse Chapel hashCode=54

Doctor hashCode=31

Harry hashCode=14

Tom hashCode=52

Kes hashCode=39

Archer hashCode=9

T’Pol hashCode=2

Tucker hashCode=34

Hash Table:

Index Key

2 T’Pol

3 Hoshi

5 Riker

7 Picard

8 Seven of Nine

9 Archer

12 Quark

13 Uhura

14 Harry

17 Sisko

20 Chekov

24 B’Elanna

25 Neelix

26 O’Brien

27 Dr. Phlox

31 Doctor

32 La Forge

33 Tuvok

34 Tucker

38 Dr. Bashier

39 Kes

42 Data

45 Travis

47 Janeway

48 Reed

52 Tom

54 Nurse Chapel

55 Kira

58 Scotty

64 Chakotay

65 Kirk

68 Dr. Crusher

77 Tasha

78 Worf

83 Bones

** Press any key to continue **