Website Summary Files Commits Branches @commit @diff @stats
path: root / Week07
diff options
context:
space:
mode:
Commit3e8ac7ba4a8f410703aa5a8426f542217adcbf2f (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-11-14 20:55:42 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-11-14 20:55:42 +0200
Files85d854d449924ef1b88ddb4e5e3f37fb2a68299f /Week07
upp-2025-solutions-3e8ac7ba4a8f410703aa5a8426f542217adcbf2f.tar
upp-2025-solutions-3e8ac7ba4a8f410703aa5a8426f542217adcbf2f.tar.gz
upp-2025-solutions-3e8ac7ba4a8f410703aa5a8426f542217adcbf2f.zip
Parentec8ecd4dc19854007428761c330303854478201f (diff)
feat(Week07): Add solutions
16 files changed, 404 insertions, 0 deletions
-rw-r--r--Week07/Exercise01.cpp16
-rw-r--r--Week07/Exercise02.cpp29
-rw-r--r--Week07/Exercise03.cpp22
-rw-r--r--Week07/Exercise04.cpp19
-rw-r--r--Week07/Exercise05.cpp27
-rw-r--r--Week07/Exercise06.cpp30
-rw-r--r--Week07/Exercise07.cpp25
-rw-r--r--Week07/Exercise08.cpp32
-rw-r--r--Week07/Exercise09.cpp29
-rw-r--r--Week07/Exercise10.cpp26
-rw-r--r--Week07/Exercise11.cpp24
-rw-r--r--Week07/Exercise12.cpp29
-rw-r--r--Week07/Exercise13.cpp30
-rw-r--r--Week07/Exercise14.cpp23
-rw-r--r--Week07/Exercise15.cpp40
-rw-r--r--Week07/README.md3
diff --git a/Week07/Exercise01.cpp b/Week07/Exercise01.cpp
new file mode 100644
index 0000000..b76a81c
--- /dev/null
+++ b/Week07/Exercise01.cpp
@@ -0,0 +1,16 @@
+#include <iostream>
+
+// Името на функция трябва да бъде налично преди да използвате функцията.
+// Това означава дефиницията (самата имплементация) и/или декларацията
+// на функцията да бъде създадена преди нейната употреба.
+bool evenAndPositive(int x) {
+ return (x % 2 == 0) && x > 0;
+}
+
+int main() {
+ int number;
+ std::cin >> number;
+ std::cout << evenAndPositive(number) << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise02.cpp b/Week07/Exercise02.cpp
new file mode 100644
index 0000000..7074353
--- /dev/null
+++ b/Week07/Exercise02.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+
+// Това е пример за декларация на функция: просто описваме функцията
+// как ще изглежда. Това ни позволява да сложим самата имплементация
+// някъде другаде.
+// Има специални механизми, чрез които самата дефиниция/имплементация
+// се "свързва" навсякъде където я използваме. За това в бъдеще.
+unsigned int fib(unsigned int n);
+
+int main() {
+ unsigned int num;
+ std::cin >> num;
+ std::cout << fib(num) << std::endl;
+
+ return 0;
+}
+
+// Това е дефиницията/имплементацията на функцията.
+unsigned int fib(unsigned int n) {
+ unsigned int f0 = 0, f1 = 1;
+ while (n > 0) {
+ unsigned int temp = f0;
+ f0 = f1;
+ f1 += temp;
+
+ n--;
+ }
+ return f0;
+}
diff --git a/Week07/Exercise03.cpp b/Week07/Exercise03.cpp
new file mode 100644
index 0000000..71342d9
--- /dev/null
+++ b/Week07/Exercise03.cpp
@@ -0,0 +1,22 @@
+#include <iostream>
+
+const unsigned int SIZE = 15;
+
+double average(int numbers[SIZE]) {
+ double sum = 0;
+
+ for (unsigned int i = 0; i < SIZE; i++)
+ sum += numbers[i];
+
+ return sum / SIZE;
+}
+
+int main() {
+ int input[SIZE];
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cin >> input[i];
+
+ std::cout << average(input) << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise04.cpp b/Week07/Exercise04.cpp
new file mode 100644
index 0000000..931931d
--- /dev/null
+++ b/Week07/Exercise04.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+
+void calc(float number, double &half, int* integer) {
+ half = number / 2.0;
+ *integer = (int)number;
+}
+
+int main() {
+ float input;
+ std::cin >> input;
+
+ double a;
+ int b;
+ calc(input, a, &b);
+
+ std::cout << a << ' ' << b << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise05.cpp b/Week07/Exercise05.cpp
new file mode 100644
index 0000000..71494a2
--- /dev/null
+++ b/Week07/Exercise05.cpp
@@ -0,0 +1,27 @@
+#include <iostream>
+
+// Добра практика е, когато приемаме указатели, от които само четем стойности,
+// да ги направим константни. В близкото бъдеще това ще стане строго ограничение.
+double maxElem(const double* numbers, unsigned int size) {
+ double max = *numbers; // еквивалентно на numbers[0], защото numbers[0] се свежда до *(numbers + 0)
+
+ for (unsigned int i = 0; i < size; i++)
+ if (numbers[i] > max) // numbers[i] е еквивалентно на *(numbers + i)
+ max = numbers[i];
+
+ return max;
+}
+
+int main() {
+ const unsigned int SIZE1 = 5, SIZE2 = 7;
+ double series1[SIZE1], series2[SIZE2];
+
+ for (unsigned int i = 0; i < SIZE1; i++)
+ std::cin >> series1[i];
+ for (unsigned int i = 0; i < SIZE2; i++)
+ std::cin >> series2[i];
+
+ std::cout << maxElem(series1, SIZE1) << ' ' << maxElem(series2, SIZE2) << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise06.cpp b/Week07/Exercise06.cpp
new file mode 100644
index 0000000..e64c886
--- /dev/null
+++ b/Week07/Exercise06.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+
+// Припомняме, че "unsigned" е същото като "unsigned int"
+void minmaxElems(int* arr, unsigned size, int* min, int* max) {
+ *min = arr[0];
+ *max = arr[0];
+ for (unsigned int i = 1; i < size; i++) {
+ if (arr[i] > *max) *max = arr[i];
+ if (arr[i] < *min) *min = arr[i];
+ }
+}
+
+int main() {
+ const unsigned int SIZE1 = 10, SIZE2 = 8;
+ int series1[SIZE1], series2[SIZE2];
+
+ for (unsigned int i = 0; i < SIZE1; i++)
+ std::cin >> series1[i];
+ for (unsigned int i = 0; i < SIZE2; i++)
+ std::cin >> series2[i];
+
+ int min, max;
+ minmaxElems(series1, SIZE1, &min, &max);
+ std::cout << max << ' ' << min << std::endl;
+
+ minmaxElems(series2, SIZE2, &min, &max);
+ std::cout << max << ' ' << min << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise07.cpp b/Week07/Exercise07.cpp
new file mode 100644
index 0000000..6bb2a8c
--- /dev/null
+++ b/Week07/Exercise07.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+
+// Припомняме, че "unsigned" е същото като "unsigned int"
+unsigned sumElems(int* arr, unsigned size) {
+ int sum = 0;
+ for (unsigned int i = 0; i < size; i++)
+ sum += arr[i];
+ return sum;
+}
+
+int main() {
+ const unsigned int SIZE = 11;
+ int input[SIZE];
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cin >> input[i];
+
+ for (unsigned int i = 0; i <= SIZE - 3; i++) {
+ if (sumElems(input + i, 3) == 0) {
+ std::cout << input[i] << ' ' << input[i + 1] << ' ' << input[i + 2] << std::endl;
+ return 0;
+ }
+ }
+
+ return 0;
+}
diff --git a/Week07/Exercise08.cpp b/Week07/Exercise08.cpp
new file mode 100644
index 0000000..375d1d4
--- /dev/null
+++ b/Week07/Exercise08.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+
+void rotate(const unsigned int* input, unsigned int size, unsigned int rotation, unsigned int* output) {
+ rotation %= size;
+
+ for (unsigned int i = 0; i < size; i++)
+ output[(i + rotation) % size] = input[i];
+}
+
+// Никой не е забранил да правим допълнителни функции.
+// ВНИМАНИЕ: входния размер ТРЯБВА да бъде константа!
+void rotateUserInput(const unsigned int SIZE) {
+ unsigned int series[SIZE], rotation;
+
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cin >> series[i];
+ std::cin >> rotation;
+
+ unsigned int result[SIZE];
+ rotate(series, SIZE, rotation, result);
+
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cout << result[i] << ' ';
+ std::cout << std::endl;
+}
+
+int main() {
+ rotateUserInput(6);
+ rotateUserInput(7);
+
+ return 0;
+}
diff --git a/Week07/Exercise09.cpp b/Week07/Exercise09.cpp
new file mode 100644
index 0000000..d68b427
--- /dev/null
+++ b/Week07/Exercise09.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+
+double topower(double x, unsigned int a) {
+ double result = 1.0;
+ for (unsigned int i = 1; i <= a; i++)
+ result *= x;
+ return result;
+}
+
+int main() {
+ const unsigned int SIZE = 11;
+ double coeffs[SIZE], root;
+
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cin >> coeffs[i];
+ std::cin >> root;
+
+ double sum = 0.0;
+ for (unsigned int i = 0; i < SIZE; i++)
+ sum += coeffs[i] * topower(root, 10 - i);
+
+ const double ZERO_BORDER = 0.0001;
+ if (-ZERO_BORDER < sum && sum < ZERO_BORDER)
+ std::cout << "Is a root" << std::endl;
+ else
+ std::cout << "Not a root" << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise10.cpp b/Week07/Exercise10.cpp
new file mode 100644
index 0000000..f31d4b7
--- /dev/null
+++ b/Week07/Exercise10.cpp
@@ -0,0 +1,26 @@
+#include <iostream>
+
+unsigned int countSameElementsOnSameIndecies(int* arr1, unsigned int size1, int* arr2, unsigned int size2) {
+ unsigned int count = 0;
+ unsigned int size = size1 < size2 ? size1 : size2;
+
+ for (unsigned int i = 0; i < size; i++)
+ if (arr1[i] == arr2[i])
+ count++;
+
+ return count;
+}
+
+int main() {
+ const unsigned int SIZE1 = 10, SIZE2 = 15;
+ int series1[SIZE1], series2[SIZE2];
+
+ for (unsigned int i = 0; i < SIZE1; i++)
+ std::cin >> series1[i];
+ for (unsigned int i = 0; i < SIZE2; i++)
+ std::cin >> series2[i];
+
+ std::cout << countSameElementsOnSameIndecies(series1, SIZE1, series2, SIZE2) << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise11.cpp b/Week07/Exercise11.cpp
new file mode 100644
index 0000000..5e5c3e4
--- /dev/null
+++ b/Week07/Exercise11.cpp
@@ -0,0 +1,24 @@
+#include <iostream>
+
+bool isPrime(unsigned int x) {
+ for (unsigned int div = 2; div < x; div++) {
+ if (x % div == 0)
+ return false;
+ }
+ return x > 1;
+}
+
+int main() {
+ unsigned int a, b;
+ std::cin >> a >> b;
+
+ unsigned int count = 0;
+ while (a <= b) {
+ count += isPrime(a);
+ a++;
+ }
+
+ std::cout << count << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise12.cpp b/Week07/Exercise12.cpp
new file mode 100644
index 0000000..9fdfd05
--- /dev/null
+++ b/Week07/Exercise12.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+
+void incArr(int* arr, unsigned int size) {
+ for (unsigned int i = 0; i < size; i++)
+ arr[i]++;
+}
+
+int main() {
+ const unsigned int SIZE1 = 10, SIZE2 = 9;
+ int series1[SIZE1], series2[SIZE2];
+
+ for (unsigned int i = 0; i < SIZE1; i++)
+ std::cin >> series1[i];
+ for (unsigned int i = 0; i < SIZE2; i++)
+ std::cin >> series2[i];
+
+ incArr(series1, SIZE1);
+ incArr(series2, SIZE2);
+
+ for (unsigned int i = 0; i < SIZE1; i++)
+ std::cout << series1[i] << ' ';
+ std::cout << std::endl;
+
+ for (unsigned int i = 0; i < SIZE2; i++)
+ std::cout << series2[i] << ' ';
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise13.cpp b/Week07/Exercise13.cpp
new file mode 100644
index 0000000..95115f9
--- /dev/null
+++ b/Week07/Exercise13.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+
+bool isDigit(char x) {
+ return '0' <= x && x <= '9';
+}
+
+bool isLetter(char x) {
+ return ('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z');
+}
+
+bool followsPattern(char letters[4]) {
+ return isLetter(letters[0]) && isDigit(letters[1]) && isLetter(letters[2]) && isDigit(letters[3]);
+}
+
+int main() {
+ const unsigned int SIZE = 12;
+ char input[SIZE];
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cin >> input[i];
+
+ for (unsigned int i = 0; i < SIZE; i += 4) {
+ if (!followsPattern(input)) {
+ std::cout << "Does not follow pattern" << std::endl;
+ return 0;
+ }
+ }
+
+ std::cout << "Follows pattern" << std::endl;
+ return 0;
+}
diff --git a/Week07/Exercise14.cpp b/Week07/Exercise14.cpp
new file mode 100644
index 0000000..9556520
--- /dev/null
+++ b/Week07/Exercise14.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+
+const unsigned int SIZE_INPUT = 10, SIZE_OUTPUT = 5;
+
+void subtractions(const double input[SIZE_INPUT], double output[SIZE_OUTPUT]) {
+ for (unsigned int i = 0; i < SIZE_OUTPUT; i++)
+ output[i] = input[i * 2 + 1] - input[i * 2];
+}
+
+int main() {
+ double in[SIZE_INPUT], out[SIZE_OUTPUT];
+
+ for (unsigned int i = 0; i < SIZE_INPUT; i++)
+ std::cin >> in[i];
+
+ subtractions(in, out);
+
+ for (unsigned int i = 0; i < SIZE_OUTPUT; i++)
+ std::cout << out[i] << ' ';
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/Week07/Exercise15.cpp b/Week07/Exercise15.cpp
new file mode 100644
index 0000000..de89a87
--- /dev/null
+++ b/Week07/Exercise15.cpp
@@ -0,0 +1,40 @@
+#include <iostream>
+
+void pow2(unsigned long long* arr, unsigned int size) {
+ for (unsigned int i = 0; i < size; i++)
+ arr[i] *= arr[i];
+}
+
+void mult3(unsigned long long* arr, unsigned int size) {
+ for (unsigned int i = 0; i < size; i++)
+ arr[i] *= 3;
+}
+
+bool contains(unsigned long long* arr, unsigned int size, unsigned long long element) {
+ for (unsigned int i = 0; i < size; i++)
+ if (arr[i] == element)
+ return true;
+ return false;
+}
+
+int main() {
+ const unsigned long long SIZE = 5;
+
+ unsigned long long input[SIZE], goal;
+ for (unsigned int i = 0; i < SIZE; i++)
+ std::cin >> input[i];
+ std::cin >> goal;
+
+ for (unsigned long long i = 0; !contains(input, SIZE, goal); i++) {
+ if (i % 2 == 0)
+ pow2(input, SIZE);
+ else
+ mult3(input, SIZE);
+ }
+
+ for (unsigned long long i = 0; i < SIZE; i++)
+ std::cout << input[i] << ' ';
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/Week07/README.md b/Week07/README.md
new file mode 100644
index 0000000..d3cb9a8
--- /dev/null
+++ b/Week07/README.md
@@ -0,0 +1,3 @@
+# Задачи
+
+<https://syndamia.com/teaching/upp-2025/week07/>