Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
24
Task/Catamorphism/C/catamorphism.c
Normal file
24
Task/Catamorphism/C/catamorphism.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
|
||||
typedef int (*intFn)(int, int);
|
||||
|
||||
int reduce(intFn fn, int size, int *elms)
|
||||
{
|
||||
int i, val = *elms;
|
||||
for (i = 1; i < size; ++i)
|
||||
val = fn(val, elms[i]);
|
||||
return val;
|
||||
}
|
||||
|
||||
int add(int a, int b) { return a + b; }
|
||||
int sub(int a, int b) { return a - b; }
|
||||
int mul(int a, int b) { return a * b; }
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int nums[] = {1, 2, 3, 4, 5};
|
||||
printf("%d\n", reduce(add, 5, nums));
|
||||
printf("%d\n", reduce(sub, 5, nums));
|
||||
printf("%d\n", reduce(mul, 5, nums));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue