Exercise 9-2. Beginning C, I. Horton

Implement functions with the prototypes:

double add(double a, double b); //Returns a + b
double subtract(double a, double b); //Returns a - b
double multiply(double a, double b); //Returns a * b
double array_op(double array[], size_t size,
double (*pfun)(double, double));

The parameters for the array_op() function are the array to be operated on, the number of elements in the array, and a pointer to a function defining the operation to be applied between successive elements. The array_op function should be implemented so that when the subtract() is passed as the third argument, the function combines the elements with alternating signs. So for an array with four elements, x1,x2,x3, and x4, it computes the value of x1 - x2 + x3 - x4. To multiply, it will just be the product of the elements, x1 * x2 * x3 * x4, for example.

Demos¡nstrate the operation of these functions with a suitable version of main().

exercise_9_2a

This entry was posted in C. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s