fork download
  1. //(c)Terminator
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. int _rand(void){ return -17 + (rand() % 35); }
  8.  
  9.  
  10.  
  11. int main(void){
  12. const size_t N = 17;
  13. int arr[N];
  14.  
  15. //1-ое задание
  16. generate(arr, arr + N, _rand);
  17. copy(arr, arr + N, ostream_iterator<int>(cout, " "));
  18. cout << endl << endl << endl;
  19.  
  20.  
  21. //2-ое задание
  22. generate(arr, arr + N, _rand);
  23. copy(arr, arr + N, ostream_iterator<int>(cout, " "));
  24.  
  25. double avg = 0.0;
  26. int cnt = 0;
  27. const int* ptr = &arr[0];
  28. while(ptr != arr + N){
  29. if(*ptr < 0){
  30. avg += (double)*ptr;
  31. ++cnt;
  32. }
  33. ++ptr;
  34. }
  35.  
  36. avg /= (double)cnt;
  37. cout << "\nAVG: " << avg;
  38. return 0;
  39. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
-9 -6 -15 -12 -9 -7 14 -15 12 14 -15 -5 3 -13 -4 4 -7 


-16 -15 -1 14 11 -5 -8 -15 -12 -5 -4 5 -12 2 -10 0 16 
AVG: -9.36364