This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,45 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. Nested-Loop.
DATA DIVISION.
LOCAL-STORAGE SECTION.
78 Table-Size VALUE 10.
01 Table-Area.
03 Table-Row OCCURS Table-Size TIMES
INDEXED BY Row-Index.
05 Table-Element PIC 99 OCCURS Table-Size TIMES
INDEXED BY Col-Index.
01 Current-Time PIC 9(8).
PROCEDURE DIVISION.
* *> Seed RANDOM.
ACCEPT Current-Time FROM TIME
MOVE FUNCTION RANDOM(Current-Time) TO Current-Time
* *> Put random numbers in the table.
* *> The AFTER clause is equivalent to a nested PERFORM VARYING
* *> statement.
PERFORM VARYING Row-Index FROM 1 BY 1
UNTIL Table-Size < Row-Index
AFTER Col-Index FROM 1 BY 1
UNTIL Table-Size < Col-Index
COMPUTE Table-Element (Row-Index, Col-Index) =
FUNCTION MOD((FUNCTION RANDOM * 1000), 20) + 1
END-PERFORM
* *> Search through table for 20.
* *> Using proper nested loops.
PERFORM VARYING Row-Index FROM 1 BY 1
UNTIL Table-Size < Row-Index
PERFORM VARYING Col-Index FROM 1 BY 1
UNTIL Table-Size < Col-Index
IF Table-Element (Row-Index, Col-Index) = 20
EXIT PERFORM
ELSE
DISPLAY Table-Element (Row-Index, Col-Index)
END-IF
END-PERFORM
END-PERFORM
GOBACK
.

View file

@ -0,0 +1,16 @@
use Random;
var nums:[1..10, 1..10] int;
var rnd = new RandomStream();
[ n in nums ] n = floor(rnd.getNext() * 21):int;
delete rnd;
// this shows a clumsy explicit way of iterating, to actually create nested loops:
label outer for i in nums.domain.dim(1) {
for j in nums.domain.dim(2) {
write(" ", nums(i,j));
if nums(i,j) == 20 then break outer;
}
writeln();
}

View file

@ -0,0 +1,21 @@
-module( loops_nested ).
-export( [task/0] ).
task() ->
Size = 20,
Two_dimensional_array = [random_array(Size) || _X <- lists:seq(1, Size)],
print_until_found( [], 20, Two_dimensional_array ).
print_until_found( [], N, [Row | T] ) -> print_until_found( print_until_found_row(N, Row), N, T );
print_until_found( _Found, _N, _Two_dimensional_array ) -> io:fwrite( "~n" ).
print_until_found_row( _N, [] ) -> [];
print_until_found_row( N, [N | T] ) -> [N | T];
print_until_found_row( N, [H | T] ) ->
io:fwrite( "~p ", [H] ),
print_until_found_row( N, T ).
random_array( Size ) -> [random:uniform(Size) || _X <- lists:seq(1, Size)].

View file

@ -0,0 +1,24 @@
import math, strutils
const arrSize = 10
var a: array[0..arrSize-1, array[0..arrSize-1, int]]
var s: string = ""
randomize() # different results each time this runs
for i in 0 .. arrSize-1:
for j in countup(0,arrSize-1):
a[i][j] = random(20)+1
block outer:
for i in countup(0,arrSize-1):
for j in 0 .. arrSize-1:
if a[i][j] < 10:
s.add(" ")
addf(s,"$#",$a[i][j])
if a[i][j] == 20:
break outer
s.add(", ")
s.add("\n")
echo(s)