




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this past exam are: Implementation Requirements, Related, Debugging, Linked Data Structures, Multi-Dimensional Arrays, Destroy, Related
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Practice exam
Problem 1: MP5-related Problem 2: LC- Problem 3: Debugging Problem 4: Linked data structures Problem 5: Multi-dimensional arrays
Please note You are allowed to bring four handwritten 8.5 x 11" sheets of notes. Printed notes will be confiscated! On the real exam, we will provide you with the actual codes to work on as well as details about compiling, executing, and testing. For the practice exam, we provide you with just a handout and some limited examples of how to use the functions that you are required to implement. It will be a good exercise for you to write the rest of the code.
For this problem, you will be implementing the destroy search tree function which tears down an AI search tree data structure. Each node on the tree has a distance associated with it, and an array of four pointers to child nodes, each element corresponding to the four cardinal directions. A NULL pointer means no child node in that direction.
You are required to implement the following recursive function that destroys the tree:
void destroy_search_tree(tree_node * node);
Tree node structure is defined as follows:
typedef struct tree_node { int distance; /*! Pointers to the four child nodes: N,S,E,W */ struct tree_node * branches[4]; } tree_node;
We highly recommend running your code with valgrind to verify that the tree is de-allocated correctly.
/* create some tree */ tree_node *root=calloc(1, sizeof(tree_node)); for (i=0; i < 4; i++) root->branches[i] = calloc(1, sizeof(tree_node)); for (i=0; i < 4; i++) root->branches[1]->branches[i] = calloc(1, sizeof(tree_node));
/* destroy it */ destroy_search_tree(root);
.orig x MAX_DEPTH ; Build the activation record
PART_A ; IMPLEMENT ME! ; if (node == NULL) ; { ; return 0; ; } ; ; Note: If "return 0" is to be executed, set the return ; value to "0" and then jump/branch to DONE where the rest ; of the stack is torn down. Otherwise jump/branch to PART_B
PART_B ; IMPLEMENT ME! ; ldepth = max_depth(node->left); ; rdepth = max_depth(node->right); ; ; Note: You will need to set up the parameters for the ; recursive call and then JSR to MAX_DEPTH. Finally, ; read the return values and store in local variables as ; needed to implement C code.
PART_C ; IMPLEMENT ME! ; if (ldepth > rdepth) ; { ; return ldepth + 1; ; } ; ; Note: If this is to be executed, set the return value ; and jump/branch to DONE. Otherwise jump/branch to PART_D.
PART_D ; IMPLEMENT ME! ; else ; { ; return rdepth + 1; ; } ; ; Note: Set return value and jump/branch to DONE.
DONE ; Teardown the activation record and return RET .end
You are given the following code for sorting elements of a linked list. The code contains a single bug which you are asked to find and fix. In order to do so, you need to setup a test environment that calls sort_list function on some linked list. Any modifications of the provided code not related to fixing the bug are not allowed. Good luck!
typedef struct node_t { int val; struct node_t *next; } node;
int compare(void *xa, void *xb) { node *a = xa; node *b = xb; return a->val - b->val; }
int count_nodes(node *head) { if (!head) return 0; return 1+count_nodes(head->next); }
void sort_list(node *head) { int n = count_nodes(head); node **all = malloc(sizeof(node )n); int flag = 1; node *tmp = *head; int i;
for (i=0;i<n;i++) { all[i] = tmp; tmp = tmp->next; }
while (flag) { flag = 0; for (i=1;i<n;i++) { if (compare(all+i-1, all+i) < 0) { tmp = all[i-1]; all[i-1] = all[i]; all[i] = tmp; flag = 1; } } }
*head = all[0]; for (i=1;i<n;i++) all[i-1]->next = all[i]; all[i-1]->next = NULL; free(all); }
In this assignment, you will implement a function for computing 2D image convolution. The result of an image convolution is a new image of the same size as the original image with pixel values computed as the sum of the product between the pixels from the original image and the convolution kernel.
Given an image A represented as an N×M array and a convolution kernel B represented as 3 × 3 array , the result of the convolution of image A by the convolution kernel B is an N×M image C whose pixels are computed as
∑ ∑
where 0≤ i ≤N-1 is the row index and 0≤ j ≤M-1 is the column index.
You are required to write the following function that implements the above algorithm:
void im_convolution(int *A, int *B, int *C, int N, int M);
The two parameters, A and B, are pointers to one-dimensional arrays containing the two- dimensional image and convolution kernel arrays, respectively. The parameter C is a pointer to the array for storing the final image. Note that the image A has dimensions N×M pixels, the convolution kernel B has fixed dimensions 3 × 3 pixels, and the image C has dimensions N×M pixels. You can assume that all three arrays are properly allocated and the input arrays are filled in with data stored in row-major order. You are required to fill in the output array, C, with the convolved image also stored in the row-major order.
You are also required to implement a helper function that computes a convolution at location (x, y):
int point_cnv(int *A, int *B, int N, int M, int x, int y);
You are required to call this function within im_convolution function to compute the pixel values of the resulting image C.
When computing the convolution, assume that image A is zero-padded. That is, when point falls outside the image array (when i + s <0, or i + s ≥N, or j + t <0, or j + t ≥N), assume its value is always set to 0.
int A[] = { 1, 1, 1, 1, 1, /* a 5 ×5 pixels image / 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, 2, 2, 2, 2, 2 }; int B[] = { -1, -2, -1, / a 3 ×3 convolution kernel / 0, 0, 0, 1, 2, 1 }; int C[25]; / a 5×5 image / im_convolution(A, B, C, 5, 5); / C = A convolved with B */
After this code is executed, image array C should be set to
{ 27, 36, 36, 36, 27 24, 32, 32, 32, 24, -12, -16, -16, -16, -12, -21, -28, -28, -28, -21, -15, -20, -20, -20, -15 }