fork(5) download
  1. #include <iostream> //for cin
  2. #include <random> //this
  3. #include <math.h> //for floor
  4. using namespace std;
  5.  
  6. double fRand(double fMin, double fMax)
  7. {//random gen for double from stackoverflow
  8. double f = (double)rand() / RAND_MAX;
  9. return fMin + f * (fMax - fMin);
  10. }
  11.  
  12. class array_member {
  13. public:
  14. double value;
  15. bool added;
  16. };
  17.  
  18. int main() {
  19. int seed = static_cast<int>(time(0));
  20. srand(seed);
  21. int arrSize = 0;
  22. int cond_C = 0;
  23. int val_cond_C = 0;
  24. int last_negative_element_index = 0;
  25. int val_cond_after_last = 0;
  26. array_member arr[100];
  27. do { //define array size
  28. cin >> arrSize;
  29. }
  30. while (arrSize>100&&arrSize<=0);
  31. cin >> cond_C;
  32. cout.precision(6);
  33. double max = -1000;
  34. int max_index = 0;
  35. for (int i=0;i<arrSize;i++) {
  36. //arr.push_front();
  37. arr[i].added = false;
  38. if (rand() % 100 > 70)
  39. arr[i].value = fRand(-100,100);
  40. else arr[i].value = rand() % 200-100;
  41. //value set
  42. if (arr[i].value>max) {
  43. max = arr[i].value; //finding max arr value
  44. max_index = i;
  45. }
  46. arr[max_index].added = true;
  47. cout << arr[i].value << endl;
  48. if (arr[i].value < cond_C) val_cond_C++;
  49. if (arr[i].value < 0) last_negative_element_index = i;
  50. }
  51. for (int i=last_negative_element_index;i<arrSize;i++) {
  52. if (arr[i].value > 0 && arr[i].value == floor(arr[i].value)) val_cond_after_last++;
  53. }
  54. cout << "Elements lower than C: " << val_cond_C << endl;
  55. cout << "Natural array elements higher than 0 after last negative element: " <<val_cond_after_last<<endl;
  56. cout << "Modified array: " << endl << max << endl;
  57. for (int i=0;i<arrSize;i++) {
  58. if (!arr[i].added && i!=max_index && arr[i].value >= max*0.8 && arr[i].value <=max*1.2) {
  59. cout << arr[i].value << endl;
  60. arr[i].added = true;
  61. }
  62. }
  63. for (int i=0;i<arrSize;i++)
  64. if (!arr[i].added) cout << arr[i].value << endl;
  65. return 0;
  66. }
Success #stdin #stdout 0s 2936KB
stdin
20
15
stdout
-58.3036
58
8.68654
-0.091254
-5
49
-15
-84
-25
-93
47
64.9779
0
-89
-99.9433
28
-81.9669
82
-24
-87
Elements lower than C: 14
Natural array elements higher than 0 after last negative element: 0
Modified array: 
82
-58.3036
58
8.68654
-0.091254
-5
49
-15
-84
-25
-93
47
64.9779
0
-89
-99.9433
28
-81.9669
82
-24
-87