落書きノート

ふと自分が気になった事を書いてます

OCamlで不定期練習

open Printf;;
let a = Array.make 5 0;;

for i = 0 to 4 do
  a.(i) <- 5 - i;
done;;

let rec int_array i xs = match xs with
    [] -> ()
  | x :: xs -> printf "a[%d] = %d\n" i x;
               int_array (i + 1) xs;;

int_array 0 (Array.to_list a);;



open Printf;;
print_string "要素数 : ";;
let n = read_int ()
let a = Array.make n 0;;
for i = 0 to n - 1 do
  printf "a[%d] = " i;
  a.(i) <- read_int ();
done;;
let rec show i xs = match xs with
    [] -> print_string ")\n";
  | x :: xs -> printf "%d " x;
               show (i + 1) xs;;
print_string "( ";;
show 0 (Array.to_list a);;




open Printf;;
print_string "人数 : ";;
let n = read_int ();;
print_string "点数を入力せよ。\n";;
let array = Array.make n 0.0;;
let sum = ref 0.0;;

for i = 0 to n - 1 do
  printf "%d番の点数 : " (i + 1);
  array.(i) <- read_float ();
  sum := !sum +. array.(i);
done;;

let max = ref array.(0);;
let min = ref array.(0);;

for j = 0 to n - 1 do
  if !max < array.(j) then
    max := array.(j)
  else
    ();
  if !min > array.(j) then
    min := array.(j)
  else
    ();
done;;

let main () = printf "合計点は%.0f点です。\n" !sum;
              printf "平均点は%.1f点です。\n" (!sum /. float_of_int n);
              printf "最高点は%.0f点です。\n" !max;
              printf "最低点は%.0f点です。\n" !min;;

main ();;