source: Deliverables/D2.2/8051/tests/tmp_tests/Frontend/search.c @ 1525

Last change on this file since 1525 was 1525, checked in by ayache, 9 years ago

D2.2: function pointers using JMP.

File size: 576 bytes
Line 
1#define SIZE 5
2#define NB_FINDS 2
3
4// Searching for the values below
5int to_find[NB_FINDS] = {57, -1};
6
7int 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
20int 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    print_sint(search(tab, SIZE, to_find[i]));
27    space();
28  }
29  newline();
30
31  return 0;
32}
Note: See TracBrowser for help on using the repository browser.