parad0xxxxx

LW5 odd/even rearrange

Jan 24th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 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=15, randRangeFrom=-20, randRangeTo=30;
  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. v = 'A';
  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 << "(" << i << ") " << 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.  
  102. for (int i = 0; i < arraySize; i++)
  103. {
  104. cout << "(" << i << ") " << arr[i] << " ";
  105. }
  106.  
  107. cout << "\n\n\nRearrange the array so that the first half of the array contains all elements of odd nonpair positions and the second contains all elements of even positions. \n";
  108.  
  109. float s;
  110.  
  111. for (int i = 0, j = 1; j < arraySize; i = i + 1, j = j + 2)
  112. {
  113. s = arr[j];
  114. for (int k = j; k > i; k--)
  115. {
  116. arr[k] = arr[k - 1];
  117. }
  118. arr[i] = s;
  119. }
  120.  
  121. for (int i = 0; i < arraySize; i++)
  122. {
  123. cout << arr[i] << " ";
  124. }
  125.  
  126. delete[] arr;
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment