]> matita.cs.unibo.it Git - pkg-cerco/acc.git/blob - tests/tmp_tests/Backend/search.c
Imported Upstream version 0.2
[pkg-cerco/acc.git] / tests / tmp_tests / Backend / search.c
1 #define SIZE 5
2 #define NB_FINDS 2
3
4 // Searching for the values below
5 signed char to_find[NB_FINDS] = {57, -1};
6
7 signed char search (signed char tab[], signed char size, signed char to_find) {
8   signed char 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 signed char main () {
21   signed char tab[SIZE] = {-30, -18, 23, 57, 120};
22   signed char res;
23   signed char i;
24
25   for (i = 0 ; i < NB_FINDS ; i++) {
26     print_schar(search(tab, SIZE, to_find[i]));
27     space();
28   }
29   newline();
30
31   return 0;
32 }