2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,6 @@
|
|||
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over <math>[1,\ldots,20]</math>.
|
||||
|
||||
The loops iterate rows and columns of the array printing the elements until the value <math>20</math> is met.
|
||||
|
||||
Specifically, this task also shows how to [[Loop/Break|break]] out of nested loops.
|
||||
<br><br>
|
||||
|
|
|
|||
24
Task/Loops-Nested/C++/loops-nested-2.cpp
Normal file
24
Task/Loops-Nested/C++/loops-nested-2.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include<cstdlib>
|
||||
#include<ctime>
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int arr[10][10];
|
||||
srand(time(NULL));
|
||||
for(auto& row: arr)
|
||||
for(auto& col: row)
|
||||
col = rand() % 20 + 1;
|
||||
|
||||
for(auto& row : arr) {
|
||||
for(auto& col: row) {
|
||||
cout << ' ' << col;
|
||||
if (col == 20) goto out;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
out:
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
defmodule Loops do
|
||||
def nested do
|
||||
:random.seed(:os.timestamp)
|
||||
list = Enum.shuffle(1..20) |> Enum.chunk(5)
|
||||
IO.inspect list, char_lists: :as_lists
|
||||
try do
|
||||
10
Task/Loops-Nested/Elixir/loops-nested-2.elixir
Normal file
10
Task/Loops-Nested/Elixir/loops-nested-2.elixir
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
list = Enum.shuffle(1..20) |> Enum.chunk(5)
|
||||
IO.inspect list, char_lists: :as_lists
|
||||
Enum.any?(list, fn row ->
|
||||
IO.puts ""
|
||||
Enum.any?(row, fn x ->
|
||||
IO.write "#{x} "
|
||||
x == 20
|
||||
end)
|
||||
end)
|
||||
IO.puts "done"
|
||||
19
Task/Loops-Nested/Kotlin/loops-nested.kotlin
Normal file
19
Task/Loops-Nested/Kotlin/loops-nested.kotlin
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import java.util.Random
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val r = Random()
|
||||
val a = Array(10) { IntArray(10) { r.nextInt(20) + 1 } }
|
||||
println("array:")
|
||||
for (i in a.indices) println("row $i: " + a[i].asList())
|
||||
|
||||
println("search:")
|
||||
Outer@ for (i in a.indices) {
|
||||
print("row $i: ")
|
||||
for (j in a[i].indices) {
|
||||
print(" " + a[i][j])
|
||||
if (a[i][j] == 20) break@Outer
|
||||
}
|
||||
println()
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
|
@ -1,22 +1,22 @@
|
|||
/*REXX program loops through a 2-dimensional array to look for a '20'. */
|
||||
parse arg rows cols targ . /*obtain optional args from C.L. */
|
||||
if rows=='' | rows==',' then rows=60 /*Rows not specified? Use default*/
|
||||
if cols=='' | cols==',' then cols=10 /*Cols " " " " */
|
||||
if targ=='' | targ==',' then targ=20 /*Targ " " " " */
|
||||
w=max(length(rows),length(cols),length(targ)) /*for formatting output.*/
|
||||
not='not' /* [↓] construct the 2-dim array.*/
|
||||
do row=1 for rows /*1st dimension of the array. */
|
||||
do col=1 for cols /*2nd " " " " */
|
||||
@.row.col=random(1,targ) /*generate some random numbers. */
|
||||
/*REXX program loops through a two-dimensional array to search for a '20' (twenty). */
|
||||
parse arg rows cols targ . /*obtain optional arguments from the CL*/
|
||||
if rows=='' | rows=="," then rows=60 /*Rows not specified? Then use default*/
|
||||
if cols=='' | cols=="," then cols=10 /*Cols " " " " " */
|
||||
if targ=='' | targ=="," then targ=20 /*Targ " " " " " */
|
||||
w=max(length(rows), length(cols), length(targ)) /*W: used for formatting the output. */
|
||||
not= 'not' /* [↓] construct the 2─dimension array*/
|
||||
do row=1 for rows /*ROW is the 1st dimension of array. */
|
||||
do col=1 for cols /*COL " " 2nd " " " */
|
||||
@.row.col=random(1, targ) /*create some positive random integers.*/
|
||||
end /*row*/
|
||||
end /*col*/
|
||||
/*═════════════════════════════════════now, search for the target {20}.*/
|
||||
do r=1 for rows
|
||||
|
||||
do r=1 for rows /* ◄───────────────── now, search for the target {20}.*/
|
||||
do c=1 for cols
|
||||
say left('@.'r"."c,3+w+w) '=' right(@.r.c,w) /*display.*/
|
||||
if @.r.c==targ then do; not=; leave r; end /*found ? */
|
||||
say left('@.'r"."c, 3+w+w) '=' right(@.r.c, w) /*show an array element.*/
|
||||
if @.r.c==targ then do; not=; leave r; end /*found the targ number?*/
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
|
||||
say right(space('Target' not 'found:') targ, 33, '─')
|
||||
/*stick a fork in it, we're done.*/
|
||||
say right( space( 'Target' not "found:" ) targ, 33, '─')
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
slices = (1..20).to_a.shuffle.each_slice(4)
|
||||
p slices = [*1..20].shuffle.each_slice(4)
|
||||
|
||||
slices.any? do |slice|
|
||||
puts
|
||||
slice.any? do |element|
|
||||
puts element
|
||||
print "#{element} "
|
||||
element == 20
|
||||
end
|
||||
end
|
||||
puts "done"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue