Advertisement
constk

VectorsPractice

Dec 14th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <vector>
  2. #include <numeric>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <ctime>
  6. #include <iomanip>
  7.  
  8. int main()
  9. {
  10.     using namespace std;
  11.  
  12.     srand(time(NULL));
  13.  
  14.     const int N = 10;
  15.  
  16.     const int MAX = 100;
  17.     const int MIN = -100;
  18.  
  19.     // Инициализируем массив случайными числами
  20.     vector<double> array(N);
  21.     for (double & i : array)
  22.         i = rand() % (MAX - MIN + 1) + MIN;
  23.  
  24.     // Выводим массив на экран
  25.     cout << "array: " << endl;
  26.     for (int i = 0; i != array.size(); ++i)
  27.         cout << "array[" << i << "] = " << array[i] << endl;
  28.  
  29.  
  30.  
  31.     // ====================== Задание 1.1 ======================
  32.  
  33.     cout << endl << "Task 1: " << endl;
  34.  
  35.     // Получаем указатель на максимальный элемент
  36.     vector<double>::iterator iMax = max_element(array.begin(), array.end());
  37.  
  38.     // Находим сумму элементов после максимального
  39.     double sum = accumulate(iMax, array.end(), 0.0);
  40.  
  41.     // Выведем сумму на экран
  42.     cout << "Sum of elements after one at " << (iMax - array.begin()) << " is " << sum << endl << endl;
  43.  
  44.  
  45.  
  46.     // ====================== Задание 1.2 ======================
  47.  
  48.     cout << "Task 2: " << endl;
  49.  
  50.     // Находим среднее арифметическое
  51.     double average = accumulate(array.begin(), array.end(), 0.0) / array.size();
  52.     int deleted = 0;
  53.  
  54.     cout << "Average value is " << average << endl;
  55.  
  56.     // Удаляем элементы, которые меньше чем среднее арифметическое
  57.     for (vector<double>::iterator i = array.begin(); i != array.end();)
  58.         if (*i < average)
  59.         {
  60.             i = array.erase(i);
  61.             ++deleted;
  62.         }
  63.         else
  64.             ++i;
  65.  
  66.     // Дозаписываем массив нулями
  67.     for (int i = 0; i != deleted; ++i)
  68.         array.push_back(0.0);
  69.  
  70.     // Выводим массив на экран
  71.     for (int i = 0; i != array.size(); ++i)
  72.         cout << "array[" << i << "] = " << array[i] << endl;
  73.  
  74.     cout << endl;
  75.  
  76.     // ====================== Задание 1.3 ======================
  77.  
  78.     cout << "Task 3: " << endl;
  79.  
  80.     // Сортируем массив по возрастанию
  81.     sort(array.begin(), array.end());
  82.  
  83.     // Выводим массив на экран
  84.     for (int i = 0; i != array.size(); ++i)
  85.         cout << "array[" << i << "] = " << array[i] << endl;
  86.  
  87.     // ====================== Задание 1.4 ======================
  88.  
  89.     cout << endl << "Task 4: " << endl;
  90.  
  91.     double numberToFind = 0.0;
  92.     cout << "Input number you want to find: " << endl;
  93.     cin >> numberToFind;
  94.  
  95.     auto foundElement = find(array.begin(), array.end(), numberToFind);
  96.     if (foundElement == array.end())
  97.         cout << "Number not found." << endl;
  98.     else
  99.         cout << "Number " << *foundElement << " was found on the " << foundElement - array.begin() << " position" << endl;
  100.  
  101.     system("pause>nul");
  102.     system("cls");
  103.  
  104.     // ====================== Задание 2.1 ======================
  105.  
  106.     vector<vector<double>> matr(N);
  107.  
  108.     // Инициализируем матрицу случайными значениями
  109.     for (auto & row : matr)
  110.         for (int i = 0; i != N; ++i)
  111.             row.push_back(rand() % (MAX - (MIN/10) + 1) + (MIN/10));
  112.  
  113.     // Выводим матрицу на экран
  114.     for (auto row : matr)
  115.     {
  116.         for (auto elem : row)
  117.             cout << setw(4) << elem;
  118.         cout << endl;
  119.     }
  120.  
  121.     // Находим сумму элеметов всех строк, где есть хотя бы один отрицательный элемент
  122.     sum = 0.0;
  123.     int counter = 0;
  124.     for (auto row : matr)
  125.         for(auto elem : row)
  126.             if (elem < 0.0)
  127.             {
  128.                 sum = accumulate(row.begin(), row.end(), sum);
  129.                 ++counter;
  130.                 break;
  131.             }
  132.     cout << "Sum = " << sum << " in " << counter << " rows" << endl;
  133.  
  134.     system("pause>nul");
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement