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


 
int main()
{
    // enumerate() normally returns a linearly enumerated array, but
    // allows for the forth argument to specify a function that will
    // be called and return the value that should be in each cell. We
    // create an anonymous function (lambda) that just returns a
    // random value.
    array a = ({});
    for(int i=0; i<20; i++)
        a += ({ enumerate( 20, 1, 1, lambda(){return random(20)+1;} ) });
 
    // We could use for() and a[x][y] indexing, but foreach is just
    // shorter and easier to use even if the 2D-array becomes less
    // obvious.
 mynestedloops:
    foreach(a, array inner_a) {
        foreach(inner_a, int value) {
            write(value +" ");
            if(value == 20)
                break mynestedloops;
        }
    }
    write("\n");
}
 