Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ 1. debug this so that both BILL (Value: Roger and Wright) prints properly (under Hastable after inserting keys and values is:) 2. what

C++

1. debug this so that both BILL (Value: Roger and Wright) prints properly (under "Hastable after inserting keys and values is:")

2. what does "value = 37*value + k[i];" do and why was 37 chosen out of all the possibilities.

3. Describe what EACH line in int Map::find_index(), string Map::insert();, string Map::find(); string Map::remove(), does. You can also directly coment (using //) on the code if it's easier.

Thank you!

#include #include #include using namespace std;

class Map{ //declare all methods public: Map(); Map(string filename); string find(const string& key); void insert(const string& key, const string& value); string remove(const string& key); void print(); protected: unsigned int hash(const string& key); int find_index(const string& key, bool override_duplicated_key); const static unsigned int size = 100; string keys[size]; string values[size]; };

Map::Map(){ for (int i = 0; i < size; i++){ keys[i] = string(); values[i] = string(); } }

unsigned int Map::hash(const string& k){ unsigned int value = 0 ; for (int i = 0; i < k.length(); i++){ value = 37*value + k[i]; } return value; }

void Map::print() { cout << "{"; for (int i = 0; i < size; i++) if (!keys[i].empty()) cout << keys[i] << ":" << values[i] << ", "; //prints all keys and values cout << "}" << endl; cout<

int Map::find_index(const string& key, bool override_duplicate_key = true){ unsigned int h = hash(key) % size; unsigned int offset = 0; unsigned int index; while (offset < size){ index = (h + offset) % size; if (keys[index].empty()|| (override_duplicate_key && keys[index] == key)) return index; offset++; } return -1; }

void Map::insert(const string& key, const string& value) { //user k/v passed as parameters int index = find_index(key); // finds index of the key entered by user if (index == -1) { cout << "Table is full!" << endl; return; } keys[index] = key; values[index] = value; }

string Map::find(const string& key) { int index = find_index(key); if (index != -1) return values[index]; return ""; }

string Map::remove(const string& key){ int index = find_index(key); if (index == -1) return ""; string value = values[index]; keys[index].clear(); values[index].clear(); return value; }

int main(){ Map map; cout << "Hastable: " << endl; map.print(); map.insert("Jane", "Smith"); map.insert("John", "Do"); map.insert("Susan", "Collins"); map.insert("Bill", "Rodgers"); map.insert("Eric", "Jones"); map.insert("Bill", "Wright"); map.insert("Mike", "Bader"); cout << "Hastable after inserting keys and values is: " << endl; map.print(); cout << "Mike: " << map.find("Mike") << endl; cout << "Bill: " << map.remove("Bill") << endl; cout<

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions