Advertisement
parad0xxxxx

LW5 Juliette

Jan 24th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 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.  
  20. cout << "\n\nHow would You like to fill in array, (M)anually or (A)utomatically? \n:";
  21. cin >> v;
  22.  
  23. if (v == 'M') {
  24. for (int i = 0; i < arraySize; i++)
  25. {
  26. cout << "Enter element " << i + 1 << ": ";
  27. cin >> arr[i];
  28. }
  29.  
  30. }
  31.  
  32. else if (v == 'A') {
  33. cout << "Enter initial range value: ";
  34. cin >> randRangeFrom;
  35.  
  36. cout << "Enter end range value: ";
  37. cin >> randRangeTo;
  38. cout << endl;
  39.  
  40. srand(time(NULL));
  41. for (int i = 0; i < arraySize; i++)
  42. {
  43. arr[i] = (rand() % (randRangeTo * 10 - randRangeFrom * 10 + 1) + randRangeFrom * 10) / 10.0;
  44. }
  45.  
  46. }
  47.  
  48. else {
  49. cout << "\nI expected \"M\" or \"A\".\n";
  50. return 0;
  51.  
  52. }
  53.  
  54. cout << "Your array is ready:\n";
  55. for (int i = 0; i < arraySize; i++)
  56. {
  57. cout << "(" << i << ") " << arr[i] << " ";
  58. }
  59.  
  60. cout << "\n\nNow starting our Variant 6 tasks:\n\n\n1. Find minimum array element by the module.\n";
  61.  
  62. float min = fabs(arr[0]);
  63. for (int i = 1; i < arraySize; i++)
  64. {
  65. if (min > fabs(arr[i])) min = fabs(arr[i]);
  66. }
  67. cout << "Minimum array element by the module is: " << min;
  68.  
  69. cout << "\n\n\n2. Calculate the sum of the elements between the first and the last positive elements of the array.\n";
  70.  
  71. float sumBetween = 0;
  72. int firstPos = 0, lastPos = 0;
  73. bool isFirstPositiveFound = false;
  74. for (int i = 0; i < arraySize; i++) {
  75. if (arr[i] >= 0) {
  76. if (isFirstPositiveFound) {
  77. lastPos = i;
  78. }
  79. else {
  80. firstPos = i;
  81. isFirstPositiveFound = true;
  82. }
  83. }
  84. }
  85.  
  86. for (int i = firstPos + 1; i < lastPos; i++) {
  87. sumBetween += arr[i];
  88. }
  89.  
  90. cout << "The sum of the elements between the first (" << firstPos << ") and the last (" << lastPos << ") positives is: " << sumBetween;
  91.  
  92. cout << "\n\n\n3. Reorder the array so that zero value elements are placed before all others.\n";
  93.  
  94.  
  95. for (int i = 0; i < arraySize; i++)
  96. {
  97. if (arr[i] == 0)
  98. {
  99. for (int j = i - 1; j >= 0; j--)
  100. if (arr[j] != 0)
  101. {
  102. float tmp = arr[j];
  103. arr[j] = arr[j + 1];
  104. arr[j + 1] = tmp;
  105. }
  106. }
  107. }
  108.  
  109. for (int i = 0; i < arraySize; i++)
  110. {
  111. cout << arr[i] << " ";
  112. }
  113.  
  114. cout << "\n\nIf there were zeroes, they are at the beginning of the array." << endl;
  115.  
  116. delete[] arr;
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement