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

Need help with adding comments to the completed C++ code. On just about every li

ID: 3640724 • Letter: N

Question

Need help with adding comments to the completed C++ code. On just about every line so that I can understand it or anyone else who reads this code can follow the comments.
Just treat it as I don't know what any line means even simple variable declaration.
comments doesn't have to be long but rather understandable.


I will give helpful if you give full comments everywhere.


/ * There are 101 buckets (a vector of 0 to 100 list<Item<T>>).
* Each Item is a pair of key and value.
* ************************************************************/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
#include <list>

using namespace std;

template <class T> class Hash_table; //forward decl

template <class T>
class Item {
private:
Item(const string & k, const T & v): key(k), value(v) {}
string key;
T value;
friend class Hash_table<T>;
};

template < class T>
class Hash_table {
public:
Hash_table(int n): size(n), bucket(vector<list<Item<T> > >(n)) {}
bool lookup(const string & k, T & v);
void insert(const string & k, const T & v);
void print();
private:
vector<list<Item<T> > > bucket;
int size;
int hash_function(const string & s) { return (s[0]+s[1])%size; }
};

template < class T>
void Hash_table<T>::print()
{
for (int i = 0 ; i < size; i++)
if (!bucket[i].empty()) {
cout << i << ": ";
typename list<Item<T> >::iterator j;
for (j = bucket[i].begin(); j != bucket[i].end(); j++)
cout << "(" << j->key << "," << j->value << ") ";
cout << endl;
}
}

// returns status: true means found it, false otherwise
template < class T>
bool Hash_table<T>::lookup(const string & k, T & v)
{
int index = hash_function(k);
typename list<Item<T> >::iterator i;
for (i = bucket[index].begin(); i != bucket[index].end(); i++)
if (i -> key == k) {
v = i -> value;
return true;
}
return false;
}

template < class T>
void Hash_table<T>::insert(const string & k, const T & v)
{
int index = hash_function(k);
bucket[index].push_front(Item<T>(k, v));
}

main()
{
string name;
int age;
ifstream in("hash.in");

if (!in.is_open()) {
cout << "couldn't open 'hash.in' for input. ";
exit(1);
}

// 101 buckets of list<(string, int)>
Hash_table<int> h(101);

while (in >> name >> age)
h.insert(name, age);
in.close();

h.print();

cout << "lookup what? ";
while (cin >> name) {
if (h.lookup(name, age))
cout << "found: " << name << "," << age << endl;
else cout << name << " doesn't exist ";
cout << "lookup what? ";
}
}

Explanation / Answer

/ * There are 101 buckets (a vector of 0 to 100 list). * Each Item is a pair of key and value. * ************************************************************/ //Including all the libraries as in your previous example #include #include //io manipulations - setting base for integer etc. #include //This header defines several general purpose functions, including dynamic memory //management, random number generation, #include //string lib #include //vector and list are part of STL - define collections: these are like unbounded arrays #include #include using namespace std; //Define sth here, so that you can use it in the code later: in C++ everything must be //predefined to be used template class Hash_table; //forward decl //template class: accepting any type T as argument. Really it describes a pair, whose //first arg is string and value is any object template class Item { //private constructors and properties private: //string k and T v are constants -> won't change in the program Item(const string & k, const T & v): key(k), value(v) {} string key; T value; //hashTable can access private elements: declared as a friend friend class Hash_table; }; template class Hash_table { public: //Definition of the HashTable: it really is a vector (array) where at each index, you //find another list. So it is a bit like array of arrays. This is bucket variable Hash_table(int n): size(n), bucket(vector(n)) {} //dfinition of methods to be implemented later: lookup of a key, insertion etc. bool lookup(const string & k, T & v); void insert(const string & k, const T & v); void print(); //private instance variables private: vector bucket; int size; //IMPORTANT: Hash function: given the string, what bucket (array index) it falls into? // Here it is the value of first and second character mod size of array int hash_function(const string & s) { return (s[0]+s[1])%size; } }; //printing method template void Hash_table::print() { //iterate through buckets for (int i = 0 ; i list of lists as mentioned if (!bucket[i].empty()) { cout