Магический квадрат

Download Report

Transcript Магический квадрат

Tver lyceum
Practical work on Computer
Technology
Made by pupils of the 112 form:
Kadushkina Maria
Krotkov Eugeny
Kuchinskaya Svetlana
Litvinov Alexander
Ratkevich Leonid
Samojlenko Julia
Examened by:
Naumova A. I.
Magic Square
(Made on Turbo Pascal
programming language)
The task:
Define, if entered square matrix is a
magic square.
Magic square is a square matrix,
which has equival sums in all strings,
columns and diagonals.
Examples for testing the
program:
1.
2.
3.
4.
5.
Example
Example
Example
Example
Example
1: 1 8 1 8 1 8 1 1 1
2: 1 8 1 8 1 1 1 8 1
3: 1 1 8 8 1 1 1 8 1
4: 1 8 1 8 1 1 1 1 8
5: 4 3 8 9 5 1 2 7 6
Examples for testing the
program:
6.
Example 6: 31 8 22 15 30 5 20 11 24 16 28 12 17
26 13 21 9 25 34 1 23 14 35 4 7 32 10
27 6 29 2 33 19 18 3 36
7.
Example 7: 16 81 79 77 75 11 13 15 2 78 28 65
63 61 25 27 18 4 76 62 36 53 51 35
30 20 6 74 60 50 40 45 38 32 22 8 9
23 33 39 41 43 49 59 73 10 24 34 44
37 42 48 58 72 12 26 52 29 31 47 46
56 70 14 64 17 19 21 57 55 54 68 80
1 3 5 7 71 69 67 66
PROGRAM MATRIX;
USES CRT;
{ USES }
LABEL M, M1;
VAR
A: ARRAY [1..10, 1..10] OF INTEGER;
I, J, N, S, S1 : INTEGER;
BEGIN
{ FILLING THE MATRIX }
REPEAT
{ REPEAT-UNTIL }
CLRSCR;
{ CLEAR SCREEN }
WRITELN ('DIMENSIONS N= (N<10)')
READ (N);
WRITELN ('FILLING MAGIC SQUARE');
FOR I:=1 TO N DO
BEGIN
FOR J:=1 TO N DO READ(A [I,J]);
END;
S:=0;
{ SUM OF THE 1-ST STRING }
FOR J:=1 TO N DO S:=S+A[I,J];
S1:=0;
{ SUM OF ALLSTRINGS }
FOR I:=2 TO N DO
BEGIN
FOR J:=1 TO N DO
BEGIN
S1:=S1+A[I,J];
END;
IF S<>S1 THEN GOTO M1 ELSE S1:=0;
END;
FOR I:=1 TO N DO
{ SUM OF COLUMNS }
BEGIN
FOR J:=1 TO N DO
BEGIN
S1:=S1+A[I,J];
END;
IF S<>S1 THEN GOTO M1 ELSE S1:=0;
END;
J:=1;
{ 1-ST DIAGONAL }
FOR I:=1 TO N DO
BEGIN
S1:=S1+A[I,J];
J:=J+1;
END;
IF S1<>S THEN GOTO M1 ELSE S1:=0;
J:=1;
{ 2-ND DIAGONAL }
FOR I:=N DOWNTO 1 DO
BEGIN
S1:=S1+A[I,J];
J:=J+1;
END;
IF S<>S1 THEN GOTO M1 ELSE
WRITELN ('MAGIC SQUARE!');
GOTO M;
M1: WRITELN ('NO MAGIC SQUARE');
M: WRITELN ('REPEAT? YES-Y,NO-ENTER ');
UNTIL READKEY <>'Y';
END.
Example 1
(Error in the string)
1
8
1
8
1
8
1
1
1
Testing the program:
Example 1:
Dimensions N=(N<10)
3
Filling magic square
181818111
No magic square
Repeat? Yes-y, No-Enter
Example 2
(Error in the column)
1
8
1
8
1
1
1
8
1
Testing the program:
Example 2:
Dimensions N=(N<10)
3
Filling magic square
181811181
No magic square
Repeat? Yes-y , No-Enter
Example 3
(Error in the 1-st diagonal)
1
1
8
8
1
1
1
8
1
Testing the program:
Example 3:
Dimensions N=(N<10)
3
Filling magic square
118811181
No magic square
Repeat? Yes-y , No-Enter
Example 4
(Error in the 2-nd diagonal)
1
8
1
8
1
1
1
1
8
Testing the program:
Example 4:
Dimensions N=(N<10)
3
Filling magic square
181811118
No magic square
Repeat? Yes-y , No-Enter
Example 5
(Magic square)
4
3
8
9
5
1
2
7
6
Testing the program:
Example 5:
Dimensions N=(N<10)
3
Filling magic square
438951276
Magic square!
Repeat? Yes-y , No-Enter
Example 6
(Magic square)
31
8
22
15
30
5
20
11
24
16
28
12
17
26
13
21
9
25
34
1
23
14
35
4
7
32
10
27
6
29
2
33
19
18
3
36
Testing the program:
Example 6:
Dimensions N=(N<10)
6
Filling Magic Square
31 8 22 15 30 5 20 11 24 16 28 12
17 26 13 21 9 25 34 1 23 14 35 4
7 32 10 27 6 29 2 33 19 18 3 36
Magic Square!
Repeat? Yes-Y, No-Enter
Example 7
(Magic square)
16
78
76
74
9
10
12
14
80
81
28
62
60
23
24
26
64
1
79
65
36
50
33
34
52
17
3
77
63
53
40
39
44
29
19
5
75
61
51
45
41
37
31
21
7
11
25
35
38
43
42
47
57
71
13
27
30
32
49
48
46
55
69
15
18
20
22
59
58
56
54
67
2
4
6
8
73
72
70
68
66
Testing the program:
Example 7:
Dimensions N=(N<10)
9
Filling Magic Square
16 81 79 77 75 11 13 15 2 78 28 65 63 61 25 27 18 4
76 62 36 53 51 35 30 20 6 74 60 50 40 45 38 32 22 8
9 23 33 39 41 43 49 59 73 10 24 34 44 37 42 48 58 72
12 26 52 29 31 47 46 56 70 14 64 17 19 21 57 55 54 68
80 1 3 5 7 71 69 67 66
Magic Square!
Repeat? Yes-Y, No-Enter