Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int comp(const void *p1, const void *p2) {
- const float nr1 = *(const float*)p1;
- const float nr2 = *(const float*)p2;
- return nr1 - nr2;
- }
- int main(void) {
- unsigned n;
- scanf("%u", &n);
- float *arr = (float*)calloc(sizeof(float), n), x;
- if (arr == NULL) {
- perror("");
- exit(EXIT_FAILURE);
- }
- for (unsigned i = 0; i < n; i++) {
- scanf("%f", arr + i);
- }
- qsort(arr, n, sizeof(float), comp);
- for (unsigned i = 0; i < n; i++) {
- printf("%g ", arr[i]);
- }
- puts("");
- scanf("%f", &x);
- float *p = bsearch(&x, arr, n, sizeof(float), comp);
- if (p != NULL) {
- printf("%g found at pos %ld\n", x, p - arr);
- } else {
- printf("%g not found!\n", x);
- }
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement