Advertisement
parad0xxxxx

LW5 intervals resort better

Jan 24th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <time.h>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. int main()
  8. {
  9.  
  10. int n, a, b, c;
  11.  
  12. cout << "Enter the size of the array: " << endl;
  13. cin >> n;
  14. float* arr = new float[n];
  15. cout << "select and enter the number: 1-manual entry, 2-automatic input " << endl;
  16. cin >> a;
  17.  
  18. if (a == 1) {
  19. cout << "Entry the element of array: " << endl;
  20.  
  21.  
  22. for (int i = 0; i < n; i++)
  23. {
  24. cin >> arr[i];
  25. }
  26. cout << endl << "Manual input array: " << endl;
  27. for (int i = 0; i < n; i++)
  28. {
  29. cout << " " << arr[i] << endl;
  30. }
  31.  
  32. }
  33. else if (a == 2) {
  34. cout << endl << "Automatic input array: " << endl;
  35. srand(time(0));
  36.  
  37. for (int i = 0; i < n; i++)
  38. {
  39. float random = ((float)rand() / RAND_MAX) * rand() - rand();
  40. arr[i] = random;
  41. cout << " " << arr[i] << endl;
  42. }
  43. }
  44. else {
  45. cout << "Try again" << endl;
  46. return 0;
  47. }
  48.  
  49.  
  50. cout << endl << "Enter the begining of interval: ";
  51. cin >> b;
  52. cout << "Enter the ending of interval: ";
  53. cin >> c;
  54. cout << endl << "Numbers in the interval:" << endl;
  55. int count = 0;
  56. for (int i = 0; i < n; i++)
  57. {
  58. if (arr[i] >= b && arr[i] <= c) {
  59. cout << arr[i] << "; ";
  60. count++;
  61. }
  62. }
  63. cout << endl << "Count:" << count << endl;
  64.  
  65. float sum = 0;
  66. float max = arr[0];
  67. int arrNumber = 0;
  68. for (int i = 1; i < n; i++)
  69. {
  70. if (max < arr[i]) max = arr[i];
  71. arrNumber = i;
  72. }
  73.  
  74. for (int i = arrNumber + 1; i < n; i++)
  75. {
  76. sum += arr[i];
  77. }
  78. cout << endl << "Max element in array: " << max << endl << "Amount:" << sum << endl;
  79.  
  80.  
  81. float perem;
  82. cout << endl << endl << "Numbers in descending order: " << endl;
  83.  
  84. for (int i = 0; i < n; i++)
  85. {
  86. for (int j = i + 1; j < n; j++)
  87. {
  88. if (fabs(arr[i]) < fabs(arr[j]))
  89. {
  90. perem = arr[i];
  91. arr[i] = arr[j];
  92. arr[j] = perem;
  93. }
  94. }
  95. }
  96.  
  97. for (int i = 0; i < n; i++)
  98. {
  99. cout << arr[i] << " ";
  100. }
  101.  
  102. cout << endl << endl;
  103.  
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement