[prev] 22 [next]

Exercise 2: Key-to-index Mapping

Define a function which
  • takes a key value in the range lo..hi
  • returns an index value into array of size hi-lo+1
  • aborts if the "key" value is outside the range lo..hi
E.g. lo == 12, hi == 27, a[16]

indexOf(12) == 0, indexOf(17) = 5,

indexOf(21) == 9, indexOf(27) == 15