-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_index.cpp
executable file
·61 lines (50 loc) · 1.19 KB
/
find_index.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//function to find out the index of the number just greater than or equal to the selected number in the passed array.
int find_index(long long num, long long temp[], int l, int h){
int mid;
if(num<temp[l])
return -2;
else if(num>temp[h])
return -3;
else{
while(h-l>=0){
mid=(l+h)/2;
if (num>temp[mid]){
l=mid+1;
}
else if(num<temp[mid]){
h=mid-1;
}
else if(temp[mid]==num)
return mid;
}
if(h-l==-1)
return l;
else return -10;
}
}
int main(){
int n;
cout<<"input the len of the array"<<endl;
cin>>n;
long long A[n];
for(int i=0; i<n; i++)
cin>>A[i];
long long num;
char ch='y';
for(int i=0; i<n-1; i++)cout<<A[i]<<" ";
cout<<endl;
while(ch=='y') {
cout<<"Enter number to find the index ";
cin>>num;
cout<<find_index(num, A, 0, n-1);
cout<<"input again ";
cin>>ch;
}
return 0;
}