Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <cstdlib>
  4.  
  5. #include <ctime>
  6.  
  7. #include <math.h>
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12. int n;
  13. int userChoice;
  14. int lowestValue;
  15. int highestValue;
  16. double* arr;
  17. cout << "Array element count:" << endl;
  18. cin >> n;
  19. arr = new double[n];
  20.  
  21. cout << "1 - manual 2 - auto" << endl;
  22. cin >> userChoice;
  23.  
  24. if (userChoice == 2) {
  25. do {
  26. cout << "Minimal element" << endl;
  27. cin >> lowestValue;
  28. cout << "Maximal element" << endl;
  29. cin >> highestValue;
  30. } while (lowestValue > highestValue);
  31. }
  32. srand(time(NULL));
  33. for (int i = 0; i < n; i++) {
  34.  
  35. if (userChoice == 1) {
  36. cout << "Enter " << i + 1 << " array element:" << endl;
  37. cin >> arr[i];
  38. }
  39. if (userChoice == 2) {
  40. arr[i] = lowestValue + (rand() % (highestValue * 100 - lowestValue * 100 + 1) / 100.000);
  41.  
  42. }
  43. }
  44.  
  45. for (int i = 0; i < n; i++) {
  46. cout << " | " << arr[i] << " ";
  47. }
  48.  
  49. int maxValue = 0;
  50. int maxIndex;
  51. for (int i = 0; i < n; i++) {
  52. if (arr[i] > maxValue) {
  53. maxValue = arr[i];
  54. maxIndex = i;
  55. }
  56. }
  57. cout << "Max value index: " << maxIndex << endl;
  58.  
  59. int zeroCount = 0;
  60. double reiz = 0;
  61.  
  62. for (int x = 0; x < n; x++) {
  63. if (arr[x] == 0) {
  64.  
  65. zeroCount++;
  66. continue;
  67. }
  68. if (zeroCount == 1) {
  69. reiz *= arr[x];
  70. }
  71. else if (zeroCount == 2) {
  72. break;
  73. }
  74.  
  75. }
  76.  
  77. cout << "res: " << reiz << endl;
  78.  
  79. int i, j, pass = 0;
  80. double temp;
  81. bool done = false;
  82. int lastIndex = 0;
  83.  
  84. while (!done) {
  85. lastIndex = 0;
  86. for (int i = 0; i < n - 1; i++) {
  87. if ((int)arr[i] % 2 == 0 && (int)arr[i + 1] % 2 != 0) {
  88. temp = arr[i + 1];
  89. arr[i + 1] = arr[i];
  90. arr[i] = temp;
  91. break;
  92. }
  93. else {
  94. lastIndex++;
  95. }
  96.  
  97. if (lastIndex == n - 1) {
  98. done = true;
  99. }
  100. }
  101. }
  102.  
  103. cout << endl;
  104.  
  105.  
  106. for (int x = 0; x < n; x++) {
  107. cout << arr[x] << ", ";
  108. }
  109.  
  110. delete[] arr;
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement