C++
Based upon the information provided on Hashing and Hash tables,
create a Hash Table to store the data in the provided file which is
a list of colors,ColorList.txt
Using the basic algorithm that adds the ASCII values of each letter in the color and then mods by your chosen number of bins. Determine the best size (number of bins) for your hash table that is close to Balanced with an average list length between 12-15 (NOTE: list lengths of 0 do not count in the average).
Your code should do the average list length calculations for you
and provide the recommendation for the best array size.
ColorList.txt
Air Force blue Alice blue Alizarin crimson Almond Amaranth Amber
American rose Amethyst Android Green Anti-flash white Antique brass
Antique fuchsia Antique white Apple green Apricot Aqua Aquamarine
Army green Arylide yellow Ash grey Asparagus Atomic tangerine
Auburn Aureolin Auro Metal Saurus Awesome Azure Azure mist/web Baby
blue Baby blue eyes Baby pink Ball Blue Banana Mania Banana yellow
Battleship grey Bazaar Beau blue Beaver Beige Bisque Bistre
Bittersweet Black Blanched Almond Bleu de France Blizzard Blue
Blond Blue Blue Bell Blue Gray Blue green Blue purple Blue violet
Blush
That's all the information I have on the assignment.
From what I understand. I need to.
load the text file in the program.
Convert each of those words into a ASCII value.
Then once that value has been converted have that value placed in a
bin.
The bins should be between 12-15 on average length.
Structure of the answer
The load factor is a measure used to determine the trade-off between time and space costs.
A higher load factor value decreases the space requirements and increases the odds of a collision. A collision increases the amount of time needed to insert and retrieve the value in a hashtable The Ideal is O(1).
A lower load factor value increases memory requirements but decreases the odds of a collision.
In java, with class HashTable, the default load factor (.75) offers a good tradeoff between time and space costs.
A higher load factor value decreases the space requirements and increases the odds of a collision. A collision increases the amount of time needed to perform a get() and put(...).
A lower load factor value increases disk/memory space requirements, causing lots of reserved space that is permanently unused. The increased number of bins decreases the odds of a collision.
So a load factor of (.75) means the HashTable bins are 75% full. If you have 75 elements to store, the number of bins in your HashTable should be 100. Therefore given N the number of items in your case the colour in the .txt file, the size of hashtable should be 1.33 * N
Note: Since the colours were provided in a single line I had to manually google the name of each colour becz. some colours have more than 1 words in their names. In doing this I found that the total no. of colours was 48. Don't worry if that's not the actual number the programme should programmatically calculate the best no of bins.
In your case N = 48. (the one I obtained)
and Size = 1.33 * N => 64 (round it)
The load factor = 48 / 64 = 0.75 or 75% which is good
Code:
/* To calculate the best array size (no. of bins ) we use load factor The best way to calculate load factors is if n = 'no. of elements to store' then no. of bins = 1.33 * n */ #include <iostream> #include <fstream> #include<math.h> using namespace std; int hash_function(string color){ int hashvalue = 0; for(int i =0;i<color.length();i++) { if(color[i] == ' ') continue; hashvalue += color[i] ; } return hashvalue; } int main() { string color; // Read from the text file ifstream MyReadFile("ColorFile.txt"); int counter=0; // Use a while loop together with the getline() function to read the file line by line while(getline (MyReadFile, color)) { counter++; } MyReadFile.close(); int best_bin_size = round(1.33 * counter); cout<< "No . of Colors = "<<counter << "\n"; cout<< "best bin size = "<< best_bin_size<<"\n"; string HashTable[best_bin_size]; ifstream MyReadFile2("ColorFile.txt"); while(getline (MyReadFile2, color)) { int index = hash_function(color) % best_bin_size; //uncommnet this to check for any collisions; //cout<< index<<" "<<color<<"\n"; HashTable[index] = color; } MyReadFile2.close(); return 0; }
The Colorlist.txt file I Used.
Output: You can comment out the line (read the comments) in the 2nd while loop if you don't want the index and colour printing per line but this helps is detecting any collisions
C++ Based upon the information provided on Hashing and Hash tables, create a Hash Table to...