Guest User

Untitled

a guest
Oct 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6. void bubblesort(int *arr, int n)
  7. {
  8. cout << "Отсортированный по убыванию массив: " << endl;
  9. for (int i = 1; i < n; ++i)
  10. {
  11. for (int j = 0; j < n - i; j++)
  12. {
  13. if (arr[j] < arr[j + 1])
  14. {
  15.  
  16. int temp = arr[j];
  17. arr[j] = arr[j + 1];
  18. arr[j + 1] = temp;
  19. }
  20. }
  21. }
  22. for (int i = 0; i < n; i++)
  23. {
  24. cout << arr[i] << " ";
  25. }
  26. }
  27. int bsearch(int *arr, int X, int n)
  28. {
  29. int index=0;
  30. cout << "Индекс первого элемента массива, строго меньшего Х: " << endl;
  31. for (int j = 0; j < n; j++)
  32. {
  33. if (arr[j] < X)
  34. {
  35. index = j;
  36. cout << endl;
  37. break;
  38. }
  39. }
  40. if (index < 0)
  41. return -1;
  42. return index;
  43. }
  44. int main()
  45. {
  46. setlocale(LC_ALL, "Rus");
  47. int *arr;
  48. int n, x, m;
  49. srand(time(0));
  50. cout << "Введите размерность массива: " << endl;
  51. cin >> n;
  52. if (n <= 0) {
  53. cerr << "Неверное значение размерности массива!" << endl;
  54. return 1;
  55. }
  56. arr = new int[n];
  57. cout << "Выберите вариант заполнения массива: " << endl;
  58. cout << " 1. Случайными числами." << endl;
  59. cout << " 2. Ввод значений вручную. " << endl;
  60. cin >> m;
  61. switch (m)
  62. { case 1:
  63. for (int i = 0; i < n; i++)
  64. {
  65. arr[i] = rand() % 100;
  66. }
  67. bubblesort(arr, n);
  68. cout << endl;
  69. break;
  70. case 2:
  71. cout << "Введите элементы массива: " << endl;
  72. for (int i = 0; i < n; i++)
  73. {
  74. cin >> arr[i];
  75.  
  76. }
  77. bubblesort(arr, n);
  78. cout << endl;
  79. default:
  80. break;
  81. }
  82. cout << "Введите число X: " << endl;
  83. cin >> x;
  84. for (int i=0; i<n; i++)
  85. if (arr[i] > x)
  86. {
  87.  
  88. break;
  89. }
  90. cout<<bsearch(arr, x, n)<<endl;
  91. delete[] arr;
  92. system("pause > NULL");
  93.  
  94. }
Add Comment
Please, Sign In to add comment