diff --git a/10_strings/practice/strings_advanced/crossword.cpp b/10_strings/practice/strings_advanced/crossword.cpp new file mode 100644 index 0000000..fd877dd --- /dev/null +++ b/10_strings/practice/strings_advanced/crossword.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file crossword.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for advanced_string tasks from practice 10. + */ + +#include + +const int MAX = 16; + +// fill the crossword puzzle with words +void fill(char cross[][MAX], int size) { + + for (size_t i = 0; i < size - 1 ; i++) + std::cin >> cross[i]; +} + +void print(char cross[][MAX], int size) { + + for (size_t i = 0; i < size - 1; i++) { + for (size_t j = 0; j < size; j++) + std::cout << cross[i][j]; + + std::cout << '\n'; + } +} + +// check if given row and column are filled with the same elements +bool check_word(char cross[][MAX], int size, int row, int col) { + + for (size_t i = 0; i < size -1; i++) + if (cross[row][i] != cross[i][col]) + return false; + + return true; +} + +// check how many rows and columns match +int check_cross(char cross[][MAX], int size) { + + int result = 0; + for (size_t i = 0; i < size - 1; i++) { + for (size_t j = 0; j < size - 1; j++) + if (check_word(cross, size, i, j)) + result++; + } + + return result; +} + +int main() { + + char cross[MAX][MAX]; + int size; + std::cin >> size; + fill(cross, size); + + std::cout << "\nThe cross looks: \n"; + print(cross, size); + std::cout << "\nYou have: " << check_cross(cross, size) << " matching words! \n"; + + return 0; +} \ No newline at end of file diff --git a/10_strings/practice/strings_advanced/dictionary.cpp b/10_strings/practice/strings_advanced/dictionary.cpp new file mode 100644 index 0000000..4284389 --- /dev/null +++ b/10_strings/practice/strings_advanced/dictionary.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file dictionary.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for advanced_string tasks from practice 10. + */ + +#include // cin, cout +#include // strlen, strcat + +const unsigned int WORD_CNT = 3; +const unsigned int MAX_INPUT = 1024; + +bool check_dic(const char* dic[], const char* word) { + for (size_t i = 0; i < WORD_CNT; i++) + if (strcmp(word, dic[i]) == 0) // if the word on position i + return true; //in the dicionary is as same as the given one + + return false; +} + +int main() { + + const char* dic[] = { "cats", "dogs", "animals" }; + + char input[MAX_INPUT]; // "it is raining cats and dogs"; + + std::cin.getline(input, MAX_INPUT); //reading the whole input + + //for each word form it, check into the dictionary + + //takes the first word + char* curr_word = strtok(input, " "); + unsigned int how_many = 0; + while (curr_word != nullptr) { + + if (check_dic(dic, curr_word)) + how_many++; + + // takes the next word form input string + curr_word = strtok(nullptr, " "); + } + + std::cout << how_many << '\n'; + + return 0; +} \ No newline at end of file diff --git a/10_strings/practice/strings_advanced/sequence.cpp b/10_strings/practice/strings_advanced/sequence.cpp new file mode 100644 index 0000000..142ba72 --- /dev/null +++ b/10_strings/practice/strings_advanced/sequence.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file sequence.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for advanced_string tasks from practice 10. + */ + +#include +#include + +size_t get_same_symb_cnt(const char* str) { + + size_t cnt = 1; + while (*str != '\0' && *str == *(str + 1)) { + ++str; + ++cnt; + } + + return cnt; +} + +void resize(char*& str, const char* next_symbols) { + + if (str == nullptr) { + str = new char[strlen(next_symbols) + 1]; + strcpy(str, next_symbols); + return; + } + + size_t len = strlen(str); + size_t more_symb = strlen(next_symbols); + char* new_str = new char[len + more_symb + 1]; + strcpy(new_str, str); + strcat(new_str, next_symbols); + + delete[] str; + str = new_str; +} + +char* get_sequence(size_t n) { + + char* result_str = new char[2]; + strcpy(result_str, "1"); + + const char* temp_ptr = result_str; + + char* temp_str = nullptr; + + char temp[32]; + + for (size_t i = 1; i < n; i++) { + + while (*temp_ptr != '\0') { + + size_t symb_cnt = get_same_symb_cnt(temp_ptr); + + temp[0] = '0' + symb_cnt; + temp[1] = *temp_ptr; + temp[2] = '\0'; + + resize(temp_str, temp); + temp_ptr += symb_cnt; + } + delete[] result_str; + result_str = new char[strlen(temp_str) + 1]; + strcpy(result_str, temp_str); + delete[] temp_str; + temp_str = nullptr; + temp_ptr = result_str; + } + + delete[] temp_str; + return result_str; +} + +int main() { + + char* sequence = get_sequence(5); + + delete[] sequence; + return 0; +} diff --git a/10_strings/practice/strings_advanced/yoda_talks.cpp b/10_strings/practice/strings_advanced/yoda_talks.cpp new file mode 100644 index 0000000..edbc4b3 --- /dev/null +++ b/10_strings/practice/strings_advanced/yoda_talks.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file yoda_talks.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for advanced_string tasks from practice 10. + */ + +#include +#include // strtok, strlen, strcpy +#include // time +#include // rand + +const unsigned int MAX_INPUT = 1024; + +// how many spaces are out there + 1 +size_t how_many_words(const char* str) { + + if (str == nullptr) + return 0; + + size_t cnt = 1, i = 0; // at least one word + + while (str[i] != '\0') { + + if (str[i] == ' ') + cnt++; + + i++; + } + + return cnt; +} + +void free_words(char**& words, size_t size) { + + for (size_t i = 0; i < size; i++) + delete[] words[i]; + + delete words; + + words = nullptr; +} + +void yoda_talks(char** words, size_t size) { + + // here we will check the taken words + bool* is_taken_word = new (std::nothrow) bool[size]; + + if (is_taken_word == nullptr) + return; + + // mark all as untaken + for (size_t i = 0; i < size; i++) + is_taken_word[i] = false; + + size_t num_taken = 0; + size_t index; + + // while there is untaken word + while (num_taken < size) { + + index = rand() % size; // trying to take this word + + while (is_taken_word[index]) + index = (index + 1) % size; // try the next one, if outsize array, start from the beg + + // mark it as taken + is_taken_word[index] = true; + num_taken++; + + // print it + std::cout << words[index]; + + if(num_taken != size) // no space after the last word + std::cout << ' '; + } + + delete[] is_taken_word; +} + +int main() { + + char buff[MAX_INPUT]; + + std::cin.getline(buff, MAX_INPUT); + + size_t words_cnt = how_many_words(buff); + + // allocating pointer for each string ( word ) + char** words = new char*[words_cnt]; + + // taking the first word from the buff + char* word = strtok(buff, " "); + + int i = 0; + while (word != nullptr) { + + // allocating memory for the current word + words[i] = new (std::nothrow) char[strlen(word) + 1]; + + if (words[i] == nullptr) { + + std::cerr << "can't allocate memory :( !\n"; + free_words(words, i - 1); + return -1; + } + + // copying its content + strcpy(words[i], word); + + i++; + + // taking the next word + word = strtok(nullptr, " "); + } + + // makes us sure that all the memory is successfully allocated and copied + // for (int j = 0; j < i; j++) + // std::cout << words[j] << std::endl; + + srand(time(nullptr)); + + yoda_talks(words, words_cnt); + + free_words(words, words_cnt); + + return 0; +} \ No newline at end of file diff --git a/11_recursion/practice/recursion_advanced/task_01_03.cpp b/11_recursion/practice/recursion_advanced/task_01_03.cpp new file mode 100644 index 0000000..0a8832c --- /dev/null +++ b/11_recursion/practice/recursion_advanced/task_01_03.cpp @@ -0,0 +1,107 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file task_01_03.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for recursion tasks from practice 11. + */ + +#include + +typedef bool(*fptr)(int); + +const size_t MAXN = 100; + +unsigned int addends[MAXN]; + +void print(size_t len) { + + for (size_t i = 1; i < len; i++) + std::cout << addends[i] << '+' ; + + std::cout << addends[len] << '\n'; +} + +void create_sum(unsigned int n, size_t pos) { + + if (n == 0) { + + print(pos - 1); + return; + } + + for (unsigned int k = n; k >= 1; k--) { + + addends[pos] = k; + + if (addends[pos] <= addends[pos - 1]) + create_sum(n - k, pos + 1); + } +} + +void push_back(int*& arr, size_t& size, int elem) { + + int* result = new int[size + 1]; + for (size_t i = 0; i < size; i++) + result[i] = arr[i]; + + result[size] = elem; + delete[] arr; + + arr = result; + size++; +} + +bool is_even(int number) { + return number % 2 == 0; +} + +void print_arr(int* arr, size_t size) { + + for (int i = 0; i < size; ++i) + std::cout << arr[i] << ' '; + + std::cout << std::endl; +} + +void filter_numbers(int*& arr, size_t& size, fptr pred) { + + int* result_arr = nullptr; + size_t new_size = 0; + + for (size_t i = 0; i < size; ++i) { + if (fptr(arr[i])) { + push_back(result_arr, new_size, arr[i]); + new_size++; + } + } + delete[] arr; + + arr = result_arr; + size = new_size; +} + +int main() { + + unsigned int n = 7; + + addends[0] = n + 1; + + create_sum(n, 1); + + size_t size = 10; + int* arr = new int[size]; + for(int i = 0; i < size; ++i) { + arr[i] = i*i; + } + + filter_numbers(arr, size, is_even); + + delete[] arr; + return 0; +} diff --git a/11_recursion/practice/recursion_advanced/task_05.cpp b/11_recursion/practice/recursion_advanced/task_05.cpp new file mode 100644 index 0000000..d237a60 --- /dev/null +++ b/11_recursion/practice/recursion_advanced/task_05.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file task_05.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for recursion tasks from practice 11. + */ + +#include +#include // setw + +const size_t MAXN = 100; +const size_t NUM_TESTS = 10; + +// the focus it not on dynamic memory, so .. +unsigned int matrix[MAXN][MAXN] = { 0 }; + +void print_matrix(size_t k) { + + for (size_t i = 0; i < k; i++) { + for (size_t j = 0; j < k; j++) + std::cout << std::setw(3) + << matrix[i][j]; + + std::cout << std::endl; + } + + std::cout << std::endl + << std::endl + << std::endl + << std::endl; +} + +void fill_matrix(const size_t k, size_t it, size_t cur_size) { + + // only one cell to go + if (it == k * k) { + + matrix[k / 2][k / 2] = it; + return; + } + + // begin index + size_t start_cell = (k - cur_size) / 2; + // end index + size_t border = start_cell + cur_size - 1; + + // down fill + for (size_t i = 0; i < cur_size; i++) + matrix[start_cell + i][start_cell] = it++; + + // right fill -> + for (size_t i = 1; i < cur_size; i++) + matrix[border][start_cell+ i] = it++; + + // up fill + for (size_t i = border - 1; i > start_cell; i--) + matrix[i][border] = it++; + + // left fill <- + for (size_t i = border; i > start_cell; i--) + matrix[start_cell][i] = it++; + + + if (it <= k * k) + fill_matrix(k, it, cur_size - 2); + +} + +int main() { + + unsigned int k = 1; + + while (k <= NUM_TESTS) { + + fill_matrix(k, 1, k); + + print_matrix(k); + + k++; + } + + return 0; +} \ No newline at end of file diff --git a/11_recursion/practice/recursion_basic/tasks_01-02.cpp b/11_recursion/practice/recursion_basic/tasks_01-02.cpp index f4f1060..c6c1eb9 100644 --- a/11_recursion/practice/recursion_basic/tasks_01-02.cpp +++ b/11_recursion/practice/recursion_basic/tasks_01-02.cpp @@ -1,4 +1,17 @@ -#include +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file task_01-02.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for recursion tasks from practice 11. + */ + +#include const int MAX = 12; diff --git a/11_recursion/practice/recursion_basic/tasks_03-04.cpp b/11_recursion/practice/recursion_basic/tasks_03-04.cpp index af39921..92149ea 100644 --- a/11_recursion/practice/recursion_basic/tasks_03-04.cpp +++ b/11_recursion/practice/recursion_basic/tasks_03-04.cpp @@ -1,4 +1,17 @@ -#include +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file task_03-04.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for recursion tasks from practice 11. + */ + +#include /*************** Task 3 ***************/ // print the binary representation of a number diff --git a/11_recursion/practice/recursion_basic/tasks_05-06.cpp b/11_recursion/practice/recursion_basic/tasks_05-06.cpp index 0411eaa..8476b62 100644 --- a/11_recursion/practice/recursion_basic/tasks_05-06.cpp +++ b/11_recursion/practice/recursion_basic/tasks_05-06.cpp @@ -1,4 +1,17 @@ -#include +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file task_05-06.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for recursion tasks from practice 11. + */ + +#include // PAY ATTENTION how swapping two code lines changes the whole program /*************** Task 5 ***************/ diff --git a/11_recursion/practice/recursion_basic/tasks_07-08.cpp b/11_recursion/practice/recursion_basic/tasks_07-08.cpp index c30f357..0159a51 100644 --- a/11_recursion/practice/recursion_basic/tasks_07-08.cpp +++ b/11_recursion/practice/recursion_basic/tasks_07-08.cpp @@ -1,3 +1,16 @@ +/**************************************************************************** + * This file is part of the "Introduction to programming" course. FMI 2019/20 + *****************************************************************************/ + + /** + * @file task_07-08.cpp + * @author Ivan Filipov + * @author Kristian Krastev + * @author Vasilena Peycheva + * @date 01.2020 + * @brief Solution for recursion tasks from practice 11. + */ + #include /*************** Task 7 ***************/ diff --git a/practical_tips/debugging_tips/debugger.md b/practical_tips/debugging_tips/debugger.md new file mode 100644 index 0000000..8d8c7d0 --- /dev/null +++ b/practical_tips/debugging_tips/debugger.md @@ -0,0 +1,7 @@ +# Debugging + +[Debugging Basics](https://docs.microsoft.com/en-us/visualstudio/debugger/debugging-absolute-beginners?view=vs-2019) + +[How to work with the debugger (MSVS)](https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019&fbclid=IwAR2jRhtIVDZXJzKH2X2LRmmxN1cT73xL74a9PYePzei-808_VigOD5WZuyI) + +[![Why debugging is Important](http://i3.ytimg.com/vi/2yXYNxNgJek/maxresdefault.jpg)](https://www.youtube.com/watch?v=2yXYNxNgJek&fbclid=IwAR1kowqfq-HAaClhMP5jRZPf6X3wNFRSI0q_deyblTyLIdh13pL7KftNSZs)