Question: #include #include /* define data_t as int */ typedef int data_t; /* Create abstract data type for vector */ typedef struct { long len; data_t

#include

#include /* define data_t as int */ typedef int data_t; /* Create abstract data type for vector */ typedef struct { long len; data_t *data; } vec_rec, *vec_ptr; /* Create vector of specified length */ vec_ptr new_vec(long len) { /* Allocate header structure */ vec_ptr result = (vec_ptr) malloc(sizeof(vec_rec)); data_t *data = NULL; if (!result) return NULL; /* Couldnt allocate storage */ result->len = len; /* Allocate array */ if (len > 0) { data = (data_t *)calloc(len, sizeof(data_t)); if (!data) { free((void *) result); return NULL; /* Couldnt allocate storage */ } } /* data will either be NULL or allocated array */ result->data = data; return result; } /* * 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; }

Let's rewrite the get_max_element_opt function to find the max value, but with only half the number of iterations.

Supply the missing code in a segment of the optimized function. Assume that the array stores an odd number of elements.

Drag the appropriate labels to their correct location in the function. Not all labels will be used.

#include #include /* define data_t as int */ typedef int data_t; /*

Reset Help *dest =-1; /ot found yet get_vec_element (v, max index, max) len-vec-length(v) /2 max index i+1 for get_vea_element (v, i, scurrent) if (current > max) t maxindex = i - max index i: max - current; len - vec_length(v) (i=1; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!