-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise6.c
48 lines (35 loc) · 974 Bytes
/
exercise6.c
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
//documentation section
/* Exercise 5 - Binary Search*/
//pre-processor section
#include<stdio.h>
//global variable section
int binarySearch(int arr[], int search_value, int low, int high){
while(low <= high){
int mid = low + (high - low) / 2;
if(arr[mid] == search_value){
return mid;
}
if(arr[mid] < search_value){
low = mid + 1;
}
else{
high = mid - 1;
}
}
return -1;
}
//main function section
int main(){
//write here your program
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int n = sizeof(arr)/sizeof(arr[0]);
int search_value = 45;
int result = binarySearch(arr, search_value, 0 , n-1);
if (result == -1){
printf("Element with value %d is not present in arr[]", search_value);
}
else{
printf("Element with value %d is present at index %d", search_value, result);
}
}
//user-defined section