KCULEARN Back Forw Back Forward SYSC2006-Assignment2 (100 points) Purpose. To al
ID: 3867932 • Letter: K
Question
KCULEARN Back Forw Back Forward SYSC2006-Assignment2 (100 points) Purpose. To allow you to exercise with basic arrays, structs, and pointers (make and document any reasonable assumptions) Question: Given a struct as follows: typedef struct int y lpoint: Write a separate C program to implement the following functions, your programs must have a main function to test your functions: a float euclideanDistance(point pl, point p2): //computes the Euclidean distance between two points b. float averageDistance(point arr1[- point arr211): /takes two arrays of points, computes the Euclidean distance //between each corresponding points in arr1, and arr2 //and returns the average of the distances c. float maximumDistance(point "arr1, point arr2) /takes two pointers to arrays of points, computes the Eucdidean distance //between each corresponding points in arrl, and arr2 //and returns the MAXIMUM of the distances vertex 1, point vertex2, point P d int //given a rectangle with the upper left vertex as vertex //and the lower right vertex as vertex 2 //the function returns 1 ifpoint p is within the bounds of the rectanple //or returns 0 if the point is not within the bounds of the rectangle e.point fourthVertex[point vertex1, point vertex2, point vertex3): //vertexi is the upper left vertex of a rectangle //vertex 2 is the lower left vertex of a rectangle /vertex 3 is the upper right vertex of a rectangle //the function returns a point that holds the value of the fourth vertex You will be graded as follows for each problem (10 points eachExplanation / Answer
#include #include #include struct test_struct { int val; struct test_struct *next; }; struct test_struct *head = NULL; struct test_struct *curr = NULL; struct test_struct* create_list(int val) { printf(" creating list with headnode as [%d] ",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf(" Node creation failed "); return NULL; } ptr->val = val; ptr->next = NULL; head = curr = ptr; return ptr; } struct test_struct* add_to_list(int val, bool add_to_end) { if(NULL == head) { return (create_list(val)); } if(add_to_end) printf(" Adding node to end of list with value [%d] ",val); else printf(" Adding node to beginning of list with value [%d] ",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf(" Node creation failed "); return NULL; } ptr->val = val; ptr->next = NULL; if(add_to_end) { curr->next = ptr; curr = ptr; } else { ptr->next = head; head = ptr; } return ptr; } struct test_struct* search_in_list(int val, struct test_struct **prev) { struct test_struct *ptr = head; struct test_struct *tmp = NULL; bool found = false; printf(" Searching the list for value [%d] ",val); while(ptr != NULL) { if(ptr->val == val) { found = true; break; } else { tmp = ptr; ptr = ptr->next; } } if(true == found) { if(prev) *prev = tmp; return ptr; } else { return NULL; } } int delete_from_list(int val) { struct test_struct *prev = NULL; struct test_struct *del = NULL; printf(" Deleting value [%d] from list ",val); del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next; if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } } free(del); del = NULL; return 0; } void print_list(void) { struct test_struct *ptr = head; printf(" -------Printing list Start------- "); while(ptr != NULL) { printf(" [%d] ",ptr->val); ptr = ptr->next; } printf(" -------Printing list End------- "); return; } int main(void) { int i = 0, ret = 0; struct test_struct *ptr = NULL; print_list(); for(i = 5; i0; i--) add_to_list(i,false); print_list(); for(i = 1; ival); } print_list(); ret = delete_from_list(i); if(ret != 0) { printf(" delete [val = %d] failed, no such element found ",i); } else { printf(" delete [val = %d] passed ",i); } print_list(); } return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.