ProgrammingVery hard

Which expression always gives a valid bucket index in the range 0 to m - 1 for a signed int key k, with m > 0?

  1. A. k % m
  2. B. ((k % m) + m) % m
  3. C. (k % m) + m
  4. D. k / (m + 1)

Correct answer: B. ((k % m) + m) % m

C99 % keeps the sign of the dividend, so k % m can be negative for negative k. Adding m and taking % again gives a value in range.

More C Programming questions

Exams that ask this topic