Question: c++ the function takes a given string and will need to add the ASCII value for each character in the string to produce a hash

c++

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. The hash table must have a size of 512. In this case, a hash code can be generated by taking the value of the sum of the characters modulo 512.

For the calculateHash() function, create a for-loop similar to the one in the dumpTable() function, except your for-loop will be used 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, 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.

#include

#include

#include

#include

#include

using namespace std;

int calculateHash(string key);

void insertTable(int hashCode, string item);

void dumpTable();

// TODO: Change the table size to match the hash table size mention

// in the handout.

const int TableSize = 0;

string hashTable[TableSize];

int main(int argc, char **argv)

{

string starTrekCharacters[] = {

"Reed", "Travis", "Hoshi", "Dr. Phlox", "Kirk", "Spock",

"Chakotay", "Janeway", "Neelix", "Seven of Nine", "Tuvok",

"Picard", "Riker", "Data", "La Forge", "Worf", "Dr. Crusher",

"Doctor", "Harry", "Tom", "Kes", "Archer", "T'Pol", "Tucker",

"Dr. Pulaski", "Wesley", "Troi", "Tasha", "Sisko", "Odo",

"Bones", "Scotty", "Chekov", "Uhura", "Sulu", "Nurse Chapel",

"Dax", "O'Brien", "Quark", "Dr. Bashier", "Kira", "B'Elanna",

};

const int numberOfCharacters = 42;

int hash = 0;

for (int i=0; i < numberOfCharacters; i++)

{

hash = calculateHash(starTrekCharacters[i]);

cout << setw(15) << left << starTrekCharacters[i]

<< "hashCode=" << setw(0) << hash << endl;

insertTable(hash, starTrekCharacters[i]);

}

dumpTable();

cout << " ** Press any key to continue ** ";

getchar();

return 0;

}

int calculateHash(string key)

{

// TODO: Use a loop to add together the ASCII value of

// each character in the passed in string called key.

//

// HINT: You can get the length of the string by calling the

// function key.size() or key.length().

return 0;

}

void insertTable(int hashCode, string item)

{

// TODO: Implement the details of the this function

// This function should insert item in the hashTable

// at the hashCode position. Print an error message

// if the position is occupied and do not insert

// the string into the hash table.

//

// HINT: the hashTable variable is an array. The

// position in the hashTable is occupied if the size()

// or length() of the position is greater than 0.

return;

}

void dumpTable()

{

cout << " Hash Table:" << endl;

cout << "Index\tKey" << endl;

for (int index=0; index < TableSize; index++)

{

if (hashTable[index].length() > 0)

{

cout << index << "\t" << hashTable[index] << endl;

}

}

return;

}

OUTPUT:

Reed

Travis

Hoshi

Dr. Phlox

Kirk

Spock

Chakotay

Janeway

Neelix

Seven of Nine hashCode=160

hashCode=384

hashCode=121

hashCode=507

hashCode=271

hashCode=401

hashCode=0

hashCode=308

hashCode=207

hashCode=101

Tuvok

Picard

Riker

Data

La Forge

Worf

Dr. Crusher

Doctor

Harry

hashCode=25

hashCode=83

hashCode=509

hashCode=378

hashCode=192

hashCode=414

hashCode=480

hashCode=107

hashCode=6

hashCode=304

hashCode=291

hashCode=85

hashCode=422

hashCode=110

hashCode=477

hashCode=121

Tom

Kes

Archer

T'Pol

Tucker

Dr. Pulaski

Wesley

Error: Wesley collides at hashCode=121 not inserting

Troi hashCode=414

Error: Troi collides at hashCode=414 not inserting

Tasha

Sisko

Odo

Bones

Scotty

Chekov

Uhura

Sulu

Nurse Chapel

Dax

O'Brien

Quark

Dr. Bashier

Kira

B'Elanna

Hash Table: Index Key 0 Spock

4 Quark

5 Uhura 6 Harry 9 Sisko 25 Tuvok

hashCode=497

hashCode=9

hashCode=290

hashCode=503

hashCode=134

hashCode=96

hashCode=5

hashCode=425

hashCode=122

hashCode=285

hashCode=102

hashCode=4

hashCode=450

hashCode=391

hashCode=184

283 Picard 85 Archer 96 Chekov

101 Neelix 102 O'Brien 107 Doctor 110 Tucker 121 Travis

122Nurse Chapel 134 Scotty 160Seven of Nine 184 B'Elanna 192La Forge

207 Janeway 271Dr. Phlox 285 Dax

290 Odo

291 Kes

304 Tom

308 Chakotay

378 Data 384 Reed 391 Kira 401 Kirk 414 Worf

422 T'Pol 425 Sulu

450Dr. Bashier 477Dr. Pulaski 480Dr. Crusher

497 Tasha 503 Bones 507 Hoshi 509 Riker

** Press any key to continue **

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!