Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Define a function pointer variable that can point to the four functions creat

ID: 3545995 • Letter: 1

Question

1.        Define a function pointer variable that can point to the four functions created earlier.

2.        Set that function pointer variable to your sum function.

3.        Call calculate with the function pointer variable and the values 4 and -2.  The result should be 2.

4.        Set the function pointer variable to your difference function.

5.        Call calculate with the function pointer variable and  the values 4 and -2.  The result should be 6.

6.        Call calculate with your divide function (NOT using the function pointer variable) and the values 2 and 6.  The result should be 0.333333333...

7.        Call calculate with your your average function (NOT using the function pointer variable) and the values 4 and -4.  The result should be 0.


Explanation / Answer

#include<stdio.h>


double sum(int a,int b)

{

return a+b;

}

double differ(int a,int b)

{

return b-a;

}

double avg(int a,int b)

{

return (a+b)/2;

}

double div(int a,int b)

{

double t;

t=(double)a/b;

return t;

}

int main()

{

double (*calc)(int , int);

double temp;

calc=&sum;

temp=calc(4,-2);

printf("%lf ",temp);


calc=&differ;

temp=calc(4,-2);

printf("%lf ",temp);


temp=div(2,6);

printf("%lf ",temp);


temp=avg(4,-4);

printf("%lf ",temp);

}