fork download
  1.  
  2.  
  3. #include <iostream>
  4. #
  5. #include <vector>
  6. using namespace std;
  7.  
  8. int SearchPosition(vector<int>&, int,int);
  9.  
  10. int main(int argc, const char * argv[]) {
  11. vector<int> v;
  12. int input;
  13. int x;
  14. cout<<"Enter the numbers: ";
  15. while(cin>>input){
  16. v.push_back(input);
  17.  
  18. }
  19. sort(v.begin(),v.end());
  20. cout<<"Sorted Array is : ";
  21. for (int i=0; i<v.size(); i++) {
  22. cout<<v[i]<<" ";
  23. }
  24. cout<<endl;
  25. cout<<"Enter the number to be searched or added : ";
  26. cin>>x;
  27. int h = SearchPosition(v,v.size(),x);
  28. if(h!=-1){
  29. cout<<"Position is : "<<h+1;
  30. }
  31. cout<<endl;
  32. return 0;
  33. }
  34.  
  35. int SearchPosition(vector<int>& a,int n,int x){
  36.  
  37. int start=0;
  38. int end=n-1;
  39. int mid;
  40. while(start<=end){
  41. mid = (start + end)/2;
  42.  
  43.  
  44. if(x==a[mid]){
  45. return mid;
  46. cout<<"ddd";
  47. }
  48.  
  49. else if (x<a[mid]){
  50. end=mid-1;
  51. }
  52.  
  53. else{
  54. start=mid+1;
  55. }
  56. }
  57. if(mid==0){
  58. return mid;
  59. }
  60. else if(mid==end){
  61. return (mid+1);
  62. }
  63. else{
  64. return mid;
  65. }
  66. return -1;
  67.  
  68. }
  69.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:19:5: error: use of undeclared identifier 'sort'
    sort(v.begin(),v.end());
    ^
1 error generated.
stdout
Standard output is empty