Week 7 Short on Hash Table provides the following suggested hash function:
int hash_function (char* key)
{
// hash on first letter of string
int hash = toupper(key[0]) - 'A' ;
return hash % SIZE;
}
The first line of the function returns an integer between 26 and 0, because you are subtracting 'A' or 65 from a ASCII value between 90 (for Z) and 65 (for A).
The next line says to return the remainder of the "hash" value (0 to 26) divided by SIZE
.
SIZE
is the size of the hash table. The CS50 study site says: "Modding by the size of the hash table is a good way to avoid indexing into a hash table slot that does not exist." if your hash table is 500 in size. What does dividing a hash value of 0 to 26 by 500 do for you?