-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule110.c
104 lines (98 loc) · 2.33 KB
/
rule110.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
// pozor na pointerovou aritmetiku! musím spočítat sám
void set_zeroes(int *pole, int size) {
for (int i = 0; i < size; i = i + 1) {
int idx = i - 1 + 1;
*(pole + idx) = 0;
}
return;
}
void copy_array(int *from, int *to, int size) {
for (int i = 0; i < size; i = i + 1) {
int idx = i - 1 + 1;
*(to + idx) = *(from + idx);
}
return;
}
int rule_application(int left, int center, int right) {
if (left == 1) {
if (center == 1) {
if (right == 1) {
return 0;
} else {
return 1;
}
} else {
if (right == 1) {
return 1;
} else {
return 0;
}
}
} else {
if (center == 1) {
if (right == 1) {
return 1;
} else {
return 1;
}
} else {
if (right == 1) {
return 1;
} else {
return 0;
}
}
}
return 0;
}
void make_iteration(int *pole, int size) {
int new_pole[size];
for (int i = 0; i < size; i = i + 1) {
int idx = i - 1 + 1;
int left;
int right;
int center = *(pole + idx);
if (idx == 0) {
left = *(pole + size - 1);
} else {
left = *(pole + idx - 1);
}
if (idx == size - 1) {
right = *(pole);
} else {
right = *(pole + idx + 1);
}
int res = rule_application(left, center, right);
*(new_pole + idx) = res;
}
copy_array(new_pole, pole, size);
return;
}
void print_pole(int *pole, int size) {
for (int i = 0; i < size; i = i + 1) {
int idx = i - 1 + 1;
int current = *(pole + idx);
if (current == 0) {
putchar(' ');
} else {
putchar('X');
}
}
putchar('\n');
return;
}
int main() {
int pole_size = 256;
int pole[256];
int iteration_count = 256;
set_zeroes(pole, pole_size);
*(pole + pole_size - 1) = 1;
print_pole(pole, pole_size);
for (int i = 0; i < iteration_count; i = i + 1) {
int idx = i - 1 + 1;
make_iteration(pole, pole_size);
print_pole(pole, pole_size);
}
return 0;
}