I have built a function in which the user will insert the array size and then proceed to insert its values
let array_builder =
let coin = Array.make (10000) 0 in
let number_coins = read_int() in
for i = 0 to (number_coins-1) do
let k = read_int() in
coin.(i) <- (k);
done;
The input looks something like this:
>3 ---> Array size
>10 ---> coin.(0)
>9 ---> coin.(1)
>8 ---> coin.(2)
Is there anyway to pass the array, coin
, as an argument to another function?
In OCaml array are regular value and can be passed to other function without specific syntax.
Maybe you want an example. I have created a dummy function that do like your loop.
let rec array_filler coin_array current_index size =
if current_index < size then
let k = read_int() in
let () = coin_array.(current_index) <- k in
array_filler coin_array (current_index + 1) size
else
coin_array
let array_builder =
let number_coins = read_int() in
let coin = Array.make number_coins 0 in
array_filler coin 0 number_coins
I also switched 2 lines so number_coins
is available when creating the array (to not reserve more cell if the user don't need more than 10000)