Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #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; }

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; }

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, assume that the array stores an even number of elements.

image text in transcribed

Drag the appropriate labels to their correct location in the function. Not all labels will be used View Available Hint(s) Reset Help dest 1; /ot found yet get vec element (v, max index, max); (i 1; i max max max index-i; max = current ; length vec_length(v)-1 length = vee length(v) (i = 1; i max) - max current ; get vec element

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Name and explain the most widely used neural network algorithm.

Answered: 1 week ago

Question

Were they made on a timely basis?

Answered: 1 week ago

Question

Did the decisions need to be made, or had they already been made?

Answered: 1 week ago