Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions sem2/ZolotovskiiED/homework_4/mini_hw_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <algorithm>
#include <fstream>
#include <iterator>
#include <numeric>
#include <random>
#include <vector>

int main() {
std::ofstream file_output("output.txt");

std::random_device rd;
std::mt19937 gen(rd());

std::uniform_int_distribution<int> distrib(-50, 100);

file_output << "Исходный вектор:\n";

std::vector<int> start_lines(20);

std::generate(start_lines.begin(), start_lines.end(), [&]() {
return distrib(gen);
});

std::for_each(start_lines.begin(), start_lines.end(), [&file_output](int char_vec) {
file_output << char_vec << " ";
});

file_output << "\n\nМинимальное и максимальное число:\n";
auto min_max = std::minmax_element(start_lines.begin(), start_lines.end());
file_output << *min_max.first << " " << *min_max.second;

file_output << "\n\nОтсортированный по возрастанию вектор:\n";
std::sort(start_lines.begin(), start_lines.end(), [](int a, int b) {
return a < b;
});
std::for_each(start_lines.begin(), start_lines.end(), [&file_output](int char_vec) {
file_output << char_vec << " ";
});

file_output << "\n\nКоличество положительных, отрицательных и нулевых элементов:\n";
int pos = std::count_if(start_lines.begin(), start_lines.end(), [](int x) {
return x > 0;
});

int neg = std::count_if(start_lines.begin(), start_lines.end(), [](int x) {
return x < 0;
});

int zero = std::count_if(start_lines.begin(), start_lines.end(), [](int x) {
return x == 0;
});
file_output << pos << " " << neg << " " << zero;

file_output << "\n\nСреднее арифметическое всех элементов:\n";
file_output << std::accumulate(start_lines.begin(), start_lines.end(), 0.0) / 20;

file_output << "\n\nОтсортированный вектор без чётных чисел:\n";
std::vector<int> odd_vector = start_lines;
odd_vector.erase(std::remove_if(odd_vector.begin(), odd_vector.end(), [](int x) {
return x % 2 == 0;
}),
odd_vector.end());

std::for_each(odd_vector.begin(), odd_vector.end(), [&file_output](int char_vec) {
file_output << char_vec << " ";
});

file_output << "\n\nВектор без чётных чисел с уникальными значениями:\n";
std::sort(odd_vector.begin(), odd_vector.end());

std::vector<int> unique_vector = odd_vector;
unique_vector.erase(std::unique(unique_vector.begin(), unique_vector.end()),
unique_vector.end());

std::for_each(unique_vector.begin(), unique_vector.end(), [&file_output](int char_vec) {
file_output << char_vec << " ";
});

file_output.close();

return 0;
}
20 changes: 20 additions & 0 deletions sem2/ZolotovskiiED/homework_4/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Исходный вектор:
23 -20 9 80 76 -44 68 47 96 -11 97 8 -3 -7 -3 48 -42 84 43 70

Минимальное и максимальное число:
-44 97

Отсортированный по возрастанию вектор:
-44 -42 -20 -11 -7 -3 -3 8 9 23 43 47 48 68 70 76 80 84 96 97

Количество положительных, отрицательных и нулевых элементов:
13 7 0

Среднее арифметическое всех элементов:
30.95

Отсортированный вектор без чётных чисел:
-11 -7 -3 -3 9 23 43 47 97

Вектор без чётных чисел с уникальными значениями:
-11 -7 -3 9 23 43 47 97