Stolar228

Untitled

Dec 22nd, 2022
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include <random>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. #define rand_min 0 // m1
  10. #define rand_max 100  // m2
  11.  
  12. // генерирует случайное число в диапазоне от A до B
  13. int random_int(int a, int b) {
  14.   return a + (rand() % ( b - a + 1 ) );
  15. }
  16.  
  17. class Array {
  18. private:
  19.     vector<double> arr;
  20. public:
  21.     Array(int);
  22.     void print();
  23.     void firstTask();
  24.     void secondTask();
  25.     void thirdTask();
  26. };
  27.  
  28. Array::Array(int size) {
  29.     arr = vector<double>();
  30.     for (int i = 0; i < size; i++)
  31.         arr.push_back(random_int(rand_min, rand_max));
  32. }
  33.  
  34. void Array::print() {
  35.     for (int i = 0; i < arr.size(); i++)
  36.         cout << arr[i] << " ";
  37.     cout << endl;
  38. }
  39.  
  40. bool gretter_10(int i) {
  41.     return (i > 10);
  42. }
  43.  
  44. // первое задание (количесов эл больше 10)
  45. void Array::firstTask() {
  46.     int count = count_if(arr.begin(), arr.end(), gretter_10);
  47.     cout << "Количество элементов со значениями больше 10: " << count << endl;
  48. }
  49.  
  50. // второе задание (квадратный корень из всех элементов)
  51. void Array::secondTask() {
  52.     double sqrt_sum = sqrt(accumulate(arr.begin(), arr.end(), 0));
  53.     cout << "Квадратный корень из всех элементов: " << sqrt_sum << endl;
  54. }
  55.  
  56. // третье задание (сумма всех элементов)
  57. void Array::thirdTask() {
  58.     double sum = accumulate(arr.begin(), arr.end(), 0);
  59.     cout << "Сумма всех элементов: " << sum << endl;
  60. }
  61.  
  62. int main() {
  63.     setlocale(LC_ALL, "rus");
  64.     srand(time(NULL));
  65.     int size;
  66.     cout << "Введите размер массива: ";
  67.     cin >> size;
  68.     Array a = Array(size);
  69.     cout << "\nИсходный массив: " << endl;
  70.     a.print();
  71.     a.firstTask();
  72.     a.secondTask();
  73.     a.thirdTask();
  74.  
  75.     return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment