Question
Part A: #include #include /* define data_t as int */ typedef int data_t; /* Create abstract data type for vector */ typedef struct { long
Part A:
#include
} /* * Find the greatest element (max) in a vector. * Return the index of the max value */ void get_max_element_opt(vec_ptr v, data_t *dest) { long max_index = 0; long i; long len; data_t max; data_t current; *dest = -1; /ot found yet get_vec_element(v, max_index, &max); len = vec_length(v); for (i = 1; i max) { max_index = i; max = current; } } *dest = max; }
PART B:
Let's stay with the get_max_element_opt() function from Part A. We include the get_vec_element() and get_max_element_opt() function for reference.
/* * Retrieve vector element and store at dest. * Return 0 (out of bounds) or 1 (successful) */ int get_vec_element(vec_ptr v, long index, data_t *dest) { if (index = v->len) return 0; *dest = v->data[index]; return 1; } /* Return length of vector */ long vec_length(vec_ptr v) { return v->len; } /* * Find the greatest element (max) in a vector. * Return the index of the max value */ void get_max_element_opt(vec_ptr v, data_t *dest) { long max_index = 0; long i; long len; data_t max; data_t current; *dest = -1; /ot found yet get_vec_element(v, max_index, &max); len = vec_length(v); for (i = 1; i max) { max_index = i; max = current; } } *dest = max; }
PART C:
Let's revisit the code from Parts A and B one more time. We include the get_vec_element() and get_max_element_opt() function for reference.
/* * Retrieve vector element and store at dest. * Return 0 (out of bounds) or 1 (successful) */ int get_vec_element(vec_ptr v, long index, data_t *dest) { if (index = v->len) return 0; *dest = v->data[index]; return 1; } /* Return length of vector */ long vec_length(vec_ptr v) { return v->len; } /* * Find the greatest element (max) in a vector. * Return the index of the max value */ void get_max_element_opt(vec_ptr v, data_t *dest) { long max_index = 0; long i; long len; data_t max; data_t current; *dest = -1; /ot found yet get_vec_element(v, max_index, &max); len = vec_length(v); for (i = 1; i max) { max_index = i; max = current; } } *dest = max; }Supply the missing code in a segment of the optimized function The aim is still to find the max value with only half of the iterations This time, reduce the number of iterations by a factor k and assume that the vector stores any number of elements Drag the appropriate labels to their correct location in the function. Not all labels will be used View Available Hint(s) Reset Help max_index-i+2 get vec element (v, max index, max); max intfexi+1 len = length-k+1 (v, i, ¤t) length - vec_lengthv) for t(v, i, ¤t) if (current> max) max index = i; max current ; get vec element(v, i + 1, ¤t); if (current>max) max indexi1; maxcurrent; i length; i (v, ln, ¤t) length - vec_lengthiv)-1 (v, langth, ¤t) get vea element (v, i 2, current) if (current > max) max-current for
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started