From a3bb3236203d333d412a544923e864a376bb0537 Mon Sep 17 00:00:00 2001 From: Gathros Date: Mon, 11 Sep 2017 17:26:55 +0100 Subject: [PATCH 1/4] Added C --- .../fundamental_algorithms/tree_traversal.md | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/chapters/fundamental_algorithms/tree_traversal.md b/chapters/fundamental_algorithms/tree_traversal.md index ced447cdd..668587d3f 100644 --- a/chapters/fundamental_algorithms/tree_traversal.md +++ b/chapters/fundamental_algorithms/tree_traversal.md @@ -327,3 +327,156 @@ def main(): main() ``` + +###C + +```C +#include +#include +#include + +typedef struct node{ + struct node *children; // I chose a pointer to create a dynamic array + int children_num; + int ID; +} node; + +// There is no stack or queue in c so I created my own +typedef struct node_list{ + node n; + struct node_list *last_list, *next_list; +} node_list; + +typedef struct node_points{ + node_list *start_point, *end_point; +} node_points; + +void push(node_points *np, node *n){ + node_list *temp = (node_list*)malloc(sizeof(node_list)); + temp->n = *n; + temp->last_list = temp->next_list = NULL; + + if(!np->end_point){ + np->start_point = temp; + }else{ + np->end_point->next_list = temp; + temp->last_list = np->end_point; + } + np->end_point = temp; +} + +void stack_pop(node_points *np){ + node_list *temp; + temp = np->end_point; + if(temp){ + np->end_point = temp->last_list; + if(!np->end_point){ + np->start_point = NULL; + } + free(temp); + } +} + +void queue_pop(node_points *np){ + node_list *temp; + temp = np->start_point; + if(temp){ + np->start_point = temp->next_list; + if(!np->start_point){ + np->end_point = NULL; + } + free(temp); + } +} + +void create_tree(node *n, int num_row, int num_child) { + n->ID = num_row; + if(num_row == 0){ + return; + } + + // Creating children + // Using malloc to make an array of nodes with size num_child + n->children = (node *)malloc(num_child*sizeof(*n->children)); + // When an array is made into a pointer you can't get it's size later + n->children_num = num_child; + for(int i = 0; i < num_child; ++i){ + node child; + create_tree(&child, num_row - 1, num_child); + *(n->children + i) = child; + } +} + +void DFS_recursive(node *n){ + printf("%d\n", n->ID); + + // Checking if the node's children exist + if(!n->children){ + return; + } + + for(int i = 0; i < n->children_num; ++i){ + DFS_recursive((n->children + i)); + } +} + +void DFS_stack(node *n){ + node_points stack; + memset(&stack, 0, sizeof(node_points)); + push(&stack, n); + node temp; + + while(stack.start_point != NULL){ + temp = stack.end_point->n; + printf("%d\n", temp.ID); + stack_pop(&stack); + for(int i=0; i < temp.children_num; ++i){ + if(!temp.children){ + break; + } + push(&stack, temp.children + i); + } + } +} + +void BFS_queue(node *n){ + node_points queue; + memset(&queue, 0, sizeof(node_points)); + push(&queue, n); + node temp; + + while(queue.start_point != NULL){ + temp = queue.start_point->n; + printf("%d\n", temp.ID); + queue_pop(&queue); + for(int i = 0; i < temp.children_num; ++i){ + if(!temp.children){ + break; + } + push(&queue, temp.children + i); + } + } +} + +void destroy_tree(node *n){ + if(n->ID == 0){ + return; + } + + for(int i = 0; i < n->children_num; ++i){ + destroy_tree(n->children + i); + } + free(n->children); +} + +int main() { + node root; + create_tree(&root, 3, 3); + //DFS_recursive(&root); + //DFS_stack(&root); + BFS_queue(&root); + destroy_tree(&root); + + return 0; +} +``` From c4f0653bb13b45bf14eff91b99c6aee53274f8b9 Mon Sep 17 00:00:00 2001 From: Gathros Date: Mon, 11 Sep 2017 17:28:52 +0100 Subject: [PATCH 2/4] fixed error --- chapters/fundamental_algorithms/tree_traversal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/fundamental_algorithms/tree_traversal.md b/chapters/fundamental_algorithms/tree_traversal.md index 668587d3f..4e610f50d 100644 --- a/chapters/fundamental_algorithms/tree_traversal.md +++ b/chapters/fundamental_algorithms/tree_traversal.md @@ -328,7 +328,7 @@ def main(): main() ``` -###C +### C ```C #include From e76e4f9667441b3869002793cee27cfc4cb8bcfb Mon Sep 17 00:00:00 2001 From: Gathros Date: Mon, 11 Sep 2017 17:46:54 +0100 Subject: [PATCH 3/4] Adding comments --- chapters/fundamental_algorithms/tree_traversal.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/chapters/fundamental_algorithms/tree_traversal.md b/chapters/fundamental_algorithms/tree_traversal.md index 4e610f50d..8ef4d0ad9 100644 --- a/chapters/fundamental_algorithms/tree_traversal.md +++ b/chapters/fundamental_algorithms/tree_traversal.md @@ -352,6 +352,7 @@ typedef struct node_points{ } node_points; void push(node_points *np, node *n){ + // Adding node into a queue or a stack node_list *temp = (node_list*)malloc(sizeof(node_list)); temp->n = *n; temp->last_list = temp->next_list = NULL; @@ -366,6 +367,7 @@ void push(node_points *np, node *n){ } void stack_pop(node_points *np){ + // Removing the last node_list of the stack node_list *temp; temp = np->end_point; if(temp){ @@ -378,6 +380,7 @@ void stack_pop(node_points *np){ } void queue_pop(node_points *np){ + // Removing the first node_list of the queue node_list *temp; temp = np->start_point; if(temp){ @@ -421,6 +424,7 @@ void DFS_recursive(node *n){ } void DFS_stack(node *n){ + // Creating a stack and then setting its value to 0 node_points stack; memset(&stack, 0, sizeof(node_points)); push(&stack, n); @@ -431,6 +435,7 @@ void DFS_stack(node *n){ printf("%d\n", temp.ID); stack_pop(&stack); for(int i=0; i < temp.children_num; ++i){ + // Checking if the node has any children if(!temp.children){ break; } @@ -440,6 +445,7 @@ void DFS_stack(node *n){ } void BFS_queue(node *n){ + // Creating a queue and then setting its value to 0 node_points queue; memset(&queue, 0, sizeof(node_points)); push(&queue, n); @@ -450,6 +456,7 @@ void BFS_queue(node *n){ printf("%d\n", temp.ID); queue_pop(&queue); for(int i = 0; i < temp.children_num; ++i){ + // Checking if the node has any children if(!temp.children){ break; } @@ -459,6 +466,7 @@ void BFS_queue(node *n){ } void destroy_tree(node *n){ + // This function is for cleaning up all the nodes if(n->ID == 0){ return; } From 610566d964280417297e307f1b2ea66579673b1f Mon Sep 17 00:00:00 2001 From: Gathros Date: Mon, 11 Sep 2017 18:01:02 +0100 Subject: [PATCH 4/4] Adding C to Verlet --- chapters/computational_physics/verlet.md | 69 ++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/chapters/computational_physics/verlet.md b/chapters/computational_physics/verlet.md index 8c4508ebb..2b6a5aed0 100644 --- a/chapters/computational_physics/verlet.md +++ b/chapters/computational_physics/verlet.md @@ -432,3 +432,72 @@ xlabel('Time (s)'); ylabel('Height (m)'); Submitted by P. Mekhail ![Verlet LabVIEW](verlet_labview.png) + +#### C + +``` +#include + +// Simple function for velocity-verlet +void verlet(double pos, double acc, double dt){ + + // Note that we are using a temp variable for the previous position + double prev_pos, temp_pos, time; + prev_pos = pos; + time = 0; + + while (pos > 0){ + time += dt; + temp_pos = pos; + pos = pos*2 - prev_pos + acc * dt * dt; + prev_pos = temp_pos; + } + + printf("%f\n", time); + +} + +// Simple function for stormer-verlet +void stormer_verlet(double pos, double acc, double dt){ + + double prev_pos, temp_pos, time, vel; + prev_pos = pos; + vel = 0; + time = 0; + while (pos > 0){ + time += dt; + temp_pos = pos; + pos = pos*2 - prev_pos + acc * dt * dt; + prev_pos = temp_pos; + + // The acceleration is constant, so the velocity is straightforward + vel += acc*dt; + } + + printf("%f\n", time); + +} + +// Simple function for velocity-verlet +void velocity_verlet(double pos, double acc, double dt){ + + double time, vel; + vel = 0; + time = 0; + while (pos > 0){ + time += dt; + pos += vel*dt + 0.5*acc * dt * dt; + vel += acc*dt; + } + + printf("%f\n", time); + +} + +int main(){ + verlet(5.0, -10, 0.01); + stormer_verlet(5.0, -10, 0.01); + velocity_verlet(5.0, -10, 0.01); + +} +```