by Guenter Dotzel, ModulaWare
Two short, simple example programs illustrate the use of ISO Modula-2's array and record constructors:
The output of GenOut is as follows:
MODULE GenOut; (* Show variable parameter-list in Modula-2 using open arrays and array constructors written by Elmar Baumgart, ModulaWare, 29-May-1991 *) FROM InOut IMPORT WriteReal, Write, WriteLn, WriteString; TYPE String = ARRAY[0..500] OF CHAR; r1 = ARRAY[0..0] OF REAL; c1 = ARRAY[0..0] OF COMPLEX; r2 = ARRAY[0..1] OF REAL; c2 = ARRAY[0..1] OF COMPLEX; r3 = ARRAY[0..2] OF REAL; c3 = ARRAY[0..2] OF COMPLEX; s1 = ARRAY[0..0] OF String; s2 = ARRAY[0..1] OF String; s3 = ARRAY[0..2] OF String; PROCEDURE wr(x: ARRAY OF REAL); VAR i: INTEGER; BEGIN FOR i:= 0 TO HIGH(x) DO WriteReal(x[i], 0); WriteString(', '); END; WriteLn; END wr; PROCEDURE wc(x: ARRAY OF COMPLEX); VAR i: INTEGER; BEGIN FOR i:= 0 TO HIGH(x) DO Write('('); WriteReal(RE(x[i]), 0); WriteString(', '); WriteReal(IM(x[i]), 0); WriteString('), '); END; WriteLn; END wc; PROCEDURE ws(x: ARRAY OF String); VAR i: INTEGER; BEGIN FOR i:= 0 TO HIGH(x) DO WriteString(x[i]); WriteString(', '); END; WriteLn; END ws; CONST cpi = CMPLX(3.1415, 2.*3.1415); BEGIN ws (s1{ 'one real and one complex' }); wr (r1{ 3.1415 }); wc (c1{ CMPLX(3.1415, 2.*3.1415) }); ws (s2{ 'two reals', 'two complexs' }); wr (r2{ 3.1415, 3.1415 }); wc(c2{ cpi, CMPLX(2.,0.)*cpi }); wr (r3{ 3.1415, 3.1415, 3.1415 }); END GenOut.
one real and one complex, 3.141500 , ( 3.141500 , 6.283000 ), two reals, two complexs, 3.141500 , 3.141500 , ( 3.141500 , 6.283000 ), ( 6.283000 , 12.56600 ), 3.141500 , 3.141500 , 3.141500 ,
| 2 | 7 | 6 |
| 9 | 5 | 1 |
| 4 | 3 | 8 |
That's easily verified, but are the following nine consecutive primes magic? (Are they really consecutive primes? If you don't believe, find out.)
| 1480028201 | 1480028129 | 1480028183 |
| 1480028153 | 1480028171 | 1480028189 |
| 1480028159 | 1480028213 | 1480028141 |
The following output free program checks whether some 3*3 squares are magic; if they are not magic, it terminates with a forced run-time error (halt statement); The program was written 11 years ago. I believe I found some or all numbers used below in a Scientific American article.
MODULE Magic; (* Show array and record constructor features of MVR written by Guenter Dotzel, ModulaWare, 07-Jun-1990 *) TYPE C = RECORD x,y,z: INTEGER END; T = ARRAY [0..2] OF C; VAR r,c: C; t: T; PROCEDURE AddRowColD(p: T); TYPE CA = ARRAY [0..7] OF LONGREAL; PROCEDURE Magic (a: ARRAY OF LONGREAL): BOOLEAN; VAR i: INTEGER; BEGIN FOR i:= 1 TO VAL(INTEGER, HIGH(a)) DO IF a[0] <> a[i] THEN RETURN FALSE END; END; RETURN TRUE; END Magic; BEGIN (* a square is defined to be magic if the sum of all rows, columns and diagonales add up to the same value *) IF NOT Magic(CA{ LFLOAT(p[0].x) + LFLOAT(p[1].x) + LFLOAT(p[2].x), (* three rows *) LFLOAT(p[0].y) + LFLOAT(p[1].y) + LFLOAT(p[2].y), LFLOAT(p[0].z) + LFLOAT(p[1].z) + LFLOAT(p[2].z), LFLOAT(p[0].x) + LFLOAT(p[0].y) + LFLOAT(p[0].z), (* three columns *) LFLOAT(p[1].x) + LFLOAT(p[1].y) + LFLOAT(p[1].z), LFLOAT(p[2].x) + LFLOAT(p[2].y) + LFLOAT(p[2].z), LFLOAT(p[0].x) + LFLOAT(p[1].y) + LFLOAT(p[2].z), (* two diagonales *) LFLOAT(p[2].x) + LFLOAT(p[1].y) + LFLOAT(p[0].z)}) THEN HALT END; END AddRowColD; BEGIN (* The "lo shu" magic square: *) AddRowColD(T{ {2,7,6}, {9,5,1}, {4,3,8}}); (* Lee Sallows' "li shu" magic square: *) AddRowColD(T{ { 5,22,18}, {28,15, 2}, {12, 8,25}}); (* "li shu's" alphamagic partner square (alphamagic in respect to English: "five" has 4 letters, "Twentytwo" has 9 letters and so on) is magic too? *) AddRowColD(T{ {4, 9, 8}, {11,7, 3}, { 6,5,10}}); (* Now test "li shu's" "alphamagablitity": *) AddRowColD(T{ {LENGTH("five"), LENGTH("twentytwo"), LENGTH("eighteen")}, {LENGTH("twentyeight"), LENGTH("fifteen"), LENGTH("two")}, {LENGTH("twelve"), LENGTH("eight"), LENGTH("twentyfive")}}); (* The following is a square with 9 consecutive primes (if you don't believe that they are all prime numbers, then find out). Is it magic too? *) AddRowColD(T{ {1480028201,1480028129,1480028183}, {1480028153,1480028171,1480028189}, {1480028159,1480028213,1480028141}}); (* This output free program shows that the 3*3 squares are magic if it terminates normal successfully otherwise it terminates with an error (halt statement). *) END Magic.
Most of the ModulaTor back-issues are available from http://www.modulaware.com/
© (2001) by modulaware.com
Webdesign by www.otolo.com/webworx,
Created 11-May-2001, last revised 11-May-2001