Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <time.h>
- #include <math.h>
- #define COUNT 10
- int *lsearch(void* key, void* arr, size_t n, size_t size, int (*cmp)(const void*, const void*)){
- for (int i = 0; i < n * size; i += size){
- if (cmp(key, arr + i) == 0){
- return arr + i;
- }
- }
- return NULL;
- }
- // *(int*)a
- int compare(const void* a, const void* b){
- return *(int*)a - *(int*)b;
- }
- int compareD(const void* a, const void* b){
- if (fabs(a - b) < 0.001){
- return 0;
- } else if (a > b){
- return 1;
- }
- return -1;
- }
- struct rect {
- double w;
- double h;
- };
- // area
- int compareRects(const void* a, const void* b){
- struct rect r1 = *(struct rect*)a;
- struct rect r2 = *(struct rect*)b;
- double area1 = r1.w * r1.h;
- double area2 = r2.w * r2.h;
- if (fabs(area1 - area2) < 0.001){
- return 0;
- } else if (area1 > area2){
- return 1;
- }
- return -1;
- }
- int main(){
- struct rect rectangles[10];
- srand(time(NULL));
- int nums[COUNT];
- for (int i = 0; i < COUNT; i++){
- nums[i] = rand() % 100;
- }
- //qsort(nums, COUNT, sizeof(int), compare);
- for (int i = 0; i < COUNT; i++){
- printf("%d ", nums[i]);
- }
- putchar('\n');
- int key = 20;
- int* ptr = lsearch(&key, nums, COUNT, sizeof(int), compare);
- if (ptr == NULL){
- printf("%d is not found!\n", key);
- } else {
- printf("Element %d is found!\n", key);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement