Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <iterator>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <numeric>
  9.  
  10. using namespace std;
  11.  
  12. float get_rand()
  13. {
  14.    return rand() / (rand() + 0.1f) - rand() / (rand() + 1);
  15. }
  16.  
  17. int main()
  18. {
  19.    setlocale(LC_ALL, "Russian");
  20.    
  21.    int N = 0;
  22.    cout << "Кол-во в массиве: ";
  23.    cin >> N;
  24.  
  25.    if (N < 2)
  26.    {
  27.       cout << "\nНеобходимо больше элементов";
  28.       return 0;
  29.    }
  30.  
  31.    srand(time(0));
  32.  
  33.    vector<float> a(N, 0.0f);
  34.    generate_n(a.begin(), N, get_rand);
  35.  
  36.    cout << "Элементы: " << endl;
  37.    cout << fixed << setprecision(2);   // %0.2f
  38.  
  39.    for (int i = 0; i < N; i++)
  40.    {
  41.       cout << a[i] << " ";
  42.    }
  43.  
  44.    int c = 0;
  45.    for (int i = 0; i < N; ++i)
  46.    {
  47.       c += a[i] < 0;
  48.    }
  49.  
  50.    vector<float>::const_iterator itMin = a.begin();
  51.  
  52.    for (vector<float>::const_iterator it = a.begin() + 1; it != a.end(); ++it)
  53.    {
  54.       if (abs(*it) < abs(*itMin))
  55.       {
  56.          itMin = it;
  57.       }
  58.    }
  59.  
  60.    const float summa = accumulate(a.begin(), a.end(), 0.0f);
  61.  
  62.    cout << endl << "1) Количество отрицательных элементов массива: " << c;
  63.    cout << endl << "2) Сумма модулей элементов массива, расположенных после минимального по модулю элемента = " << summa;
  64.    cout << endl << "3) Отфильтрованный массив: " << endl;
  65.  
  66.    for (int i = 0; i < N; i++)
  67.    {
  68.       if (a[i] < 0) a[i] = a[i] * a[i];
  69.    }
  70.  
  71.    sort(a.begin(), a.end());
  72.  
  73.    for (int i = 0; i < N; i++)
  74.    {
  75.       cout << a[i] << " ";
  76.    }
  77.  
  78.    return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement