Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 13 / high_order_functions.cpp
blob: c6da6c7497b4d71a1e409f5cc72f106712919909 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

int get_something_from_array(const int* arr, size_t size, int result, int (*func)(int,int) )
{
	if (size == 0)
		return result;
	return get_something_from_array(arr + 1, size - 1, func(result, *arr), func);
}

int max(int a, int b)
{
	return a > b ? a : b;
}
int getMaxFromArray(const int* arr, size_t size)
{
	return get_something_from_array(arr, size, INT_MIN, max);
}
int sum(int x, int y)
{
	return x + y;
}
int getSumOnArray(const int* arr, size_t size)
{
	return get_something_from_array(arr, size, 0, sum);
}



int main()
{
	const size_t SIZE = 4;
	int arr[SIZE] = { 1, 2, 3, 4 };

	std::cout << getSumOnArray(arr, SIZE) << std::endl << getMaxFromArray(arr, SIZE) << std::endl;
}