Last change
on this file since 1878 was
717,
checked in by campbell, 10 years ago
|
Clean up Clight examples; better temporary definition of multiply.
|
File size:
501 bytes
|
Rev | Line | |
---|
[717] | 1 | // #include<stdio.h> |
---|
| 2 | |
---|
| 3 | #define SIZE 5 |
---|
| 4 | |
---|
| 5 | // Searching for the value below |
---|
| 6 | #define TO_FIND 57 |
---|
| 7 | |
---|
| 8 | char search (char tab[], char size, char to_find) { |
---|
| 9 | char low = 0, high = size-1, i; |
---|
| 10 | |
---|
| 11 | while (high >= low) { |
---|
| 12 | i = (high+low) / 2; |
---|
| 13 | if (tab[i] == to_find) return i; |
---|
| 14 | if (tab[i] > to_find) high = i-1; |
---|
| 15 | if (tab[i] < to_find) low = i+1; |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | return (-1); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | int main () { |
---|
| 22 | char tab[SIZE] = {0, 18, 23, 57, 120}; |
---|
| 23 | char res = search(tab, SIZE, TO_FIND); |
---|
| 24 | |
---|
| 25 | // printf("%d\n", res); |
---|
| 26 | return res; |
---|
| 27 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.