Advertisement
frustration

DONE. одномерный массив функция. вариант 1

May 17th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. /*В одномерном массиве, состоящем из н вещ элементов, вычислить:
  2. 1)сумму отрицательных элементов
  3. 2)произведение элементов массива между максимальным и минимальным
  4.  
  5. упорядочить элементы массива по возрастанию*/
  6.  
  7. #include <iostream>
  8. #include <time.h>
  9. #include <conio.h>
  10. #include <cmath>
  11. #include <memory>
  12. #include<stdio.h>
  13.  
  14. using namespace std;
  15.  
  16. void input(float *arr, int N){
  17. for (int i = 0; i < N; i++)
  18. scanf_s("%f", &arr[i]);
  19. }
  20.  
  21. void output(float *arr, int N){
  22. for (int i = 0; i < N; i++)
  23. printf("%6.2f ", arr[i]);
  24.  
  25. printf("\n");
  26. }
  27. void sum(float *arr, int N) {
  28. float sum=0;
  29. for (int i = 0; i < N; ++i) {
  30. if (arr[i] < 0)
  31. sum += arr[i];
  32. }
  33. if (sum==0)
  34. cout << "\n" << "1) array hasn't negative items " << "\n";
  35. else
  36. cout << "\n" << "1) sum of negative numbers: " << sum << "\n";
  37. }
  38. void product(float *arr, int N){
  39. int indmin = 0, indmax = 0;
  40. float product = 1, min, max;
  41. min = arr[0];
  42.  
  43. for (int i = 1; i < N; ++i) {
  44. if (min > arr[i]) {
  45. min = arr[i];
  46. indmin = i;
  47. }
  48. }
  49.  
  50. max = arr[0];
  51.  
  52. for (int i = 1; i < N; ++i) {
  53. if (max < arr[i]) {
  54. max = arr[i];
  55. indmax = i;
  56. }
  57. }
  58.  
  59. if (abs(indmax - indmin) == 1) {
  60. cout << "2) max and min are near" << "\n\n";
  61. }
  62. else if (indmax > indmin){
  63. for (int i = indmin + 1; i < indmax; ++i)
  64. product *= arr[i];
  65. cout << "2) product between min and max:" << product << "\n\n";
  66.  
  67. }
  68. else {
  69. for (int i = indmax + 1; i < indmin; ++i)
  70. product *= arr[i];
  71. cout << "2) product between max and min:" << product << "\n\n";
  72. }
  73. }
  74. void bubble_sort(float *arr, int N){
  75.  
  76. for (int i = 0; i < N - 1; ++i) {
  77. for (int j = 0; j < N - 1; ++j) {
  78. if (arr[j] > arr[j + 1])
  79. swap(arr[j], arr[j + 1]);
  80. }
  81. }
  82. }
  83. void Shell_sort(float *a, int N){
  84. int k, l;
  85. float item;
  86. for (int step = N / 2; step>0; step /= 2)
  87. for (l = step; l < N; ++l) {
  88. item = a[l];
  89. for (k = l; k >= step; k -= step) {
  90. if (item > a[k - step])
  91. a[k] = a[k - step];
  92. else
  93. break;
  94. }
  95. a[k] = item;
  96. }
  97. }
  98.  
  99.  
  100. int main() {
  101. int N;
  102. cout << "size array: ";
  103. cin >> N;
  104. float *arr = new float[N];
  105. cout << "original array: " << "\n";
  106. input(arr, N);
  107. output(arr, N);
  108. sum(arr, N);
  109. product(arr, N);
  110. bubble_sort(arr, N);
  111. cout << "new array after bubble sort" << "\n";
  112. output(arr, N);
  113. Shell_sort(arr, N);
  114. cout << "\n\n" << "new array after Shell sort" << "\n";
  115. output(arr, N);
  116. _getch();
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement