Advertisement
Guest User

min index

a guest
Feb 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.79 KB | None | 0 0
  1. (* FINDING THE MINIMUM  (20 points possible)
  2.  * Consider a non empty array of integers a.
  3.  *
  4.  * Write a function min : int array -> int that returns the minimal element of a.
  5.  * Write a function min_index : int array -> int that returns the index of the minimal element of a.
  6.  * Do you think these functions work well on large arrays ?
  7.  *
  8.  * Define a variable it_scales and set it to "yes" or "no". *)
  9.  
  10. let min a : int =
  11.   Array.fold_left (fun initial accumulator ->
  12.       if accumulator < initial
  13.       then accumulator
  14.       else initial) a.(0) a
  15. ;;
  16.  
  17. let rec find_index array value index : int =
  18.   if array.(index) = value
  19.   then
  20.     index
  21.   else
  22.     find_index array value (index - 1)
  23. ;;
  24.  
  25. let min_index a : int =
  26.   find_index a (min a) (Array.length a - 1)
  27. ;;
  28.  
  29. let it_scales = "no";;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement