I am studying programming and in the school book it states that given key x, hashtable A[] and hash function h() the key x it is stored in A[h(x)-1] position (with C++ implementation). However, using as hash function the function h(x)=xmodM, where M is the length of the hashtable, I don't get where keys with mod 0 are stored. For instance, if M=10 and x=60, where I should store the key value? Thanks in advance!
Asked
Active
Viewed 228 times
0
-
1Does the book you're reading mentioning a hash functions like `h(x) = x mod M`? The book probably assumes that the hash function returns a value in the range 1 to M (inclusive). – Some programmer dude Sep 14 '16 at 11:36
-
Actually, it does! That's why I am confused. – gdaras Sep 14 '16 at 11:54
-
Maybe time to find some other book? :) – Some programmer dude Sep 14 '16 at 12:04
-
Haha, well yes, maybe it's time :D – gdaras Sep 14 '16 at 15:04
1 Answers
1
It depends on how h()
is defined, if it accepts values starting with 1, then this is why you have -1
in this formula: h(x)-1
. Arrays in c++ are indexed starting from 0.
If you compute reminder of dividing of 60 by 10 in c++, then you will get value 0
(60 % 10 = 0
). In such case it makes no sense to subtract -1.

marcinj
- 48,511
- 9
- 79
- 100
-
Thanks for the answer, I couldn't agree more. The thing is that the book is getting a bit confusing when it comes to that specific example and that's why I asked – gdaras Sep 14 '16 at 11:56