Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int bsearch(vector <int>, int);
- void check(int&);
- void main()
- {
- vector <int> myvector;
- int n, element;
- cout << "Input array size: ";
- check(n);
- cout << "Input elements of array: ";
- for (int i = 0; i < n; i++)
- {
- check(element);
- myvector.push_back(element);
- }
- sort(myvector.rbegin(), myvector.rend());
- cout << "Your sorted array: ";
- for (size_t i = 0; i < myvector.size(); i++)
- cout << myvector[i] << ' ';
- int x, index;
- cout << "\nInput X: ";
- check(x);
- index = bsearch(myvector, x);
- if (index >= 0)
- cout << "Index of first element that is lesser than X: " << index << endl;
- else
- cout << "Elemts lesser than X were not found!\n";
- system("pause");
- }
- void check(int& value)
- {
- #undef max
- bool flag;
- do
- {
- try
- {
- flag = false;
- cin >> value;
- if (!cin || cin.peek() != '\n' && cin.peek() != ' ')
- throw - 1;
- }
- catch (int)
- {
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- flag = true;
- cout << "\tWrong format!\tTry once again...\n\t";
- }
- } while (flag);
- }
- int bsearch(vector <int> myvector, int x)
- {
- for (size_t i = 0; i < myvector.size(); i++)
- if (myvector[i] < x)
- return i;
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement