Advertisement
parad0xxxxx

LW5 mine

Jan 24th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int arraySize, randRangeFrom, randRangeTo;
  10. cout << "Enter element count: ";
  11. cin >> arraySize;
  12. if (arraySize < 1) {
  13. cout << "\nCount must be posivive integer.\n\n";
  14. return 0;
  15. }
  16.  
  17. float* arr = new float[arraySize];
  18. char v(0);
  19. cout << "\n\nHow would You like to fill in array, (M)anually or (A)utomatically? \n:";
  20. cin >> v;
  21.  
  22. if (v == 'M') {
  23. cout << "\nnice =)\n\n";
  24.  
  25. for (int i = 0; i < arraySize; i++)
  26. {
  27. cout << "Enter element " << i + 1 << ": ";
  28. cin >> arr[i];
  29. }
  30.  
  31. }
  32.  
  33. else if (v == 'A') {
  34. cout << "\nYou\'re lazy =)\n\n";
  35.  
  36. cout << "Enter initial range value: ";
  37. cin >> randRangeFrom;
  38.  
  39. cout << "Enter end range value: ";
  40. cin >> randRangeTo;
  41. cout << endl;
  42.  
  43. srand(time(NULL));
  44. for (int i = 0; i < arraySize; i++)
  45. {
  46. arr[i] = (rand() % (randRangeTo * 10 - randRangeFrom * 10 + 1) + randRangeFrom * 10) / 10.0;
  47. }
  48.  
  49. }
  50.  
  51. else {
  52. cout << "\nI expected \"M\" or \"A\".\nCome on, Cutie.\n\n";
  53. return 0;
  54.  
  55. }
  56.  
  57. cout << "Your array is ready and served:\n";
  58. for (int i = 0; i < arraySize; i++)
  59. {
  60. cout << arr[i] << " ";
  61. }
  62.  
  63. cout << "\n\nNow starting our Variant 9 tasks:\n\n\n1. Find maximum array element by the module.\n";
  64.  
  65. float max = fabs(arr[0]);
  66. for (int i = 1; i < arraySize; i++)
  67. {
  68. if (max < fabs(arr[i])) max = fabs(arr[i]);
  69. }
  70. cout << "Maximum array element by the module is: " << max;
  71.  
  72. cout << "\n\n\n2. Calculate the sum of the elements between the first and second positive elements of the array.\nI really hope You mean (), not [] :P\n";
  73.  
  74. bool isPositiveFound = false;
  75. bool isSummarizingFinished = false;
  76. float result = 0;
  77.  
  78. for (int i = 0; i < arraySize; i++) {
  79. if (arr[i] >= 0) {
  80. if (isPositiveFound) {
  81. isSummarizingFinished = true;
  82. }
  83. else {
  84. isPositiveFound = true;
  85. }
  86. }
  87. else {
  88. if (isPositiveFound && !isSummarizingFinished) {
  89. result += arr[i];
  90. }
  91. }
  92. }
  93.  
  94. if (!isSummarizingFinished) {
  95. result = 0;
  96. }
  97.  
  98. cout << "The sum of the elements is: " << result;
  99.  
  100.  
  101. cout << "\n\n\n3. Reorder the array so that zero value elements are placed after all others.\n";
  102.  
  103. for (int i = 0; i < arraySize; i++)
  104. {
  105. if (arr[i] == 0)
  106. {
  107. for (int j = i + 1; j < arraySize; j++)
  108. if (arr[j] != 0 && arr[i] == 0)
  109. {
  110. float tmp = arr[j];
  111. arr[j] = arr[i];
  112. arr[i] = tmp;
  113. }
  114.  
  115. }
  116. cout << arr[i] << " ";
  117. }
  118. cout << "<-- It is sorted now. \n\nIf there were zeroes, they are at the end of array." << endl;
  119.  
  120. cout << "\n\nThank You for attention. Cya! =)" << endl;
  121.  
  122. delete[] arr;
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement