]> matita.cs.unibo.it Git - pkg-cerco/acc.git/blob - tests/review1/search.c
Imported Upstream version 0.2
[pkg-cerco/acc.git] / tests / review1 / search.c
1 #define SIZE 5
2 #define NB_FINDS 2
3
4 // Searching for the values below
5 int to_find[NB_FINDS] = {57, -1};
6
7 int search (int tab[], int size, int to_find) {
8   int low = 0, high = size-1, i;
9
10   while (high >= low) {
11     i = (high+low) / 2;
12     if (tab[i] == to_find) return i;
13     if (tab[i] > to_find) high = i-1;
14     if (tab[i] < to_find) low = i+1;
15   }
16
17   return (-1);
18 }
19
20 int main () {
21   int tab[SIZE] = {-30, -18, 23, 57, 120};
22   int res;
23   int i;
24
25   for (i = 0 ; i < NB_FINDS ; i++)
26     println(search(tab, SIZE, to_find[i]));
27
28   return 0;
29 }