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); + +} +``` diff --git a/chapters/fundamental_algorithms/tree_traversal.md b/chapters/fundamental_algorithms/tree_traversal.md index ced447cdd..8ef4d0ad9 100644 --- a/chapters/fundamental_algorithms/tree_traversal.md +++ b/chapters/fundamental_algorithms/tree_traversal.md @@ -327,3 +327,164 @@ 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){ + // 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; + + 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){ + // Removing the last node_list of the stack + 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){ + // Removing the first node_list of the queue + 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){ + // Creating a stack and then setting its value to 0 + 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){ + // Checking if the node has any children + if(!temp.children){ + break; + } + push(&stack, temp.children + i); + } + } +} + +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); + 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){ + // Checking if the node has any children + if(!temp.children){ + break; + } + push(&queue, temp.children + i); + } + } +} + +void destroy_tree(node *n){ + // This function is for cleaning up all the nodes + 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; +} +```