Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over 



[
1
,
…
,
20
]


{\displaystyle [1,\ldots ,20]}

.

The loops iterate rows and columns of the array printing the elements until the value 



20


{\displaystyle 20}

 is met.

Specifically, this task also shows how to break out of nested loops.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges


$ ocaml
 
# Random.self_init();;
- : unit = ()
 
# let m = Array.make_matrix 10 10 0 ;;
val m : int array array =
  [|[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
    [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
    [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
    [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
    [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]|]
 
# for i = 0 to pred 10 do
    for j = 0 to pred 10 do
      m.(i).(j) <- 1 + Random.int 20
    done;
  done;;
- : unit = ()
 
# try
    for i = 0 to pred 10 do
      for j = 0 to pred 10 do
        Printf.printf " %d" m.(i).(j);
        if m.(i).(j) = 20 then raise Exit;
      done;
      print_newline()
    done;
  with Exit ->
    print_newline()
  ;;
 15 8 15 9 9 6 1 18 6 18
 17 1 13 15 13 1 16 4 13 9
 15 3 5 19 17 3 1 11 5 2
 1 1 6 19 20
- : unit = ()