Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit385c605fe634e7294a33aa5162c608e5f4656ca9 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-01-12 19:50:31 +0200
Committer
 GitHub <noreply@github.com>
2022-01-12 19:50:31 +0200
Files98caefabdd2781f60d2097050733c22d6951fdd7
Introduction_to_programming_FMI-385c605fe634e7294a33aa5162c608e5f4656ca9.tar
Introduction_to_programming_FMI-385c605fe634e7294a33aa5162c608e5f4656ca9.tar.gz
Introduction_to_programming_FMI-385c605fe634e7294a33aa5162c608e5f4656ca9.zip
Parent171df5fbafac3853e884f5fab370cb98b4e76694 (diff)
Create max_rec.cpp
1 files changed, 39 insertions, 0 deletions
-rw-r--r--Sem. 13/max_rec.cpp39
diff --git a/Sem. 13/max_rec.cpp b/Sem. 13/max_rec.cpp
new file mode 100644
index 0000000..19c1b9e
--- /dev/null
+++ b/Sem. 13/max_rec.cpp
@@ -0,0 +1,39 @@
+#include <iostream>
+
+
+//рекурсивна функция, която намира най-големия елемент в масив
+
+int max(int a, int b)
+{
+ return a > b ? a : b;
+}
+
+int getMaxRec(const int* arr, size_t size)
+{
+ if (size == 0)
+ return INT_MIN;
+
+ return max(*arr, getMaxRec(arr + 1, size - 1));
+}
+
+int get_max_rec_tail(const int* arr, size_t size, int currentMax)
+{
+ if (size == 0)
+ return currentMax;
+
+ return get_max_rec_tail(arr + 1, size - 1, max(currentMax, *arr));
+}
+
+int get_max_rec_wrapper(const int* arr, size_t size)
+{
+ return get_max_rec_tail(arr, size, INT_MIN);
+}
+
+
+int main()
+{
+ const size_t SIZE = 4;
+ int arr[SIZE] = { 1, 2, 3, 4 };
+
+ std::cout << get_max_rec_wrapper(arr, SIZE);
+}