如何在Ocaml中将用户输入值添加到数组中

如何在Ocaml中将用户输入值添加到数组中,ocaml,Ocaml,我在OCaml中工作,必须解决以下问题 7.1 Problem 1 – Number of values less than average Input: An integer, listlen, followed by listlen number of integer values. Output: The list of values, number of items on the list, and the number of input values that are less tha

我在OCaml中工作,必须解决以下问题

7.1 Problem 1 – Number of values less than average
Input: An integer, listlen, followed by listlen number of integer values.
Output: The list of values, number of items on the list, and the number of input values that are less than the average of all the values
Sample input:
Enter the count and the corresponding integer values:
7 10 60 3 55 15 45 40
Corresponding output:
The list:
10 60 3 55 15 45 40
The average:
32.57
Number of values less than average:
3
其思想是使用用户输入,得到要平均的数字长度,得到要平均的数字,平均它们,然后告诉列表中哪些数字小于平均值。我的问题在于尝试将用户给定的值添加到数组中。我能够创建数组,但不知道使用什么代码来添加值

我目前拥有的代码

(* Prompts the user to enter the number of values they want to average
then gets that number and prints it *)
print_string "Enter The Number of Values You Want To Average:\n";;
let n_values = Scanf.scanf "%d" (fun n -> n);;
Printf.printf "%d\n" n_values;;

(* Prompts the user to enter the numbers they want averaged then
adds those values to an array and prints the numbers *)
print_string "Enter The Values To Be Averaged:\n";;
let a = Array.make n_values 0;;

for i = 0 to Array.length a - 1 do
    (*let values = Scanf.scanf "%d" (fun n -> n)*)
    a.(i) <- i
  done;;

for i = 0 to Array.length a - 1 do
    Printf.printf "%i" a.(i);
done;;

(* Adds each of the values of the array together, stores it in sum and then divides by the n_values initialized above and stores in average, then prints this value *)
print_string "\nThe Average:\n";;
let sum = Array.fold_left (+) 0 a;;
let average = sum / n_values;;
Printf.printf "%d\n" average;;

(* Checks which numbers in the array are less than the computed average and increments a counter if it is less*)
print_string "The Number of Values Less Than The Average:\n";;
let counter = ref 0;;
for i = 0 to Array.length a - 1 do
   if a.(i) < average then incr counter;
done;;
Printf.printf "%d\n" !counter;;
(*提示用户输入要平均的值的数量
然后获取该数字并打印它*)
print_string“输入要平均的值数:\n”;;
让n_values=Scanf.Scanf“%d”(乐趣n->n);;
Printf.Printf“%d\n”n_值;;
(*提示用户输入他们想要的数字
将这些值添加到数组并打印数字*)
打印字符串“输入要平均的值:\n”;;
设a=Array.make n_值为0;;
对于i=0到Array.length a-1 do
(*让value=Scanf.Scanf“%d”(乐趣n->n)*)
a、 (i)n)*)

a、 (i)如果您包括注释掉的代码,则您有:

let values = Scanf.scanf "%d" (fun n -> n)
a.(i) <- i
let values=Scanf.Scanf“%d”(乐趣n->n)

a、 (i)我的意思是,如果我使用注释掉的代码,我会执行
a.(i)所以我像这样运行代码
let a=Array.make n_values 0;;对于i=0到Array.length a-1,请在a.(i)中设置值=Scanf.Scanf“%d”(n->n)
let values = Scanf.scanf "%d" (fun n -> n)
a.(i) <- i