diff options
Merge branch 'Angeld55:main' into sem03-solutions
57 files changed, 1191 insertions, 5 deletions
@@ -3,6 +3,7 @@ - **Тема 1:** Увод. Примитивни типове данни. Преобразуване между типове. Оператори. - **Тема 2:** Условни конструкции - if, switch и тернарен оператор. - **Тема 3:** Цикли. Оператори break и continue. + - **Тема 4:** Функции. Оператор return. ### Практикум: diff --git a/Sem. 03/README.md b/Sem. 03/README.md index 6fcb143..35624d4 100644 --- a/Sem. 03/README.md +++ b/Sem. 03/README.md @@ -67,11 +67,11 @@ do **Задача 2:** Да се напише програма, която приема 2 числа. Да се отпечатат всички общи делители сортиран наобратно. -*Вход: 28 21, Изход: 7 3 1* +*Вход: 28 21, Изход: 7 1* -*Вход: 60 30 Изход: 30 15 10 5 3 1* +*Вход: 60 30 Изход: 30 15 10 6 5 3 2 1* -*Вход: 5 4 3 0 Изход: 12* +*Вход: 19 17 Изход: 1* **Задача 3:** Да се напише програма, която приема число и премахва среднaта цифра , ако числото има нечетен брой цифри или премахва средните две цифри, ако числото има четен брой цифри. След това да се отпечата числото увеличено с 1-ца. @@ -112,6 +112,7 @@ do *Вход: 0 0 -1 5 -6 Изход: 2 3* +**!!** Задачите надолу са за рисуване на конзолата. Във файла *drawing_examples.txt* има изходите като текст.**!!** **Задача 8:** Да се напише програма, която приема цяло положително число n и отпечатва следната пирамида от доларчета @@ -159,5 +160,7 @@ do +**Задача 15:** Да се напише програма, която приема цяло положително число n и отпечатва следната папионка: + diff --git a/Sem. 03/drawing_examples.txt b/Sem. 03/drawing_examples.txt index 5179a0f..73e3130 100644 --- a/Sem. 03/drawing_examples.txt +++ b/Sem. 03/drawing_examples.txt @@ -63,7 +63,7 @@ $ $ $ $ задача 11:
-вход:9
+вход:3
1 2 3
6 5 4
7 8 9
@@ -120,6 +120,13 @@ $ $ $ $ . . . . .
. . . . . .
+вход: 5
+. . . . . . . . . .
+. . . .
+. . . . . . .
+. . . .
+. . . . . . . . . .
+
@@ -153,3 +160,31 @@ $ $ $ $ 1
121
1
+
+задача 15:
+
+вход: 3
+
+1 1
+1 2 2 1
+1 2 3 --- 3 2 1
+1 2 2 1
+1 1
+
+вход: 4
+1 1
+1 2 2 1
+1 2 3 3 2 1
+1 2 3 4 ---- 4 3 2 1
+1 2 3 3 2 1
+1 2 2 1
+1 1
+
+
+вход: 2
+
+1 1
+1 2 –- 2 1
+1 1
+
+
diff --git a/Sem. 03/solutions/exercise 08 - dollar pyramid.cpp b/Sem. 03/solutions/exercise 08 - dollar pyramid.cpp new file mode 100644 index 0000000..1ea164d --- /dev/null +++ b/Sem. 03/solutions/exercise 08 - dollar pyramid.cpp @@ -0,0 +1,19 @@ +#include <iostream> +using namespace std; + +int main() +{ + + int n; + cin >> n; + + for(int i = 1; i <= n; i++) + { + for(int j = 1; j <= i; j++) + cout << "$" << " "; // print as many dollars as the number of the current row + + cout << endl; + } + + return 0; +} diff --git a/Sem. 03/solutions/exercise 09 - rightmost pyramid.cpp b/Sem. 03/solutions/exercise 09 - rightmost pyramid.cpp new file mode 100644 index 0000000..573b98b --- /dev/null +++ b/Sem. 03/solutions/exercise 09 - rightmost pyramid.cpp @@ -0,0 +1,36 @@ +#include <iostream> +using namespace std; + +int main() +{ + + int n; + cin >> n; + + // print the first part of the pyramid (the one going from 1 to n(n-1)...21) + + for(int i = 1; i <= n; i++) + { + for(int k = n - i - 1; k >= 0; k--) + cout << " "; // print the spaces + + for(int j = i; j >= 1; j--) + cout << j; // print numbers j(j - 1)...21 + + cout << endl; + } + // print the second part of the pyramid (the one going from (n-1)...21 to 1 + + for(int i = n - 1; i >= 1; i--) + { + for(int k = n - i - 1; k >= 0; k--) + cout << " "; + + for(int j = i; j >= 1; j--) + cout << j; + + cout << endl; + } + + return 0; +} diff --git a/Sem. 03/solutions/exercise 10.cpp b/Sem. 03/solutions/exercise 10.cpp new file mode 100644 index 0000000..dae43ab --- /dev/null +++ b/Sem. 03/solutions/exercise 10.cpp @@ -0,0 +1,25 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int count; + cin >> count; + + for (int i = 1; i <= count; i++) + { + for (int j = 0; j < count; j++) + { + if (j % 2 == 0) + { + cout << i + (count * j) << " "; + } + else + { + cout << (j + 1) * count - (i - 1) << " "; + } + } + cout << endl; + } +} diff --git a/Sem. 03/solutions/exercise 11.cpp b/Sem. 03/solutions/exercise 11.cpp new file mode 100644 index 0000000..66a90a9 --- /dev/null +++ b/Sem. 03/solutions/exercise 11.cpp @@ -0,0 +1,28 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int count; + cin >> count; + + for (int i = 1; i <= count; i++) + { + if (i % 2 != 0) + { + for (int j = 1 + ((i - 1) * count); j <= i * count; j++) + { + cout << j << " "; + } + } + else + { + for (int j = i * count; j > (i - 1) * count; j--) + { + cout << j << " "; + } + } + cout << endl; + } +} diff --git a/Sem. 03/solutions/exercise 15.cpp b/Sem. 03/solutions/exercise 15.cpp new file mode 100644 index 0000000..652a201 --- /dev/null +++ b/Sem. 03/solutions/exercise 15.cpp @@ -0,0 +1,58 @@ +#include <iostream> +using namespace std; + +int main() +{ + int n; + cin >> n; + + int spacesCount = 2 * (n - 1) + n + 2 * (n - 1); + int toGuess; + for (int i = 1; i <= n - 1; i++) + { + for (int j = 1; j <= i; j++) + cout << j << " "; + + for (int j = 0; j < spacesCount; j++) + cout << " "; + + for (int j = i; j >= 1; j--) + cout << " " << j; + + spacesCount -= 4; + + cout << endl; + + } + + //mid + for (int i = 1; i <= n; i++) + cout << i << " "; + for (int i = 0; i < n; i++) + cout << '-'; + for (int i = n; i >= 1; i--) + cout << " " << i; + cout << endl; + + + //down + spacesCount += 4; + for (int i = n - 1; i >= 0; i--) + { + for (int j = 1; j <= i; j++) + cout << j << " "; + + for (int j = 0; j < spacesCount; j++) + cout << " "; + + for (int j = i; j >= 1; j--) + cout << " " << j; + + spacesCount += 4; + + cout << endl; + + } + + +} diff --git a/Sem. 04/README.md b/Sem. 04/README.md new file mode 100644 index 0000000..8e8acb0 --- /dev/null +++ b/Sem. 04/README.md @@ -0,0 +1,117 @@ +. +# Функции + + + +В контекста на програмирането, функция се нарича именувана група от инструкции, които изпълняват дадена функционалност. + +### Синтаксис +```c++ +<тип на връщане> <идентификатор> (параметър1, параметър ...) +{ + //тяло +} +``` +Всеки параметър се състои от **тип** и **идентификатор**. + +Пример: +```c++ +int max(int n, int k) +{ + return n > k ? n : k; +} +``` +Оператор **return**: + + - Прекратява изпълнението на функцията + - Замества извикването и с резултата (връща резултата) + +Операторът може да се използва и при функции, които не връщат нищо (void функции). Тогава return единствено прекратява функцията. + +### Параметри по копие +**Важно**: Функциите, написани по този начин, приемат своите параметри **по копие**. (Функции, които приемат параметри по референция (не по копие) ще учим по-натам). +```c++ +void f(int n) +{ + n++; +} +int main() +{ + int n = 4; + f(n); + cout << n << endl; //4 +} +``` + + +### Примери: + +**Задача 1:** Въвежда се неотрицателно число n. Да се отпечатат всички двойки прости числа, които са във вида 6k-1 и 6k+1. + +-решена без функции и решена с функции. + +**Задача 2:** Напишете функция, която приема неотрицателно число n и връща числото обърнато + +*Вход: 288, Изход: 882* + +**Задача 3:** Напишете функция, която приема неотрицателно число и връща дали числото е палиндром. + +*Вход: 2882 Изход: true* + +**Задача 4:** Напишете функция, която приема неотрицателни числа n и k и връща дали k е суфикс на n. + +*Вход: 288, 88, Изход: true* + +**Задача 5:** Напишете функция, която приема неотрицателни числа n и k и връща дали k е префикс на n. + +*Вход: 288, 28, Изход: true* + +**Задача 6:** Напишете функция, която приема неотрицателни числа n и k и връща дали k е инфикс в n. + +*Вход: 2831, 83, Изход: true* + +**Задача 7:** Напишете функция, която приема неотрицателно число и връща дали цифрите му са сортирани (във възходящ или в низходящ ред). + +*Вход: 122239, Изход: true* + + +**Задача 8:** Напишете функция, която приема неотрицателно число и връща дали цифрите му са еднакви. + +*Вход: 111111, Изход: true* + +**Задача 9:** Напишете функция, която приема неотрицателно числа n и k връща n^k + +*Вход: 3 4, Изход: 81* + +**Задача 10:** Напишете функция, която приема неотрицателно числа n и k връща log_k(n) (закръглено надолу) + +*Вход: 90 3 Изход: 4* + +<h3>Задачи:</h3> + +**Задача 1:** Напишете функция, която приема цяло число и връща модула му. + +*Вход: 4, Изход: 4* + +*Вход: -4, Изход: 4* + +*Вход: 5 4 3 0 Изход: 12* + +**Задача 2:** Напишете функция, която приема 4 цели числа - координати на 2 точки. Функцията да връща разстоянието между тези точки. + +*Вход: 1 1 4 3, Изход: 3,605* + +**Задача 3:** Напишете функция, която приема 6 целочислени координати (3 точки) и връща обиколката на триъгълника по тези точки. + +*Вход: 1 1 4 7 0 10, Изход: 20.764* + +**Задача 4:** Да се напише функция, която приема три естествени числа a и b и k отпечатва всички числа в интервата [a...b], които имат точно k различни прости делители. +*Вход: 19 32 3, Изход: 20 30* +*Вход: 19 32 1, Изход: 19 23 29 31* + +**Задача 5:** Да се напише функция, която приема естествено число n и отпечатва всички двойки <a,b>, такива че a и b са прости и a + b = n. + +**Задача 6:** Да се напише функция, която приема две естествени числа a и b и отпечава всички числа k и интервала [a,b], такива че k = x^y (за някои ест. числа x и y) +*Вход: 15 30, Изход: 16 25 27* , защото (4^2, 5^2, 3^3) + + diff --git a/Sem. 04/Solutions/exercise01.cpp b/Sem. 04/Solutions/exercise01.cpp new file mode 100644 index 0000000..edc9c9c --- /dev/null +++ b/Sem. 04/Solutions/exercise01.cpp @@ -0,0 +1,11 @@ +#include <iostream> +using namespace std; + +int mod(int n) +{ + return n < 0 ? -n : n; +} +int main() +{ + cout << mod(-4) << endl; +} diff --git a/Sem. 04/example01_no_functions.cpp b/Sem. 04/example01_no_functions.cpp new file mode 100644 index 0000000..06e6629 --- /dev/null +++ b/Sem. 04/example01_no_functions.cpp @@ -0,0 +1,39 @@ +#include <iostream>
+using namespace std;
+
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ for (int i = 6; i < n; i += 6)
+ {
+ int firstToCheck = i - 1;
+ int secondToCheck = i + 1;
+
+ //prime check
+ bool isFirstPrime = true;
+ double temp = sqrt(firstToCheck);
+ for (int j = 2; j <= temp; j++)
+ {
+ if (firstToCheck % j == 0)
+ isFirstPrime = false;
+ }
+
+ if (!isFirstPrime)
+ continue;
+
+ //prime check
+ bool isSecondPrime = true;
+ double temp2 = sqrt(secondToCheck);
+ for (int j = 2; j <= temp2; j++)
+ {
+ if (secondToCheck % j == 0)
+ isSecondPrime = false;
+ }
+
+ if (isSecondPrime)
+ cout << firstToCheck << " " << secondToCheck << endl;
+ }
+}
\ No newline at end of file diff --git a/Sem. 04/example01_with_functions.cpp b/Sem. 04/example01_with_functions.cpp new file mode 100644 index 0000000..f33b6b9 --- /dev/null +++ b/Sem. 04/example01_with_functions.cpp @@ -0,0 +1,27 @@ +#include <iostream>
+using namespace std;
+
+bool isPrime(int n)
+{
+ double temp = sqrt(n);
+
+ for (int i = 2; i <= temp; i++)
+ {
+ if (n % i == 0)
+ return false;
+ }
+
+ return true;
+}
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ for (int i = 6; i < n; i += 6)
+ {
+ if (isPrime(i - 1) && isPrime(i + 1))
+ cout << i - 1 << " " << i + 1 << endl;
+ }
+}
\ No newline at end of file diff --git a/Sem. 04/example02-08.cpp b/Sem. 04/example02-08.cpp new file mode 100644 index 0000000..002a8ab --- /dev/null +++ b/Sem. 04/example02-08.cpp @@ -0,0 +1,141 @@ +#include <iostream>
+using namespace std;
+
+
+//задача 2:
+// Напишете функция, която приема цяло число и връща обърнатото му.
+// 1234 - 4321
+
+int reverse(unsigned int n)
+{
+ unsigned int result = 0;
+
+ while (n != 0)
+ {
+ int lastDigit = n % 10;
+ (result *= 10) += lastDigit;
+ n /= 10;
+ }
+
+ return result;
+}
+
+//задача 3: Напишете функция, която приема цяло число и връща дали е палиндром
+
+//12321 -> true
+//123 -> false;
+
+bool isPalindrome(unsigned int n)
+{
+ return n == reverse(n);
+}
+//задача 4: Напишете функция, която приема две цели числа - n и k.
+//Функцията да връща дали k е суфикс на n.
+// n = 1234 k = 34 true
+// n = 1234 k = 234 true
+// n = 1234 k = 1234 true
+// n = 1234 k = 23 false
+
+bool isSuffix(unsigned int n, unsigned int k)
+{
+ if (k == 0 && (n % 10 != 0))
+ return false;
+
+ while (k != 0)
+ {
+ int lastDigitOfN = n % 10;
+ int lastDigitOfK = k % 10;
+
+ if (lastDigitOfN != lastDigitOfK)
+ return false;
+
+ n /= 10;
+ k /= 10;
+ }
+
+ return true;
+}
+
+
+// задача 5: Напишете функция, която приема две цели числа -n и k.
+// Функцията да връща дали k е префикс на n.
+
+// n = 1234, k = 12 true
+// n = 1234, k = 23 false;
+
+// u е префикс на w <-> u ^rev е суфикс на w^rev
+bool isPrefix(unsigned int n, unsigned int k)
+{
+ return isSuffix(reverse(n), reverse(k));
+}
+
+// задача 6: Напишете функция, която приема две числа - n,k.
+// Функцията връща дали k е инфикс на n.
+
+bool isInfix(unsigned int n, unsigned int k)
+{
+ if (n == 0 && k == 0)
+ return true;
+
+ while (n > 0)
+ {
+ if (isSuffix(n, k))
+ return true;
+ n /= 10;
+ }
+ return false;
+}
+
+
+//задача.
+//Да се напише функция, която приема цяло число и връща дали цифрите му са сортирани.
+
+// 1234 true
+// 12379 true
+// 1231 false
+// 4321 true
+// 4325 false
+// 1111 true
+
+bool areDigitsSortedAscending(unsigned int n)
+{
+ while (n >= 10)
+ {
+ int lastDigit = n % 10;
+ int prevLastDigit = (n / 10) % 10;
+
+ if (prevLastDigit > lastDigit)
+ return false;
+
+ n /= 10;
+ }
+
+ return true;
+}
+
+bool areDigitsSortedDescending(int n)
+{
+ return areDigitsSortedAscending(reverse(n));
+}
+
+bool areDigitsSorted(int n)
+{
+ return areDigitsSortedDescending(n) || areDigitsSortedAscending(n);
+}
+
+//задача.
+// Напишете функция, която приема цяло число и връща дали се съдържа само от еднакви цифри.
+
+//Вход: 11111 Изход:true
+//Вход: 123 Изход: false
+
+bool containOnlyOneDigit(int n)
+{
+ return areDigitsSortedDescending(n) && areDigitsSortedAscending(n);
+}
+
+
+int main()
+{
+ cout << containOnlyOneDigit(444) << endl;
+}
\ No newline at end of file diff --git a/Sem. 04/example09 .cpp b/Sem. 04/example09 .cpp new file mode 100644 index 0000000..f7f072b --- /dev/null +++ b/Sem. 04/example09 .cpp @@ -0,0 +1,21 @@ +#include <iostream>
+using namespace std;
+
+unsigned int log(unsigned int n, unsigned int k)
+{
+ int count = 0;
+
+ while (n >= k)
+ {
+ n /= k;
+ count++;
+ }
+
+ return count;
+}
+
+
+int main()
+{
+ cout << log(4, 10) << endl;
+}
\ No newline at end of file diff --git a/Sem. 04/example10.cpp b/Sem. 04/example10.cpp new file mode 100644 index 0000000..f7f072b --- /dev/null +++ b/Sem. 04/example10.cpp @@ -0,0 +1,21 @@ +#include <iostream>
+using namespace std;
+
+unsigned int log(unsigned int n, unsigned int k)
+{
+ int count = 0;
+
+ while (n >= k)
+ {
+ n /= k;
+ count++;
+ }
+
+ return count;
+}
+
+
+int main()
+{
+ cout << log(4, 10) << endl;
+}
\ No newline at end of file diff --git a/Sem. 05_kn/BAndC.cpp b/Sem. 05_kn/BAndC.cpp new file mode 100644 index 0000000..30f8a3d --- /dev/null +++ b/Sem. 05_kn/BAndC.cpp @@ -0,0 +1,115 @@ +#include <iostream> +using namespace std; + + +const int DIGITS_COUNT = 4; + +int getNumLen(int n) +{ + int count = 0; + while (n != 0) + { + count++; + n /= 10; + } + return count; +} + +bool containsDigit(int n, int digit) +{ + while (n != 0) + { + int lastDigit = n % 10; + + if (digit == lastDigit) + return true; + + n /= 10; + } + return false; +} +bool containsDiffrentDigits(int n) +{ + while (n >= 10) + { + int lastDigit = n % 10; + n /= 10; + + if (containsDigit(n, lastDigit)) + return false; + } + return true; +} + +bool isValid(int n) +{ + return getNumLen(n) == DIGITS_COUNT && containsDiffrentDigits(n); +} + +void userInput(int& toGuess) +{ + do + { + cin >> toGuess; + } while (!isValid(toGuess)); +} + +int getBulls(int toGuess, int myGuess) +{ + int bulls = 0; + while (toGuess != 0) + { + int toGuessLastDigit = toGuess % 10; + int myGuessLastDigit = myGuess % 10; + + if (toGuessLastDigit == myGuessLastDigit) + bulls++; + + toGuess /= 10; + myGuess /= 10; + } + return bulls; +} + +int getCows(int toGuess, int myGuess, int bulls) +{ + int cows = 0; + while (myGuess != 0) + { + int lastDigit = myGuess % 10; + + if (containsDigit(toGuess, lastDigit)) + cows++; + + myGuess /= 10; + } + return cows - bulls; +} + +void getBullsAndCows(int toGuess, int myGuess, int& bulls, int& cows) +{ + bulls = getBulls(toGuess, myGuess); + cows = getCows(toGuess, myGuess, bulls); +} + +int main() +{ + int toGuess; + userInput(toGuess); + + cout << "Try to guess:" << endl; + + int bulls = 0; + int cows = 0; + + while (bulls != DIGITS_COUNT) + { + int myGuess; + userInput(myGuess); + getBullsAndCows(toGuess, myGuess, bulls, cows); + + cout << "Bulls: " << bulls << " " << ", Cows: " << cows << endl; + } + + cout << "Success!" << endl; +} diff --git a/Sem. 05_kn/draw b/Sem. 05_kn/draw new file mode 100644 index 0000000..652a201 --- /dev/null +++ b/Sem. 05_kn/draw @@ -0,0 +1,58 @@ +#include <iostream> +using namespace std; + +int main() +{ + int n; + cin >> n; + + int spacesCount = 2 * (n - 1) + n + 2 * (n - 1); + int toGuess; + for (int i = 1; i <= n - 1; i++) + { + for (int j = 1; j <= i; j++) + cout << j << " "; + + for (int j = 0; j < spacesCount; j++) + cout << " "; + + for (int j = i; j >= 1; j--) + cout << " " << j; + + spacesCount -= 4; + + cout << endl; + + } + + //mid + for (int i = 1; i <= n; i++) + cout << i << " "; + for (int i = 0; i < n; i++) + cout << '-'; + for (int i = n; i >= 1; i--) + cout << " " << i; + cout << endl; + + + //down + spacesCount += 4; + for (int i = n - 1; i >= 0; i--) + { + for (int j = 1; j <= i; j++) + cout << j << " "; + + for (int j = 0; j < spacesCount; j++) + cout << " "; + + for (int j = i; j >= 1; j--) + cout << " " << j; + + spacesCount += 4; + + cout << endl; + + } + + +} diff --git a/Sem. 05_kn/refferences.cpp b/Sem. 05_kn/refferences.cpp new file mode 100644 index 0000000..47e4fba --- /dev/null +++ b/Sem. 05_kn/refferences.cpp @@ -0,0 +1,119 @@ +#include <iostream> +using namespace std; + + +int f(int n) +{ + n++; + return n; +} + +// lvalue ("връщане на променлива") +// rvalue ("връщане на константа") + +//Референция - друго име за някоя променлива + +// Пример: +// Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра. + +void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit) +{ + int max = 0; + int min = 9; + + while (n != 0) + { + int lastDigit = n % 10; + + if (lastDigit > max) + max = lastDigit; + + if (lastDigit < min) + min = lastDigit; + + n /= 10; + } + maxDigit = max; + minDigit = min; + +} + +//! + +int& f(int& n) //--няма да върне копие. +{ + n++; + return n; +} + +int main() +{ + int n; + cin >> n; + + int max, min; + + getMaxAndMinDigit(n, max, min); + cout << "Min digit " << min << " " << "max digit " << max << endl; +} + + + + + +#include <iostream> +using namespace std; + +// lvalue ("връщане на променлива") +// rvalue ("връщане на константа") + +//Референция - друго име за някоя променлива + +//Пример: +// Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра. + +void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit) +{ + int max = 0; + int min = 9; + + while (n != 0) + { + int lastDigit = n % 10; + + if (lastDigit > max) + max = lastDigit; + + if (lastDigit < min) + min = lastDigit; + + n /= 10; + } + maxDigit = max; + minDigit = min; + +} + + +int f1(int& n) //-връщат константа (rvalue) +{ + n++; + return n; +} +int& f2(int& n) //--връщат променлива (lvalue). +{ + n++; + return n; +} +///!!!!!!!!!!! error +int& f3() +{ + int a = 10; + return a; +} +int main() +{ + cout << ++f(); + + +} diff --git a/pract_KN/pract. 04/solutions/solutions.cpp b/pract_KN/pract. 04/solutions/solutions.cpp new file mode 100644 index 0000000..7a1df9f --- /dev/null +++ b/pract_KN/pract. 04/solutions/solutions.cpp @@ -0,0 +1,64 @@ +#include<iostream>
+
+int sumInInterval(int n, int m) {
+ int result = 0;
+ for (int i = n; i <= m; i++)
+ result += i;
+
+ return result;
+}
+
+double power(double number, int n) {
+ double result = 1;
+ for (int i = 0; i < n; i++)
+ result *= n;
+ return result;
+}
+
+int absolute(int n) {
+ if (n < 0)
+ return -n;
+ return n;
+}
+
+double dist(double x1, double y1, double x2, double y2) {
+ return sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
+}
+
+double perimeter(double x1, double y1, double x2, double y2, double x3, double y3) {
+ return dist(x1, y1, x2, y2) + dist(x1, y1, x3, y3) + dist(x2, y2, x3, y3);
+}
+
+int fact(int n) {
+ int result = 1;
+ for (int i = 1; i <= n; i++)
+ result *= i;
+ return result;
+}
+
+bool isNumber(char n) {
+ return (n >= '0' && n <= '9');
+}
+
+//Защо това работи?
+int f(int a, int b) {
+ return (a + b) * (a + b + 1) / 2 + b;
+}
+
+void play() {
+ int computerNumber = rand() % 100 + 1;
+ int myNumber = 0;
+ while (myNumber != computerNumber) {
+ std::cout << "Enter a number -> ";
+ std::cin >> myNumber;
+ if (myNumber > computerNumber)
+ std::cout << "Try smaller number" << std::endl;
+ else if (myNumber < computerNumber)
+ std::cout << "Try bigger number" << std::endl;
+ }
+ std::cout << "Rigth!!!" << std::endl;
+}
+
+int main() {
+ //(...)
+}
\ No newline at end of file diff --git a/pract_KN/pract. 04/Задачи четвърта седмица.pdf b/pract_KN/pract. 04/Задачи четвърта седмица.pdf Binary files differnew file mode 100644 index 0000000..88f53b4 --- /dev/null +++ b/pract_KN/pract. 04/Задачи четвърта седмица.pdf diff --git a/pract_SI/pract. 01/task1.cpp b/pract_SI/Pract. 01/Solutions/task1.cpp index 54a1902..54a1902 100644 --- a/pract_SI/pract. 01/task1.cpp +++ b/pract_SI/Pract. 01/Solutions/task1.cpp diff --git a/pract_SI/pract. 01/task2.cpp b/pract_SI/Pract. 01/Solutions/task2.cpp index 554653d..554653d 100644 --- a/pract_SI/pract. 01/task2.cpp +++ b/pract_SI/Pract. 01/Solutions/task2.cpp diff --git a/pract_SI/pract. 01/task3.cpp b/pract_SI/Pract. 01/Solutions/task3.cpp index 830348b..830348b 100644 --- a/pract_SI/pract. 01/task3.cpp +++ b/pract_SI/Pract. 01/Solutions/task3.cpp diff --git a/pract_SI/pract. 01/task4.cpp b/pract_SI/Pract. 01/Solutions/task4.cpp index b4829b4..b4829b4 100644 --- a/pract_SI/pract. 01/task4.cpp +++ b/pract_SI/Pract. 01/Solutions/task4.cpp diff --git a/pract_SI/pract. 01/task5.cpp b/pract_SI/Pract. 01/Solutions/task5.cpp index 576bf2e..931178d 100644 --- a/pract_SI/pract. 01/task5.cpp +++ b/pract_SI/Pract. 01/Solutions/task5.cpp @@ -4,7 +4,7 @@ using namespace std; int main() { - string card_number;; + string card_number; cout << "Card number: "; cin >> card_number; diff --git a/pract_SI/pract. 01/Tasks_1.txt b/pract_SI/Pract. 01/Tasks_1.txt index bb7ee2d..bb7ee2d 100644 --- a/pract_SI/pract. 01/Tasks_1.txt +++ b/pract_SI/Pract. 01/Tasks_1.txt diff --git a/pract_SI/pract. 02/task1.cpp b/pract_SI/Pract. 02/Solutions/task1.cpp index 3a60fad..3a60fad 100644 --- a/pract_SI/pract. 02/task1.cpp +++ b/pract_SI/Pract. 02/Solutions/task1.cpp diff --git a/pract_SI/pract. 02/task2.cpp b/pract_SI/Pract. 02/Solutions/task2.cpp index a1ddcf8..a1ddcf8 100644 --- a/pract_SI/pract. 02/task2.cpp +++ b/pract_SI/Pract. 02/Solutions/task2.cpp diff --git a/pract_SI/pract. 02/task3 - no EPSILON.cpp b/pract_SI/Pract. 02/Solutions/task3 - no EPSILON.cpp index 6c9f832..6c9f832 100644 --- a/pract_SI/pract. 02/task3 - no EPSILON.cpp +++ b/pract_SI/Pract. 02/Solutions/task3 - no EPSILON.cpp diff --git a/pract_SI/pract. 02/task3 - with EPSILON.cpp b/pract_SI/Pract. 02/Solutions/task3 - with EPSILON.cpp index dcd5f88..dcd5f88 100644 --- a/pract_SI/pract. 02/task3 - with EPSILON.cpp +++ b/pract_SI/Pract. 02/Solutions/task3 - with EPSILON.cpp diff --git a/pract_SI/pract. 02/task4.cpp b/pract_SI/Pract. 02/Solutions/task4.cpp index acb471c..acb471c 100644 --- a/pract_SI/pract. 02/task4.cpp +++ b/pract_SI/Pract. 02/Solutions/task4.cpp diff --git a/pract_SI/pract. 02/task5.cpp b/pract_SI/Pract. 02/Solutions/task5.cpp index 419d6d4..419d6d4 100644 --- a/pract_SI/pract. 02/task5.cpp +++ b/pract_SI/Pract. 02/Solutions/task5.cpp diff --git a/pract_SI/pract. 02/task6.cpp b/pract_SI/Pract. 02/Solutions/task6.cpp index a7484b2..a7484b2 100644 --- a/pract_SI/pract. 02/task6.cpp +++ b/pract_SI/Pract. 02/Solutions/task6.cpp diff --git a/pract_SI/pract. 02/task7.cpp b/pract_SI/Pract. 02/Solutions/task7.cpp index 3c22a83..3c22a83 100644 --- a/pract_SI/pract. 02/task7.cpp +++ b/pract_SI/Pract. 02/Solutions/task7.cpp diff --git a/pract_SI/pract. 02/Tasks_2.txt b/pract_SI/Pract. 02/Tasks_2.txt index a419241..a419241 100644 --- a/pract_SI/pract. 02/Tasks_2.txt +++ b/pract_SI/Pract. 02/Tasks_2.txt diff --git a/pract_SI/Pract. 03-04/Solutions/task1.cpp b/pract_SI/Pract. 03/Solutions/task1.cpp index ae955a8..ae955a8 100644 --- a/pract_SI/Pract. 03-04/Solutions/task1.cpp +++ b/pract_SI/Pract. 03/Solutions/task1.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task2.cpp b/pract_SI/Pract. 03/Solutions/task2.cpp index b3e970d..b3e970d 100644 --- a/pract_SI/Pract. 03-04/Solutions/task2.cpp +++ b/pract_SI/Pract. 03/Solutions/task2.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task3.cpp b/pract_SI/Pract. 03/Solutions/task3.cpp index a6a6362..a6a6362 100644 --- a/pract_SI/Pract. 03-04/Solutions/task3.cpp +++ b/pract_SI/Pract. 03/Solutions/task3.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task4.cpp b/pract_SI/Pract. 03/Solutions/task4.cpp index 622b8a1..622b8a1 100644 --- a/pract_SI/Pract. 03-04/Solutions/task4.cpp +++ b/pract_SI/Pract. 03/Solutions/task4.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task5.cpp b/pract_SI/Pract. 03/Solutions/task5.cpp index d6ac57c..d6ac57c 100644 --- a/pract_SI/Pract. 03-04/Solutions/task5.cpp +++ b/pract_SI/Pract. 03/Solutions/task5.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task6.cpp b/pract_SI/Pract. 03/Solutions/task6.cpp index 8d0beac..8d0beac 100644 --- a/pract_SI/Pract. 03-04/Solutions/task6.cpp +++ b/pract_SI/Pract. 03/Solutions/task6.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task7.cpp b/pract_SI/Pract. 03/Solutions/task7.cpp index aa9194b..aa9194b 100644 --- a/pract_SI/Pract. 03-04/Solutions/task7.cpp +++ b/pract_SI/Pract. 03/Solutions/task7.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task8.cpp b/pract_SI/Pract. 03/Solutions/task8.cpp index 3f32ebf..3f32ebf 100644 --- a/pract_SI/Pract. 03-04/Solutions/task8.cpp +++ b/pract_SI/Pract. 03/Solutions/task8.cpp diff --git a/pract_SI/Pract. 03-04/Solutions/task9.cpp b/pract_SI/Pract. 03/Solutions/task9.cpp index 9f1f054..9f1f054 100644 --- a/pract_SI/Pract. 03-04/Solutions/task9.cpp +++ b/pract_SI/Pract. 03/Solutions/task9.cpp diff --git a/pract_SI/Pract. 03/Startup_tasks - Solutions/task1_solution1.cpp b/pract_SI/Pract. 03/Startup_tasks - Solutions/task1_solution1.cpp new file mode 100644 index 0000000..988d53c --- /dev/null +++ b/pract_SI/Pract. 03/Startup_tasks - Solutions/task1_solution1.cpp @@ -0,0 +1,35 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n, initialSum; + cin >> n; + initialSum = n; + + int Hundred_Dollar_Bill = 0; + int Fifty_Dollar_Bill = 0; + int Twenty_Dollar_Bill = 0; + int Ten_Dollar_Bill = 0; + int Five_Dollar_Bill = 0; + int Two_Dollar_Bill = 0; + int One_Dollar_Bill = 0; + + Hundred_Dollar_Bill = n / 100; + n %= 100; + Fifty_Dollar_Bill = n / 50; + n %= 50; + Twenty_Dollar_Bill = n / 20; + n %= 20; + Ten_Dollar_Bill = n / 10; + n %= 10; + Five_Dollar_Bill = n / 5; + n %= 5; + Two_Dollar_Bill = n / 2; + n %= 2; + One_Dollar_Bill = n; + + cout << initialSum << " = " << Hundred_Dollar_Bill << "*100 + " << Fifty_Dollar_Bill << "*50 + " << Twenty_Dollar_Bill << "*20 + " << Ten_Dollar_Bill << "*10 + " << + Five_Dollar_Bill << "*5 + " << Two_Dollar_Bill << "*2 + " << One_Dollar_Bill << "*1" << endl; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 03/Startup_tasks - Solutions/task1_solution2.cpp b/pract_SI/Pract. 03/Startup_tasks - Solutions/task1_solution2.cpp new file mode 100644 index 0000000..f01ec74 --- /dev/null +++ b/pract_SI/Pract. 03/Startup_tasks - Solutions/task1_solution2.cpp @@ -0,0 +1,35 @@ +#include <iostream> + +using namespace std; + +int main() +{ + unsigned long long money; //assign money's interval + cin >> money; //enter number of money + + cout << "Initial price: " << money << " = "; //Initial price output + + cout << money / 100 << "*100 + "; //output for 100 + money %= 100; //reduce the 100's + + cout << money / 50 << "*50 + "; //output for 50 + money %= 50; //reduce the 50's + + cout << money / 20 << "*20 + "; //output for 20 + money %= 20; //reduce the 20's + + cout << money / 10 << "*10 + "; //output for 10 + money %= 10; //reduce the 10's + + cout << money / 5 << "*5 + "; //output for 5 + money %= 5; //reduce the 5's + + cout << money / 2 << "*2 + "; //output for 2 + money %= 2; //reduce the 2's + + cout << money / 1 << "*1" << endl; //output for 1 + money %= 1; //reduce the 1's + + cout << "End of program" << endl; //end program + return 0; //end program +}
\ No newline at end of file diff --git a/pract_SI/Pract. 03/Startup_tasks - Solutions/task2.cpp b/pract_SI/Pract. 03/Startup_tasks - Solutions/task2.cpp new file mode 100644 index 0000000..ab5ca6f --- /dev/null +++ b/pract_SI/Pract. 03/Startup_tasks - Solutions/task2.cpp @@ -0,0 +1,36 @@ +#include <iostream> + +using namespace std; + +int main() +{ + unsigned long long velocity; //assign velocity's interval + cin >> velocity; //enter velocity's value + + if (velocity < 10) + cout << "No fine" << endl; + else if (velocity == 10) + cout << "Fine: 20$" << endl; + else if (velocity < 20) + cout << "Fine: 50$" << endl; + else if (velocity < 30) + cout << "Fine: 100$" << endl; + else if (velocity < 40) + cout << "Fine if in Urban area: 400$" << endl //Urban area fine + << "Fine if in Metropolitan area: 300$" << endl; //Metropolitan area fine + else if(velocity <= 50) + cout << "Fine if in Urban area: 600$" << endl //Urban area fine + << "Fine if in Metropolitan area: 400$" << endl;//Metropolitan area fine + else + { + unsigned long long fine = 700 + (((velocity - 50) / 5) * 50); + + + cout << "Fine if in Urban area: " << fine << "$" << endl + << "Additional 3 months driver licence suspension and 12 driver control points." << endl << endl; //Urban area fine + cout << "Fine if in Metropolitan area: " << fine - 100 << "$" << endl;//Metropolitan area fine + } + + cout << endl << "End of program" << endl; //end program + return 0; //end program +}
\ No newline at end of file diff --git a/pract_SI/Pract. 03-04/Startup_tasks.txt b/pract_SI/Pract. 03/Startup_tasks.txt index 8ee6d58..8ee6d58 100644 --- a/pract_SI/Pract. 03-04/Startup_tasks.txt +++ b/pract_SI/Pract. 03/Startup_tasks.txt diff --git a/pract_SI/Pract. 03-04/Tasks3.txt b/pract_SI/Pract. 03/Tasks3.txt index 64c5c4c..64c5c4c 100644 --- a/pract_SI/Pract. 03-04/Tasks3.txt +++ b/pract_SI/Pract. 03/Tasks3.txt diff --git a/pract_SI/Pract. 04/Solutions/task1.cpp b/pract_SI/Pract. 04/Solutions/task1.cpp new file mode 100644 index 0000000..b56d6e1 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task1.cpp @@ -0,0 +1,34 @@ +#include <iostream> + +using namespace std; + +int sum(int a, int b) +{ + /* + //First way + int sum = a + b; + return sum; + */ + + //Second way + return a + b; +} + +int main() +{ + int num1, num2; + cin >> num1 >> num2; + + //First way + cout << sum(num1, num2) << endl; //return as output on Console + + /* + //Second way + //int sumOfNumbers = sum(num1, num2); //create an int with value num1+num2; + //cout << sumOfNumbers << endl; + */ + + return 0; +} + + diff --git a/pract_SI/Pract. 04/Solutions/task2 - example1.cpp b/pract_SI/Pract. 04/Solutions/task2 - example1.cpp new file mode 100644 index 0000000..25b4030 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task2 - example1.cpp @@ -0,0 +1,15 @@ +#include <iostream> +using namespace std; + +bool isEven(int number) +{ + return !(number % 2); +} + +int main() +{ + int n; + cin >> n; + cout << boolalpha << isEven(n) << endl; + return 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task2 - example2.cpp b/pract_SI/Pract. 04/Solutions/task2 - example2.cpp new file mode 100644 index 0000000..2d365ba --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task2 - example2.cpp @@ -0,0 +1,17 @@ +#include <iostream> + +bool isEven(int num) { + return !(num % 2); +} + +int main() +{ + int num; + std::cin >> num; + + if (isEven(num)) + std::cout << "Even"; + else + std::cout << "Odd"; + return 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task3.cpp b/pract_SI/Pract. 04/Solutions/task3.cpp new file mode 100644 index 0000000..937b833 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task3.cpp @@ -0,0 +1,4 @@ +bool isPositive(int num) +{ + return (num > 0); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task4.cpp b/pract_SI/Pract. 04/Solutions/task4.cpp new file mode 100644 index 0000000..771550b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task4.cpp @@ -0,0 +1,4 @@ +int abs(int num1, int num2) +{ + return ((num1 - num2) >= 0 ? (num1 - num2) : (num2 - num1)); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task5.cpp b/pract_SI/Pract. 04/Solutions/task5.cpp new file mode 100644 index 0000000..42b5fc6 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task5.cpp @@ -0,0 +1,20 @@ +#include <iostream> +using namespace std; +void print(int N, char S = ' ') + { + for (size_t i = 0; i < N; i++) + { + cout << S; + } + } + +int main() +{ + int N; + char S; + cin >> N >> S; + print(N); + cout<< "Error!" << endl; + print(N, S); + return 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Tasks4.txt b/pract_SI/Pract. 04/Tasks4.txt new file mode 100644 index 0000000..4daf06c --- /dev/null +++ b/pract_SI/Pract. 04/Tasks4.txt @@ -0,0 +1,32 @@ +#Никола Светославов - task4 + +## Задачи + +1. Да се състави функция sum(int a, int b), която приема две числа и връща като резултат сбора им. +2. Да се напише функция isEven(int num), която връща дали дадено число е четно или не. +3. а)Да се напише функция isPositive(int num), която връща дали дадено число е положително или не + b)Да се напише функция isNegative(int num), която връща дали дадено число е отрицателно или не. +4. a)Напишете функция abs(int num), която връща като резултат абсолютната стойност на числото num. + b)Напишете функция fabs(double num), която връща като резултат абсолютната стойност на числото num. +5. Напишете функция print(char S,int N), която извежда на екрана N-пъти символа S. +6. Напишете функция isCapitalLetter(char S), която проверява дали даден символ е главна буква. + *Реализирайте функция checkSymbol(char S), която да проверява дали символът е буква.* +7. a)Напишете функция, която проверява дали дадено число е в интервала [100;10000] + b)Напишете функция, която проверява дали дадено число е в даден интервал. + +8.Напишете функция pow(int num,unsigned N), която връща като резултат числото num повдигнато на степен N, където N е строго положително. +9.Напишете функция calculate(nt a,int b, char action), където action може да бъде [+-*/%]. + Програмата да връща като резултат съответното действие, а ако не се укаже символ да върне сбора на числата +10.Напишете функция, askUser(int from, int to), която кара потребителя да въвежда числа докато не въведе число, + което да е в указания интервал, а като резултат връща въпросното число. + +11.Напишете програма, която по въведено число изкарва представянето му в двоична бройна система. + Реализирайте го чрез функция. +12.Да се напише програма, която въвежда стойности на естествените числа a, b, c и d и намира и извежда най-големият общ делител + на числата a и b, след това на c и d и накрая на a, b, c и d. +13.Напишете програма, която получава цяло естествено число в интервала [100;100000]. + Програмата да изведе най - близкото число степен на двойката до това число.(тоест разликата на числата по абсолютна стойност да е минимална). + Входа да се валидира като при грешен ход да се дава възможност за нов опит. + Изкарайте отделните функционалности във функции. + + diff --git a/pract_SI/Pract. 04/function overload.cpp b/pract_SI/Pract. 04/function overload.cpp new file mode 100644 index 0000000..7e0bdfa --- /dev/null +++ b/pract_SI/Pract. 04/function overload.cpp @@ -0,0 +1,16 @@ +void print() //print - arguments: 0 +{ + cout << "Hello World!\n"; +} + +void print(int number) //print - arguments: 1 + //int +{ + cout << "Hello World N" << number << endl; +} + +void print(double number) //print - arguments: 1 + //double +{ + cout << "Hello World N" << number << endl; +} |
