Website Summary Files Commits Branches @commit @diff @stats
path: root / Week01 / Exercise02
diff options
context:
space:
mode:
Commitfcf38e62e8160f2c9901bdfd33760afed28beb8d (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-02-22 19:18:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-02-22 19:18:46 +0200
Files838fd5897d88700491a389cdf6837e35da3347ae /Week01/Exercise02
oop-2025-solutions-fcf38e62e8160f2c9901bdfd33760afed28beb8d.tar
oop-2025-solutions-fcf38e62e8160f2c9901bdfd33760afed28beb8d.tar.gz
oop-2025-solutions-fcf38e62e8160f2c9901bdfd33760afed28beb8d.zip
Parentc365ede952f852c6e8feb92fc3a0766186f989f1 (diff)
feat(Week01): Add solutions
3 files changed, 98 insertions, 0 deletions
-rw-r--r--Week01/Exercise02/arrmath.cpp57
-rw-r--r--Week01/Exercise02/arrmath.h15
-rw-r--r--Week01/Exercise02/main.cpp26
diff --git a/Week01/Exercise02/arrmath.cpp b/Week01/Exercise02/arrmath.cpp
new file mode 100644
index 0000000..d518134
--- /dev/null
+++ b/Week01/Exercise02/arrmath.cpp
@@ -0,0 +1,57 @@
+#include "arrmath.h"
+
+int sum(const int* elements, unsigned length) {
+ if (elements == nullptr || length == 0)
+ return 0;
+
+ int sum = 0;
+
+ for (unsigned i = 0; i < length; ++i)
+ sum += elements[i];
+
+ return sum;
+}
+
+double average(const int* elements, unsigned length) {
+ if (elements == nullptr || length == 0)
+ return 0.0;
+
+ return (double)sum(elements, length) / length;
+}
+
+int largest(const int* elements, unsigned length) {
+ if (elements == nullptr || length == 0)
+ return 0;
+
+ int largest = elements[0];
+ for (unsigned i = 1; i < length; ++i)
+ if (elements[i] > largest)
+ largest = elements[i];
+
+ return largest;
+}
+
+int smallest(const int* elements, unsigned length) {
+ if (elements == nullptr || length == 0)
+ return 0;
+
+ int smallest = elements[0];
+ for (unsigned i = 1; i < length; ++i)
+ if (elements[i] < smallest)
+ smallest = elements[i];
+
+ return smallest;
+}
+
+unsigned findIndex(const int* elements, unsigned length, int elementToSearch) {
+ if (elements == nullptr || length == 0)
+ return length;
+
+ for (unsigned i = 0; i < length; ++i)
+ if (elements[i] == elementToSearch)
+ return i;
+
+ // Коректно е да връщаме length вместо 0, защото
+ // 0 може да е валиден индекс в масива с елементи
+ return length;
+}
diff --git a/Week01/Exercise02/arrmath.h b/Week01/Exercise02/arrmath.h
new file mode 100644
index 0000000..4f1a550
--- /dev/null
+++ b/Week01/Exercise02/arrmath.h
@@ -0,0 +1,15 @@
+#ifndef ARRMATH
+#define ARRMATH
+
+// Сума на стойностите в elements
+int sum(const int* elements, unsigned length);
+// Средно аритметично на стойностите в elements
+double average(const int* elements, unsigned length);
+// Най-голямата стойност в elements
+int largest(const int* elements, unsigned length);
+// Най-малката стойност в elements
+int smallest(const int* elements, unsigned length);
+// Връща индекса на първото срещане на elementToSearch
+unsigned findIndex(const int* elements, unsigned length, int elementToSearch);
+
+#endif
diff --git a/Week01/Exercise02/main.cpp b/Week01/Exercise02/main.cpp
new file mode 100644
index 0000000..9e3c593
--- /dev/null
+++ b/Week01/Exercise02/main.cpp
@@ -0,0 +1,26 @@
+#include <iostream>
+#include "arrmath.h"
+
+int main() {
+ unsigned N;
+ std::cin >> N;
+
+ int* arr = new int[N];
+ for (unsigned i = 0; i < N; ++i)
+ std::cin >> arr[i];
+
+ int startIndex = findIndex(arr, N, smallest(arr, N));
+ int endIndex = findIndex(arr, N, largest(arr, N));
+
+ if (startIndex > endIndex) {
+ int temp = startIndex;
+ startIndex = endIndex;
+ endIndex = temp;
+ }
+
+ std::cout << average(arr + startIndex, endIndex - startIndex + 1) << std::endl;
+
+ delete[] arr;
+
+ return 0;
+}