diff options
Added my solutions to Sem. 03 exercises
14 files changed, 294 insertions, 0 deletions
diff --git a/Year 1/Programming introduction/Solutions Sem. 03/01. Digit_Sum.cpp b/Year 1/Programming introduction/Solutions Sem. 03/01. Digit_Sum.cpp new file mode 100644 index 0000000..7bedf7f --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/01. Digit_Sum.cpp @@ -0,0 +1,14 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n = 1, sum = 0; + while (n != 0) + { + cin >> n; + sum += n; + } + cout << sum << endl; +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/02. Sorted_Common_Divisors.cpp b/Year 1/Programming introduction/Solutions Sem. 03/02. Sorted_Common_Divisors.cpp new file mode 100644 index 0000000..75af7ea --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/02. Sorted_Common_Divisors.cpp @@ -0,0 +1,17 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n, k; + cin >> n >> k; + + // i = smaller number + for (int i = (n < k) ? n : k; i > 0; i--) + { + if (n % i == 0 && k % i == 0) + cout << i << " "; + } + cout << endl; +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp b/Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp new file mode 100644 index 0000000..c22a028 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp @@ -0,0 +1,25 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int digits = 0; + for (int i = n; i != 0; i /= 10) + digits++; + + int digitsToTakeFromEachEnd = digits / 2 + (digits % 2 - 1); + int tenToTheHalf = 1; // ten to the power of digits to take from each end + + for (int i = 0; i < digitsToTakeFromEachEnd; i++) + tenToTheHalf *= 10; + + int tenToTheMiddle = (digits % 2 == 0) ? 100 : 10; // ten to the power of digits to remove from the middle + + int resultNumber = (n % tenToTheHalf) + (n / tenToTheHalf / tenToTheMiddle) * tenToTheHalf; + + cout << resultNumber << ", " << resultNumber + 1 << endl; +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/04. Primes_In_Interval.cpp b/Year 1/Programming introduction/Solutions Sem. 03/04. Primes_In_Interval.cpp new file mode 100644 index 0000000..a1510da --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/04. Primes_In_Interval.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +int main() +{ + int a, b; + cin >> a >> b; + + if (a < 2) a = 2; + + for (int i = a; i <= b; i++) + { + bool isPrime = true; + int rootOfI = sqrt(i); + for (int j = 2; j <= rootOfI; j++) + { + if (i % j == 0) + { + isPrime = false; + break; + } + } + + if (isPrime) + cout << i << " "; + } + cout << endl; +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/05. Power_Of_Two.cpp b/Year 1/Programming introduction/Solutions Sem. 03/05. Power_Of_Two.cpp new file mode 100644 index 0000000..0a5c88f --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/05. Power_Of_Two.cpp @@ -0,0 +1,14 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + while (n % 2 == 0 && n > 1) + n /= 2; + + cout << ((n == 1) ? "true" : "false") << endl; +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/06. Polynomial_Solutions.cpp b/Year 1/Programming introduction/Solutions Sem. 03/06. Polynomial_Solutions.cpp new file mode 100644 index 0000000..ba34792 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/06. Polynomial_Solutions.cpp @@ -0,0 +1,18 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int a, b, c, d, e; + cin >> a >> b >> c >> d >> e; + + double fx; + for (int x = -100; x <= 100; x++) + { + fx = a * x*x*x*x + b * x*x*x + c * x*x + d * x + e; + if (fx >= 0) + cout << x << " "; + } + cout << endl; +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/08. Triangle.cpp b/Year 1/Programming introduction/Solutions Sem. 03/08. Triangle.cpp new file mode 100644 index 0000000..fa2b501 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/08. Triangle.cpp @@ -0,0 +1,17 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for (int row = 1; row <= n; row++) + { + for (int col = 0; col < row; col++) + cout << "$ "; + + cout << endl; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/09. Lefthand_Triangle.cpp b/Year 1/Programming introduction/Solutions Sem. 03/09. Lefthand_Triangle.cpp new file mode 100644 index 0000000..1aba6c7 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/09. Lefthand_Triangle.cpp @@ -0,0 +1,24 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for (int row = -n + 1; row < n; row++) + { + int absRow = ((row > 0) ? row : -row); + + for (int col = absRow; col > 0; col--) + cout << " "; + + int numberOnLine = 0; + + for (int col = n - absRow; col > 0; col--) + (numberOnLine *= 10) += col; + + cout << numberOnLine << endl; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/10. Spiral_Numbers.cpp b/Year 1/Programming introduction/Solutions Sem. 03/10. Spiral_Numbers.cpp new file mode 100644 index 0000000..66e5e70 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/10. Spiral_Numbers.cpp @@ -0,0 +1,24 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for (int row = 0; row < n; row++) + { + int previous = row + 1; + for (int col = 0; col < n; col++) + { + if (col == 0) + cout << previous << " "; + else if (col % 2 == 0) + cout << (previous += row * 2 + 1) << " "; + else + cout << (previous += (n -row -1) * 2 + 1) << " "; + } + cout << endl; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp b/Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp new file mode 100644 index 0000000..b7337a4 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp @@ -0,0 +1,30 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for (int row = 0, previous = 0; row < n; row++) + { + int modifier = 1; + + // When going from right to left, make previous be the number at the end of the line + 1, and then decrement + if (row % 2 == 1) + { + previous += n + 1; + modifier = -1; + } + + for (int col = 0; col < n; col++) + cout << (previous += modifier) << " "; + + cout << endl; + + // Account for decrementing when gong from right to left + if (row % 2 == 1) + previous += n - 1; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/12. Frame.cpp b/Year 1/Programming introduction/Solutions Sem. 03/12. Frame.cpp new file mode 100644 index 0000000..6eca5a2 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/12. Frame.cpp @@ -0,0 +1,21 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + for (int row = 0; row < n; row++) + { + bool onFirstOrLastLine = (row == 0 || row == n-1); + + cout << ((onFirstOrLastLine) ? "+ " : "| "); + + for (int col = 0; col < n - 2; col++) + cout << "- "; + + cout << ((onFirstOrLastLine) ? "+" : "|") << endl; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/13. Dumbbell.cpp b/Year 1/Programming introduction/Solutions Sem. 03/13. Dumbbell.cpp new file mode 100644 index 0000000..7db650a --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/13. Dumbbell.cpp @@ -0,0 +1,26 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int rowWidth = 3 * n - 2; + + for (int row = 0; row < n; row++) + { + if (row == 0 || row == n - 1) + { + for (int col = 0; col < rowWidth; col++) + cout << ((col >= n && col <= rowWidth - n - 1) ? " " : "."); + } + else + { + for (int col = 0; col < rowWidth; col++) + cout << (((col >= 1 && col <= n - 2) || (col >= rowWidth - n + 1 && col <= rowWidth - 2)) ? " " : "."); + } + cout << endl; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/14. Tree.cpp b/Year 1/Programming introduction/Solutions Sem. 03/14. Tree.cpp new file mode 100644 index 0000000..1955730 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/14. Tree.cpp @@ -0,0 +1,30 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + // Tree crown + for (int row = 1; row <= n; row++) + { + for (int col = n - row; col > 0; col--) + cout << " "; + + for (int col = -row + 1; col < row; col++) + cout << row - ((col < 0) ? -col : col); + + cout << endl; + } + + // Tree trunk + for (int row = n - 1; row > 0; row--) + { + for (int col = n - 1; col > 0; col--) + cout << " "; + + cout << row << endl; + } +} diff --git a/Year 1/Programming introduction/Solutions Sem. 03/README.md b/Year 1/Programming introduction/Solutions Sem. 03/README.md new file mode 100644 index 0000000..f118bc0 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 03/README.md @@ -0,0 +1,4 @@ +My solutions to all of the exercises from Seminar 03. + +Task descriptions can be found [here](https://github.com/Angeld55/Introduction_to_programming_FMI/tree/main/Sem.%2003#%D0%B7%D0%B0%D0%B4%D0%B0%D1%87%D0%B8) +My pull request with those solution can be found [here](https://github.com/Angeld55/Introduction_to_programming_FMI/pull/10) |
