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


s = 1	/* Seed of the random number generator */
 
/* Random number from 1 to 20. */
define r() {
	auto r
	while (1) {
		/*
		 * Formula (from POSIX) for random numbers of low
		 * quality, from 0 to 32767.
		 */
		s = (s * 1103515245 + 12345) % 4294967296
		r = (s / 65536) % 32768
 
		/* Prevent modulo bias. */
		if (r >= 32768 % 20) break
	}
	return ((r % 20) + 1)
}
 
r = 5	/* Total rows */
c = 5	/* Total columns */
 
/* Fill array a[] with random numbers from 1 to 20. */
for (i = 0; i < r; i++) {
	for (j = 0; j < c; j++) {
		a[i * c + j] = r()
	}
}
 
/* Find a 20. */
b = 0
for (i = 0; i < r; i++) {
	for (j = 0; j < c; j++) {
		v = a[i * c + j]
		v	/* Print v and a newline. */
		if (v == 20) {
			b = 1
			break
		}
	}
	if (b) break
	/* Print "==" and a newline. */
	"==
"
}
quit