Write this program in C LANGUAGE Implement the following C f

Write this program in C LANGUAGE

Implement the following C function. int proc(int* p, int m, int n, int k, int (*f)(int u, int v)) The parameter p points to a 2D int array of dimensions m by n(a[m][n]). The parameter k is the linear index of the element to be processed. The last parameter f is a function pointer. The function proc first locates the element a [i] [j] identified by the linear index k and its 8 neighbors as shown below. ... It then calls the function f by passing a [i] [j] to the parameter u and the stun of the 8 neighbors to v. Finally, proc returns the value returned by f. For example, the following test program should print the number 9. int fun(int u, int v) {return u - v/8;} int main() {int a[4][5] = {{1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}, {10, 20, 30, 40, 50}, {11, 22, 33, 44, 55}}; int b = proc(a[0], 4, 5, 13, fun); printf(\"%d\ \", b); return 0;}

Solution

//Please find below the implementation of the \"proc\" function in C:

int proc(int *p, int m, int n, int k, int (*fun)(int u, int v))
{
   //element stores the value at identified by linear index k
   int element = *(p+k);
  
   //below variables store the 8 neighbours of the element
   int first, second, third, fourth, fifth, sixth, seventh, eigth;
  
   //sum variable stores the sum of all the 8 neighbours
   int sum;
  
   // stores the value returned by the function \'fun\'
   int fun_return;

   // Accessing the 2d array with the respective linear indices of the neighbours
   first = *(p+(k-n));
   second = *(p+((k-n)-1));
   third = *(p+((k-n)+1));
   fourth = *(p+(k-1));
   fifth = *(p+(k+1));
   sixth = *(p+(k+n));
   seventh = *(p+((k+n)-1));
   eigth = *(p+((k+n)+1));


   //computing the sum
   sum = first+second+third+fourth+fifth+sixth+seventh+eigth;
  
   //calling the \'fun\' function with the appropriate arguments
   fun_return = fun(element,sum);
  
   // returning the value returned by the \'fun\' function  
   return fun_return;
}

Write this program in C LANGUAGE Implement the following C function. int proc(int* p, int m, int n, int k, int (*f)(int u, int v)) The parameter p points to a 2

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site