Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. int bsearch(vector <int>, int);
  6. void check(int&);
  7. void main()
  8. {
  9. vector <int> myvector;
  10. int n, element;
  11. cout << "Input array size: ";
  12. check(n);
  13. cout << "Input elements of array: ";
  14. for (int i = 0; i < n; i++)
  15. {
  16. check(element);
  17. myvector.push_back(element);
  18. }
  19. sort(myvector.rbegin(), myvector.rend());
  20. cout << "Your sorted array: ";
  21. for (size_t i = 0; i < myvector.size(); i++)
  22. cout << myvector[i] << ' ';
  23. int x, index;
  24. cout << "\nInput X: ";
  25. check(x);
  26. index = bsearch(myvector, x);
  27. if (index >= 0)
  28. cout << "Index of first element that is lesser than X: " << index << endl;
  29. else
  30. cout << "Elemts lesser than X were not found!\n";
  31. system("pause");
  32. }
  33.  
  34. void check(int& value)
  35. {
  36. #undef max
  37. bool flag;
  38. do
  39. {
  40. try
  41. {
  42. flag = false;
  43. cin >> value;
  44. if (!cin || cin.peek() != '\n' && cin.peek() != ' ')
  45. throw - 1;
  46. }
  47. catch (int)
  48. {
  49. cin.clear();
  50. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  51. flag = true;
  52. cout << "\tWrong format!\tTry once again...\n\t";
  53. }
  54. } while (flag);
  55. }
  56.  
  57. int bsearch(vector <int> myvector, int x)
  58. {
  59. for (size_t i = 0; i < myvector.size(); i++)
  60. if (myvector[i] < x)
  61. return i;
  62. return -1;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement