I am trying to loop through the elements of the array and for each of these elements I have to call a function pointed and passed the element address along with the address stored in the last parameter. And the function returns the number of elements for which the function pointed to returned true. This is the requirements which I have been trying to follow but I can't get my function to do exactly what is asked for to do..
------requirements & function------------
/* Write an enumeration function named sum() with the following parameters: a generic pointer an int that holds the number of elements in the array pointed to an int that holds the size in bytes of a single element a pointer to a function that has two generic pointer parameters and returns a bool a generic pointer Your function moves through the array pointed to by the first parameter element by element. For each element, your function calls the function pointed to and passes the element's address along with the address stored in the last parameter. Your function returns the number of elements for which the function pointed to returned true. Since your first function parameter is a generic pointer and your function can handle any type, you will need to cast the address of the input array to the address of a chars in order to move from one element to the next. */
int sum(void* x, int n, int s, bool(f)(void, void*), void* z){
char *arr = static_cast<char*>(x); int count = 0; for (; s < n-2; s++){ arr += s; count += f(arr, z); } if (n / 1 == n) return count; else if (n % 2 == 0) return count; else return 0;
}
I hope you guys can show me and explain or at least what I am not doing in the proper way.. I will really appreciate it is the only way, I can learn.. :)
If need more explanation please let me know..
******ADDITION*******
/* Write a callback function named isEven() with the following parameters: a generic pointer to an input value a generic pointer to an output value Your function works with ints and returns true if the input value is even, false otherwise. Moreover, if the value is even, your function adds the value to that pointed to by the second parameter. */ bool isEven(void* x, void* z){ int a = *static_cast<int*>(x); int b = *static_cast<int*>(z); if (a % 2 == 0){ // finding even numbers b += a; return true; } else { return false; } } /* Write another callback function named isPrime() with the following parameters: a generic pointer to an input value a generic pointer to an output value Your function works with ints and returns true if the input value is a prime number, false otherwise. Moreover, if the value is prime, your function adds the value to that pointed to by the second parameter. */ bool isPrime(void* x, void* z){ int a = *static_cast<int*>(x); int b = *static_cast<int*>(z); if ((a / 1 == a) && (a / a == 1)){ // finding prime numbers b += a; return true; } else { return false; } }
**********EXPECTED OUTPUT***********
5 evens found in {1,2,3,4,5,6,7,8,9,10,11} sum is 30 5 primes found in {1,2,3,4,5,6,7,8,9,10,11} sum is 28