Question
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started