From 56d2832632f0f7a15fc937b72519db9ef2550bf5 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 12 Apr 2022 23:26:45 +0400 Subject: [PATCH 1/2] iterative implementation of the int fib(int n) function for calculating the 10th Fibonacci number --- main.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index fba3347..0d47b60 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,25 @@ #include +#include + +int fib(int n) { + if (n <= 0) return -1; + if (n == 1) return 0; + if (n == 2) return -1; + + int a = 0, b = 1; + for (int i = 3; i <= n; i++) { + int c = a + b; + a = b; + b = c; + } + return b; +} + int main() { std::cout << "Hello, world!" << std::endl; + std::cout << fib(10) << std::endl; + return 0; -} +} \ No newline at end of file From 9c88d09edf6aeade8c84dc70f25b0363cad44fb6 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 12 Apr 2022 23:31:28 +0400 Subject: [PATCH 2/2] added void bubble(std) function::vector &v) with bubble sorting implementation --- .vscode/settings.json | 5 +++++ main.cpp | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e2c18d1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "ostream": "cpp" + } +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 0d47b60..c85b613 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include int fib(int n) { if (n <= 0) return -1; @@ -15,11 +16,30 @@ int fib(int n) { return b; } +void bubble(std::vector &v) { + for (int i = 0; i != v.size(); ++i) { + for (int j = v.size() - 1; j != i; --j) {; + if (v[j - 1] > v[j]) + std::swap(v[j - 1], v[j]); + } + } +} + int main() { std::cout << "Hello, world!" << std::endl; std::cout << fib(10) << std::endl; + std::vector vec = {6, 2, 1, 4, 5, 6, 9, 12, 3, 1}; + for (int i = 0; i != vec.size(); ++i) { + std::cout << vec[i] << " "; + } + std::cout << std::endl; + bubble(vec); + for (int i = 0; i != vec.size(); ++i) { + std::cout << vec[i] << " "; + } + std::cout << std::endl; return 0; } \ No newline at end of file