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

2. C++ data structures algorithms. Please follow ALL instructions and write your

ID: 3842641 • Letter: 2

Question

2. C++ data structures algorithms. Please follow ALL instructions and write your own code Give sample output too. Thanks Object orientated Programming or Procedural Programming are ok. 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

Hi,

Please find the code to the above mentioned scenario below:-

CODE:-

======================================================================================

//g++ 5.4.0

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
#include <map>
using namespace std;
const int TABLE_LENGTH = 100;

/*
* HashEntry Class Declaration
*/
class HashEntry
{
public:
int key;
double value;
HashEntry(int key, double value)
{
this->key = key;
this->value = value;
}
};

/*
* HashMap Class Declaration
*/
class HashMap
{
private:
HashEntry **t;
public:
HashMap()
{
t = new HashEntry * [TABLE_LENGTH];
for (int i = 0; i< TABLE_LENGTH; i++)
{
t[i] = NULL;
}
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_LENGTH;
}
/*
* Insert Element at a key
*/
void Insert(int key, double value)
{
int h = HashFunc(key);
while (t[h] != NULL && t[h]->key != key)
{
h = HashFunc(h + 1);
}
if (t[h] != NULL)
delete t[h];
t[h] = new HashEntry(key, value);
}
/*
* Display Elements Of Map
*/
void PrintMap()
{
  
for(int j=0;j<20;j++)
{
cout<<"Key"<<t[j]->key<<"Value"<<t[j]->value<<endl;
}
}

};
/*
* Main functions
*/
int main()
{
HashMap hash;
int key;
double value;
int i=0;
  
cout<<"Enter 20 elements to be inserted in hash map"<<endl;
for(i=0;i<20;i++)
{
cout<<"Value:";
cin>>value;
cout<<"Key:";
cin>>key;
hash.Insert(key,value);
}
  
cout<<"The Hash Map Values are:"<<endl;
hash.PrintMap();

return 0;
}

===================================================================================

Please let me know in case any clarification is required.