Question
Implement the approx_search function. This function returns the position of the last approximate match to a designated query value in the array. The hash-tag for
Implement the approx_search function. This function returns the position of the last approximate match to a designated query value in the array. The hash-tag for this exercise is #cab202ArrayPractice4.
The function should accept four parameters: an array of double-precision floating point values; an integer which specifies the number of usable elements in the array; a double precision floating point query value; and a double precision floating point error tolerance which represents the largest allowable mismatch. The value returned is the position of the last value in the array that is approximately equal to the query, or -1 if there are no matches or usable elements.
For the purpose of this exercise, x is approximately equal to y with tolerance tol if and only if fabs(x - y) <= tol. Here fabs is the standard C function which returns the absolute value of its double precision floating point argument.
Detailed instructions are included as in-line comments in the test driver below.
#include
// (a) Begin the definition of a function called approx_search // which locates values from an array which lie within a small // margin of error of a designated query value. Return the last // (greatest) index value for which this holds. // // Parameters: // values - an array of double precision floating point values. // num_vals - an int which specifies the number of items to process. // query - a double precision floating point value to locate. // tol - a small, strictly positive, double precision floating // point margin of error for equality comparisons. // // Returns: // An int indicating the location at which the query value may be found: // * If num_vals is 0: return -1 to record the fact that there is // no data to process. // * Otherwise: return i in 0 .. (num_vals-1) such that: // (fabs(values[i] - query) <= tol) AND // (For any j where fabs(values[j] - query) <= tol: j <= i) // i.e. // values[i] is approximately equal to query, and i is the "largest" index where // that happens.
INSERT RESULT TYPE, NAME, AND PARAMETERS HERE { // (b) Declare variable store index position, initially -1.
// (c) Iterate over array, counting from 0. { // (d) If item at current position is within +/- error of // the query, replace index with current position. }
// (e) Return index position. }
#define MAX_ITEMS (100)
int main(void) { double items[MAX_ITEMS]; int array_size; double query; double error;
// Get number of items. printf("Please enter number of items (up to %d) that will be processed: ", MAX_ITEMS); scanf("%d", &array_size);
// if number of items exceeds array size, restrict it to that value. if (array_size > MAX_ITEMS) { array_size = MAX_ITEMS; }
for (int i = 0; i < array_size; i++) { printf("Please enter (floating point) item %d of %d: ", (i + 1), array_size); scanf("%lf", &items[i]); }
printf("Please enter (floating point) query value: "); scanf("%lf", &query);
printf("Please enter (floating point) error margin: "); scanf("%lf", &error);
int result = approx_search(items, array_size, query, error);
printf("The last approximate match to the query is at position %d. ", result);
return 0; }
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