Question: Implement the get_array_product function. This function computes the product of the finite values in an array. The function should accept two parameters: an array of
Implement the get_array_product function. This function computes the product of the finite values in an array.
The function should accept two parameters: an array of double-precision floating point values; and an integer which specifies the number of usable elements in the array. The value returned is the product of all finite elements in the usable part of the array, or the special value NAN (Not A Number) if there are no finite values in the usable part of the array (which includes the case where there are no usable elements). You can use the standard library function isfinite() to determine if a floating point value is finite.
Detailed instructions are included as in-line comments in the test driver below.::::::
C Programming
#include
// (a) Begin the definition of a function called mul_items // which computes the product of the finite elements of an array // of double precision floating point values. // // Parameters: // items - an array of double precision floating point values. // num_items - an int which specifies the maximum number of items // to process. // // Returns: // A double precision floating point value: // * If the array contains at least one finite element: the result // is equal to the product of the finite elements. // * Otherwise: return NAN.
INSERT RESULT TYPE, NAME, AND PARAMETERS HERE { // (b) Insert logic required to solve the problem here. }
void run_test(const char * label, double x[], int count) { double prod = mul_items(x, count); printf("%s ", label); printf("\tInput data: ");
for (int i = 0; i < count; i++) { printf("\t%d\t%f ", i, x[i]); }
printf("\tProduct = %f ", prod); }
int main(void) { double x1[] = { 0 }; run_test("Count == 0", x1, 0);
double x2[] = { NAN, +INFINITY, -INFINITY }; run_test("No finite values", x2, 3);
double x3[] = { 1, 2, 3, 4, 5, 6, 7 }; run_test("Several finite values", x3, 7);
double x4[] = { 2, M_PI, NAN, 3, INFINITY, 4 }; run_test("A mix of finite values and infinities", x4, 6);
double x5[] = { 1 }; run_test("Product is 1", x5, 1);
double x6[] = { 1.0, NAN, 1.0, 1.0, INFINITY }; run_test("Product is also 1", x6, 5);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
