subject

Need help to complete C++ assignment to write simple hash function : Instructions : This insert function should compute a good hash function of value. This hash function should return the least-significant three decimal digits (a number from 0 to 999) of the variable value. This hash should be used as an index into the thousand-element vector table that has been initialized with -1 in each element. If the element at this location of table is available (currently set to -1), you can replace the element with value. If this location is not available (currently set to some other value than -1) then you should check the next element, repeatedly, until you find an available element and can store value there. The insert() function should then return the number of times a location in the hash table was identified to store value but was not available.
Assignment :
#include
#include
#include
#include
#include
int insert(int value, std::vector &table) {
// Code to insert value into a hashed location in table
// where table is a vector of length 1000.
// Returns the number of collisions encountered when
// trying to insert value into table.
}
int main() {
int i, j, hit, max_hit = 0, max_value = -1;
std::vector value(500);
int old_value = 0;
for (i = 0; i < 500; i++) {
old_value += rand()%100;
value[i] = old_value;
}
// create hash table of size 1000 initialized with -1
std::vector table(1000,-1);
for (i = 0; i < 500; i++) {
hit = insert(value[i],table);
if (hit > max_hit) {
max_hit = hit;
max_value = value[i];
}
}
std::cout << "Hashing value " << max_value << " experienced " << max_hit << " collisions." << std::endl < for (j = 0; j < 1000; j += 10) {
std::cout << std::setw(3) << j << ":";
for (i = 0; i < 10; i++) {
if (table[j+i] == -1)
std::cout << " ";
else
std::cout << std::setw(6) << table[j+i];
}
std::cout << std::endl;
}
return 0;
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 20:00
Which type of file can be used to import data into a spreadsheet?
Answers: 1
question
Computers and Technology, 23.06.2019 03:00
State 7 common key's for every keyboard
Answers: 1
question
Computers and Technology, 23.06.2019 23:40
4. what is the reason for including the following code snippet in the header file animal.h? #ifndef animal_h #define animal_h class animal { public: animal(); animal(double new_area_hunt); void birth(); void hunt(double new_area_hunt); void death(); double get_area_hunt() const; private: double area_hunt; }; #endif
Answers: 3
question
Computers and Technology, 24.06.2019 00:10
Read each statement below. if the statement describes a peer-to-peer network, put a p next to it. if the statement describes a server-based network, put an s next to it. p - peer-to-peer s - server-based
Answers: 1
You know the right answer?
Need help to complete C++ assignment to write simple hash function : Instructions : This insert fun...
Questions
question
Biology, 01.04.2020 02:32
Questions on the website: 13722363