Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
cantor_set.c File Reference

Program to generate Cantor ternary set More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for cantor_set.c:

Data Structures

struct  _cantor_set
 structure to define Cantor set More...
 

Typedefs

typedef struct _cantor_set CantorSet
 structure to define Cantor set
 

Functions

void propagate (CantorSet *head)
 Iterative constructor of all sets in the current level.
 
void print (CantorSet *head)
 Print sets in the current range to stdout
 
void free_memory (CantorSet *head)
 Clear memory allocated by propagate function.
 
int main (int argc, char const *argv[])
 Main function.
 

Detailed Description

Program to generate Cantor ternary set

Function Documentation

◆ free_memory()

void free_memory ( CantorSet head)

Clear memory allocated by propagate function.

Parameters
headpointer to first allocated instance.
73{
74 if (!head)
75 return;
76
77 if (head->next)
78 free_memory(head->next);
79
80 free(head);
81}
void free_memory(CantorSet *head)
Clear memory allocated by propagate function.
Definition cantor_set.c:72
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
struct node * next
List pointers.
Definition bfs.c:24
Here is the call graph for this function:

◆ main()

int main ( int  argc,
char const *  argv[] 
)

Main function.

85{
86 int start_num, end_num, levels;
87
88 if (argc < 2)
89 {
90 printf("Enter 3 arguments: start_num \t end_num \t levels\n");
91 scanf("%d %d %d", &start_num, &end_num, &levels);
92 }
93 else
94 {
95 start_num = atoi(argv[1]);
96 end_num = atoi(argv[2]);
97 levels = atoi(argv[3]);
98 }
99
100 if (start_num < 0 || end_num < 0 || levels < 0)
101 {
102 fprintf(stderr, "All numbers must be positive\n");
103 return -1;
104 }
105
106 CantorSet head = {.start = start_num, .end = end_num, .next = NULL};
107
108 // loop to propagate each level from top to bottom
109 for (int i = 0; i < levels; i++)
110 {
111 printf("Level %d\t", i);
112 print(&head);
113 propagate(&head);
114 printf("\n");
115 }
116 printf("Level %d\t", levels);
117 print(&head);
118
119 // delete all memory allocated
120 free_memory(head.next);
121
122 return 0;
123}
void propagate(CantorSet *head)
Iterative constructor of all sets in the current level.
Definition cantor_set.c:23
void print(CantorSet *head)
Print sets in the current range to stdout
Definition cantor_set.c:55
structure to define Cantor set
Definition cantor_set.c:12
Here is the call graph for this function:

◆ print()

void print ( CantorSet head)

Print sets in the current range to stdout

Parameters
headpointer to first set in the current level
56{
57 CantorSet *temp = head;
58 while (temp != NULL) // print while a valid set is found
59 {
60 printf("\t");
61 printf("[%lf] -- ", temp->start);
62 printf("[%lf]", temp->end);
63 temp = temp->next;
64 }
65
66 printf("\n");
67}

◆ propagate()

void propagate ( CantorSet head)

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.

Parameters
headpointer to interval set instance to update
24{
25 // if input is NULL, ignore the process
26 if (head == NULL)
27 return;
28
29 CantorSet *temp = head; // local pointer to track propagation
30
31 // create new node for the new set
32 CantorSet *newNode = (CantorSet *)malloc(sizeof(CantorSet));
33
34 // get 1/3rd of interval
35 double diff = (((temp->end) - (temp->start)) / 3);
36
37 // update interval ranges
38 newNode->end = temp->end;
39 temp->end = ((temp->start) + diff);
40 newNode->start = (newNode->end) - diff;
41
42 // update pointer to next set in this level
43 newNode->next = temp->next;
44
45 // point to next set
46 temp->next = newNode;
47
48 // create next set
49 propagate(temp->next->next);
50}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
Here is the call graph for this function: