-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
cantor_set.c
123 lines (102 loc) · 2.88 KB
/
cantor_set.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @file
* @brief Program to generate [Cantor ternary
* set](https://en.wikipedia.org/wiki/Cantor_set)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** structure to define Cantor set */
typedef struct _cantor_set
{
double start; /**< start of interval */
double end; /**< end of interval */
struct _cantor_set *next; /**< pointer to next set */
} CantorSet;
/** Iterative constructor of all sets in the current level. This function
* dynamically allocates memory when creating new sets. These are freed by the
* function ::free_memory.
* @param head pointer to interval set instance to update
*/
void propagate(CantorSet *head)
{
// if input is NULL, ignore the process
if (head == NULL)
return;
CantorSet *temp = head; // local pointer to track propagation
// create new node for the new set
CantorSet *newNode = (CantorSet *)malloc(sizeof(CantorSet));
// get 1/3rd of interval
double diff = (((temp->end) - (temp->start)) / 3);
// update interval ranges
newNode->end = temp->end;
temp->end = ((temp->start) + diff);
newNode->start = (newNode->end) - diff;
// update pointer to next set in this level
newNode->next = temp->next;
// point to next set
temp->next = newNode;
// create next set
propagate(temp->next->next);
}
/** Print sets in the current range to `stdout`
* @param head pointer to first set in the current level
*/
void print(CantorSet *head)
{
CantorSet *temp = head;
while (temp != NULL) // print while a valid set is found
{
printf("\t");
printf("[%lf] -- ", temp->start);
printf("[%lf]", temp->end);
temp = temp->next;
}
printf("\n");
}
/** Clear memory allocated by ::propagate function.
* @param head pointer to first allocated instance.
*/
void free_memory(CantorSet *head)
{
if (!head)
return;
if (head->next)
free_memory(head->next);
free(head);
}
/** Main function */
int main(int argc, char const *argv[])
{
int start_num, end_num, levels;
if (argc < 2)
{
printf("Enter 3 arguments: start_num \t end_num \t levels\n");
scanf("%d %d %d", &start_num, &end_num, &levels);
}
else
{
start_num = atoi(argv[1]);
end_num = atoi(argv[2]);
levels = atoi(argv[3]);
}
if (start_num < 0 || end_num < 0 || levels < 0)
{
fprintf(stderr, "All numbers must be positive\n");
return -1;
}
CantorSet head = {.start = start_num, .end = end_num, .next = NULL};
// loop to propagate each level from top to bottom
for (int i = 0; i < levels; i++)
{
printf("Level %d\t", i);
print(&head);
propagate(&head);
printf("\n");
}
printf("Level %d\t", levels);
print(&head);
// delete all memory allocated
free_memory(head.next);
return 0;
}