Question: Please write a C program to realize pointer to function. Here is a sample code that we see in our lecture. Pointer to function can
Please write a C program to realize pointer to function. Here is a sample code that we see in our lecture. Pointer to function can help us realize the function polymorphism, in other words, we can call different functions based on user's needs.
#include
int s = 0; int a[] = {3,4,7,8, 19, 23};
typedef void (*FuncPtr)(int a);
void intArrayMapper( int *array, int n, FuncPtr func ) {
// do other things.
// one particular task which depends on a particular function that is passed in here. for( int i= 0; i < n; i++ ) (*func)( array[ i ] ); }
void sumInt( int val ) { s += val; }
void printInt( int val ) { printf("val = %d ", val); }
void maxInt( int val) {
}
main( ) { // Print the values in the array intArrayMapper(a, sizeof(a)/sizeof(int), &printInt);
// Print the sum of the elements in the array s = 0; intArrayMapper(a, sizeof(a)/sizeof(int), &sumInt);
intArrayMapper(a, sizeof(a)/sizeof(int), &maxInt);
printf("total value = %d ", s);
}
Please write your own c program which can do different statistical calculation for an integer array. For example, you can choose to call different function to compute the min, max, or mean of an array. The min, max or mean can be the return value of this function. Prompt the user to choose his/her statistical function, either min, max or mean. The array items can be either read in from input.text or randomly generated.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
