diff --git a/.vs/CMake Overview b/.vs/CMake Overview new file mode 100644 index 0000000..e69de29 diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..e0ab1de --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "x64-Debug (по умолчанию)" +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..985ed40 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,10 @@ +{ + "ExpandedNodes": [ + "", + "\\include", + "\\src", + "\\tests" + ], + "SelectedNode": "\\src\\tasks.cpp", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/h00_cpp_basics-UnluckyDen/v16/.suo b/.vs/h00_cpp_basics-UnluckyDen/v16/.suo new file mode 100644 index 0000000..c08c6a2 Binary files /dev/null and b/.vs/h00_cpp_basics-UnluckyDen/v16/.suo differ diff --git a/.vs/h00_cpp_basics-UnluckyDen/v16/Browse.VC.db b/.vs/h00_cpp_basics-UnluckyDen/v16/Browse.VC.db new file mode 100644 index 0000000..9c4b493 Binary files /dev/null and b/.vs/h00_cpp_basics-UnluckyDen/v16/Browse.VC.db differ diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..e43555a Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/README.md b/README.md index 6f515a9..673ea52 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Задание 0: Основы C++ ## ФИО студента -**TODO - Пожалуйста, добавьте сюда свое ФИО** +Гладких Даниил Сергеевич ## Описание заданий diff --git a/src/tasks.cpp b/src/tasks.cpp index e30fabe..20b5f9d 100644 --- a/src/tasks.cpp +++ b/src/tasks.cpp @@ -12,44 +12,117 @@ using std::copy; // Задание 1 void swap_args(int *lhs, int *rhs) { // напишите код здесь ... + if ((lhs != nullptr) && (rhs != nullptr)) + { + *lhs = *lhs + *rhs; + *rhs = *lhs - *rhs; + *lhs = *lhs - *rhs; + } } // Задание 2 -int **allocate_2d_array(int num_rows, int num_cols, int init_value) { - // напишите код здесь ... - return nullptr; +int** allocate_2d_array(int rows, int cols, int value) +{ + if ((rows >= 1) && (cols >= 1)) + { + int** array_2d = new int* [rows]; + for (int row_num = 0; row_num < rows; row_num++) + { + array_2d[row_num] = new int[cols] {}; + } + for (int row_num = 0; row_num < rows; row_num++) + { + for (int cols_num = 0; cols_num < cols; cols_num++) + { + array_2d[row_num][cols_num] = value; + } + } + return array_2d; + } + else + { + return nullptr; + } } // Задание 3 -bool copy_2d_array(int **arr_2d_source, int **arr_2d_target, int num_rows, int num_cols) { - // напишите код здесь ... - return false; +bool copy_2d_array(int** arr_2d_source, int** arr_2d_target, int num_rows, int num_cols) { + if ((arr_2d_source == nullptr) || (arr_2d_target == nullptr) || (num_rows <= 0) || (num_cols <= 0)) { + return false; + } + else { + for (int i = 0; i < num_rows; i++) { + copy(arr_2d_source[i], arr_2d_source[i] + num_cols, arr_2d_target[i]); + } + } return true; } // Задание 4 void reverse_1d_array(vector &arr) { - // напишите код здесь ... + vector vec; + for (int elem = arr.size() - 1; elem != -1; elem--) { + int temp = arr[elem]; + vec.push_back(arr[elem]); + } + for (int elem = 0; elem < arr.size(); elem++) { + arr[elem] = vec[elem]; + } } + // Задание 5 -void reverse_1d_array(int *arr_begin, int *arr_end) { - // напишите код здесь ... +void reverse_1d_array(int* arr_begin, int* arr_end) { + if ((arr_begin == nullptr) || (arr_end == nullptr)) { + } + else { + while (arr_begin < arr_end) { + int temp = *arr_begin; + *arr_begin = *arr_end; + *arr_end = temp; + arr_begin++; + arr_end--; + } + } } // Задание 6 -int *find_max_element(int *arr, int size) { - // напишите код здесь ... - return nullptr; +int* find_max_element(int* arr, int size) { + if ((arr == nullptr) || (size <= 0)) { + return nullptr; + } + else { + int* max = arr; + for (int i = 0; i < size; i++) { + if (*arr > *max) { + max = arr; + } + arr++; + } + return max; + } + } // Задание 7 -vector find_odd_numbers(vector &arr) { - // напишите код здесь ... - return {}; +vector find_odd_numbers(vector& arr) { + for (int i = 0; i < arr.size(); i++) { + if (arr[i] % 2 == 0) { + arr.erase(arr.begin() + i); + i--; + } + } + return arr; } // Задание 8 -vector find_common_elements(vector &arr_a, vector &arr_b) { - // напишите код здесь ... - return {}; -} +vector find_common_elements(vector& arr_a, vector& arr_b) { + vector array; + for (int i = 0; i < arr_a.size(); i++) { + for (int j = 0;j < arr_b.size(); j++) { + if (arr_a[i] == arr_b[j]) { + array.insert(array.end(), arr_a[i]); + } + } + } + return array; +} \ No newline at end of file