Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 13 / high_order_functions.cpp
diff options
context:
space:
mode:
Commit171df5fbafac3853e884f5fab370cb98b4e76694 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-01-12 19:49:26 +0200
Committer
 GitHub <noreply@github.com>
2022-01-12 19:49:26 +0200
Files827a0d287f07a3a281cf5e00e5f0adec922a9846 /Sem. 13/high_order_functions.cpp
Introduction_to_programming_FMI-171df5fbafac3853e884f5fab370cb98b4e76694.tar
Introduction_to_programming_FMI-171df5fbafac3853e884f5fab370cb98b4e76694.tar.gz
Introduction_to_programming_FMI-171df5fbafac3853e884f5fab370cb98b4e76694.zip
Parenteee704529b63d39baee5429049854f6c3b2d951b (diff)
Create high_order_functions.cpp
1 files changed, 35 insertions, 0 deletions
-rw-r--r--Sem. 13/high_order_functions.cpp35
diff --git a/Sem. 13/high_order_functions.cpp b/Sem. 13/high_order_functions.cpp
new file mode 100644
index 0000000..c6da6c7
--- /dev/null
+++ b/Sem. 13/high_order_functions.cpp
@@ -0,0 +1,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;
+}