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 09:00
What is one way in which tablets differ from laptops and notebooks? tablets are designed for touch-based interaction. tablets are designed to be used as desktops. tablets are designed for input via a keyboard and mouse. tablets are designed to be larger than laptops.
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
Plz ( which is an example of a good url?
Answers: 1
question
Computers and Technology, 23.06.2019 19:40
Use a physical stopwatch to record the length of time it takes to run the program. calculate the difference obtained by calls to the method system.currenttimemillis() just before the start of the algorithm and just after the end of the algorithm. calculate the difference obtained by calls to the method system.currenttimemillis() at the start of the program and at the end of the program so that the elapsed time includes the display of the result. use the value returned by the method system.currenttimemillis() just after the end of the algorithm as the elapsed time.
Answers: 3
question
Computers and Technology, 24.06.2019 16:00
Which of the following characters is acceptable to use in a filename? ? / – %
Answers: 1
You know the right answer?
Need help to complete C++ assignment to write simple hash function : Instructions : This insert fun...
Questions
question
Social Studies, 07.05.2020 09:59
question
Mathematics, 07.05.2020 09:59
Questions on the website: 13722361