Re: Advice on a program.
"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]> Sun, 14 Feb 2016 22:09:44 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAPFanBGe5D664GyRDcb0soY5B3m-x2LpPSio9QBg5YuXwdTC_g@mail.gmail.com> |
First:
let createMatrix i j initial =
let matrix = Array.make i ( Array.make j initial ) in
for n = 0 to i - 1 do
matrix.(n) <- Array.make j initial ;
done ;
matrix ;;
could be written instead
let create_matrix i j initial =
Array.init i (fun _ -> Array.make j initial);;
Second, I think you could solve your issue by explicitly flushing the
output buffer. Instead of
printf "%5i" arr.(i).(j) ;
you could write
printf "%5i" arr.(i).(j) ; flush stdout;
or, equivalently
printf "%5i%!" arr.(i).(j) ;
On Sun, Feb 14, 2016 at 9:18 PM, Douglas Lewit [email protected]
[ocaml_beginners] <[email protected]> wrote:
>
>
> Hi everyone,
>
> I need advice on a program. I want the pause ( Thread.delay <float> ) after
> each number, but instead I'm getting the pause after each line.
>
> What am I doing wrong?
>
> Thanks!
>
> Best,
>
> Douglas.
>
> (* Ocaml program to generate a simple multiplication table or table of
> products.
>
> * A great example of imperative programming in Ocaml. *)
>
>
> open Printf ;;
>
>
> print_string "Enter a positive integer: " ;;
>
>
> let n = read_int ( ) ;;
>
>
> let createMatrix i j initial = let matrix = Array.make i ( Array.make j
> initial ) in
>
> for n = 0 to i - 1 do
>
> matrix.(n) <- Array.make j initial ;
>
> done ;
>
> matrix ;;
>
>
> let arr = createMatrix n n 0 ;; (* Initialize the 2-dimensional array
> needed to solve this problem. *)
>
>
> for j = 0 to n - 1 do
>
> arr.(0).(j) <- j + 1;
>
> done ;;
>
>
> for i = 0 to n - 1 do
>
> arr.(i).(0) <- i + 1;
>
> done ;;
>
>
> for i = 1 to n - 1 do
>
> for j = 1 to n - 1 do
>
> arr.(i).(j) <- arr.(i).(0) * arr.(0).(j) ;
>
> done ;
>
> done ;;
>
>
> print_newline ( ) ;;
>
>
> for i = 0 to n - 1 do
>
> for j = 0 to n - 1 do
>
> printf "%5i" arr.(i).(j) ;
>
> Thread.delay 0.012 ;
>
> done ;
>
> print_newline ( ) ;
>
> done ;;
>
>
> print_newline ( ) ;;
>
>
>
>
>
>