C++
- voluntas
- @ignis_fatuus
- ブン
- @Linda_pp
- 清楚なC++メイドBOT
- @tzik_tack
- 長谷川一輝
- wraith13
- @jj1bdx
- @cpp_akira
- 安藤敏彦
- @srz_zumix
- Siv3D
- @okdshin
- @hnokx
- @ishidakei
- @take_cheeze
- TAKEI Yuya
- @mumumu
- I (@wx257osn2)
- Tommy6
- わたやん
- @KorekaraSEDB
- @kariya_mitsuru
- @ciniml
- @beam2d
- @grafi_tt
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
69
1
2
using namespace std;3
4
float binarySearch(float arr[], int left, int right, float x)5
{6
while (left <= right)7
{8
int mid = left + (right - left) / 2;9
10
if (arr[mid] == x)11
{12
return mid;13
}14
else if (arr[mid] > x)15
{16
left = mid + 1;17
}18
else19
{20
right = mid - 1;21
}22
}23
24
return -1;25
}26
27
int main()28
{29
float num;30
float output;31
32
float myarr[100] = {44.64978683, 44.62352548, 44.5972165, 44.57086043, 44.54445783,33
44.51800925, 44.49151522, 44.46497629, 44.43839299, 44.41176587,34
44.38509546, 44.35838228, 44.33162687, 44.30482976, 44.27799146,35
44.25111251, 44.22419342, 44.1972347, 44.17023688, 44.14320046,36
44.11612596, 44.08901388, 44.06186473, 44.03467901, 44.00745722,37
43.98019985, 43.95290742, 43.9255804, 43.89821929, 43.87082457,38
43.84339674, 43.81593628, 43.78844366, 43.76091937, 43.73336389,39
43.70577768, 43.67816122, 43.65051497, 43.62283941, 43.595135,40
43.56740221, 43.53964148, 43.51185328, 43.48403806, 43.45619628,41
43.42832838, 43.40043481, 43.37251603, 43.34457246, 43.31660456,42
43.28861275, 43.26059748, 43.23255917, 43.20449827, 43.17641519,43
43.14831036, 43.12018421, 43.09203716, 43.06386963, 43.03568203,44
43.00747477, 42.97924828, 42.95100295, 42.92273919, 42.89445742,45
42.86615803, 42.83784141, 42.80950798, 42.78115811, 42.75279222,46
42.72441067, 42.69601387, 42.6676022, 42.63917604, 42.61073577,47
42.58228177, 42.55381441, 42.52533407, 42.49684112, 42.46833593,48
42.43981886, 42.41129027, 42.38275054, 42.35420001, 42.32563904,49
42.29706799, 42.26848721, 42.23989705, 42.21129785, 42.18268996,50
42.15407372, 42.12544947, 42.09681755, 42.06817829, 42.03953203,51
42.01087909, 41.98221981, 41.9535545, 41.9248835, 41.89620711};52
53
//while(true)54
{55
cout << "Please enter an element to search" << endl;56
cin >> num;57
58
output = binarySearch(myarr, 0, 99, num);59
60
if (output == -1)61
{62
cout << "No Match Found" << endl;63
}64
else65
{66
cout << "Match found at position: " << output << endl;67
}68
}69
}$ g++ prog.cc -Wall -Wextra -std=c++98 -pedantic
Stdin
44.03467901Start
Please enter an element to search Match found at position: 23
0
Finish