Website Summary Files Commits Branches @commit @diff @stats
path: root / Week13 / Exercise10.cpp
diff options
context:
space:
mode:
1 files changed, 23 insertions, 0 deletions
-rw-r--r--Week13/Exercise10.cpp23
diff --git a/Week13/Exercise10.cpp b/Week13/Exercise10.cpp
new file mode 100644
index 0000000..5d71e4f
--- /dev/null
+++ b/Week13/Exercise10.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+
+double sum(double totalsum, const double* elems, unsigned size) {
+ if (size == 0)
+ return totalsum;
+ else
+ return sum(totalsum + *elems, elems + 1, size - 1);
+}
+
+int main() {
+ unsigned size;
+ std::cin >> size;
+
+ double* arr = new double[size];
+ for (unsigned i = 0; i < size; ++i)
+ std::cin >> arr[i];
+
+ std::cout << sum(0.0, arr, size) << std::endl;
+
+ free(arr);
+
+ return 0;
+}