1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
using namespace std;
class hInt
{
public:
unsigned int operator() (int item) const
{
unsigned int value = (unsigned int) item;
value *= value; // square the item;
value /= 256; // discard the low order 8 bits
return value % 65536; // return item in range 0 to 65535;
}
};
void main()
{
// declare array with all the values
int arr[] = {47, 89, 90, 126, 140, 145, 153, 177, 285, 393, 395, 467, 566, 601, 735}, i;
hInt key;
cout << "table with 20 positions" << endl;
cout << "key\thash\tindex\toffset" << endl;
for (i = 0; i < 15; i++)
{
// print array value, hash value, hash value mod table size, hash value divided by table size
cout << /* array value */ << '\t'
<< /* hash value */ << '\t'
<< /* hash value mod table size */ << '\t'
<< /* hash value / table size */ << endl;
}
cout << "table with 10 positions" << endl;
cout << "key\thash\tindex" << endl;
for (i = 0; i < 15; i++)
{
// print array value, hash value, hash value mod table size
cout << /* array value */ << '\t'
<< /* hash value */ << '\t'
<< /* hash value mod table size */ << endl;
}
}
|