Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ (Xcode) Object Orientated Programming. To receive full credit, programmers m

ID: 3842784 • Letter: C

Question

C++ (Xcode)

Object Orientated Programming.

To receive full credit, programmers must apply coding conventions for code block indentation, comments describing methods/classes, white space between code blocks, and variable/method/class naming conventions.

Hard Code a Hash Map that stores has doubles as values and integers as keys that will hold 100 key,value pairs. For this example create unique key values (Do not use map class from STD library.) Create an iterator that will allow the program to return the key,value pairs.

Fill the HashMap with ~20 values and output them to the screen.

Attach Snipping Photos of source code and output below.

Explanation / Answer

#include <list>
#include <iostream>
using namespace std;

const int SIZE = 100;

class Node{
public:
Node(){}
Node(int k, double v):key(k), value(v){}
int key;
   double value;
};

class HashMap {
private:
list<Node*> data[SIZE];

public:
~HashMap();
Node* get(int key);
void put(int key, double value);

int hashFunction(int val){ return val % 13; }
};

Node* HashMap::get(int key){
int hv = hashFunction(key);
int b = hv % SIZE;
list<Node*>::iterator it = data[b].begin();

while(it != data[b].end()){

Node ** d = &(*it);
if((*d)->key == key){
return *d;
}

it++;
}
return NULL;
}

void HashMap::put(int key, double value){
int hv = hashFunction(key);
int b = hv % SIZE;
Node* node = this->get(key);
if(node == NULL){
data[b].push_front(new Node(key, value));
}
else{
node->value = value;
}
}

HashMap::~HashMap(){
for(int i = 0; i < SIZE; ++i){
list<Node*>& val = data[i];
for(list<Node*>::iterator it = val.begin(); it != val.end(); it++){
Node* n = *it;
delete n;
}
}
}

int main(){
HashMap map;
  
map.put(6, 67.87);
map.put(8, 61.87);
map.put(3, 98.65);
map.put(20, 1.97);
map.put(1, 7.8);
map.put(2, 6.8);
map.put(4, 37.90);
map.put(5, 6.7);
map.put(7, 987.87);
map.put(9, 6.54);
map.put(10, 654.99);
map.put(11, 76.87);
map.put(12, 88.87);
map.put(13, 1.47);
map.put(14, 76.876);
map.put(15, 56.37);
map.put(16, 50.76);
map.put(17, 60.87);
map.put(18, 90.45);
map.put(19, 900.87);
  
  
  
   cout << "Finding 1: " << map.get(1)->value << endl;
   cout << "Finding 2: " << map.get(2)->value<<endl;
   cout << "Finding 3: " << map.get(3)->value << endl;
   cout << "Finding 4: " << map.get(4)->value<<endl;
   cout << "Finding 5: " << map.get(5)->value << endl;
   cout << "Finding 6: " << map.get(6)->value<<endl;
   cout << "Finding 7: " << map.get(7)->value << endl;
   cout << "Finding 8: " << map.get(8)->value<<endl;
   cout << "Finding 9: " << map.get(9)->value << endl;
   cout << "Finding 10: " << map.get(10)->value<<endl;   
cout << "Finding 11: " << map.get(11)->value << endl;
   cout << "Finding 12: " << map.get(12)->value<<endl;
   cout << "Finding 13: " << map.get(13)->value<<endl;   
cout << "Finding 14: " << map.get(14)->value << endl;
   cout << "Finding 15: " << map.get(15)->value<<endl;
   cout << "Finding 16: " << map.get(16)->value<<endl;   
cout << "Finding 17: " << map.get(17)->value << endl;
   cout << "Finding 18: " << map.get(18)->value<<endl;
   cout << "Finding 19: " << map.get(19)->value<<endl;   
cout << "Finding 20: " << map.get(20)->value << endl;
  
   return 1;
}

output:

Finding 1: 7.8

Finding 2: 6.8

Finding 3: 98.65

Finding 4: 37.9

Finding 5: 6.7

Finding 6: 67.87

Finding 7: 987.87

Finding 8: 61.87

Finding 9: 6.54

Finding 10: 654.99

Finding 11: 76.87

Finding 12: 88.87

Finding 13: 1.47

Finding 14: 76.876

Finding 15: 56.37

Finding 16: 50.76

Finding 17: 60.87

Finding 18: 90.45

Finding 19: 900.87

Finding 20: 1.97