Write the following three functions: a) The function gets an array A of length n of ints, and a boolean predicate pred. It returns the smallest index i such that pred(A[i])==true. If no such element is not found, the function returns -1. int find(int* A, int n, bool (*pred)(int)); b) The function gets an array A of length n of ints, and a function f. It applies f to each element of A. void map(int* A, int n, int (*f)(int)); c) The function gets an array A of length n of ints, and a function f. The function f gets 2 ints and works as follows: Start with accumulator = A[0] For i=1...length-1 compute accumulator=f(accumulator, A[i]) Return accumulator
Write the following three functions:
a) The function gets an array A of length n of ints, and a boolean predicate pred. It returns the smallest index i such that pred(A[i])==true.
If no such element is not found, the function returns -1.
int find(int* A, int n, bool (*pred)(int));
b) The function gets an array A of length n of ints, and a function f. It applies f to each element of A.
void map(int* A, int n, int (*f)(int));
c) The function gets an array A of length n of ints, and a function f. The function f gets 2 ints and works as follows:
-
Start with accumulator = A[0]
-
For i=1...length-1 compute accumulator=f(accumulator, A[i])
-
Return accumulator
For example, if f computes the sum of the two inputs, then reduce() will compute the sum of the entire array.
int reduce(int* A, int n, int (*f)(int,int));
Step by step
Solved in 4 steps