Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- int cmpChar (const void* x, const void* y) { return (int)(*(char*)x > *(char*)y) ? 1 : (int)(*(char*)x < *(char*)y) ? - 1: 0; }
- int cmpInt (const void* x, const void* y) { return *(int*)x > *(int*)y ? 1 : *(int*)x < *(int*)y ? -1 : 0; }
- int cmpDouble (const void* x, const void* y) { return *(double*)x == *(double*)y ? 0 : (*(double*)x > *(double*)y ? 1 : -1); }
- int universalMax(void* arr, int length, int size, int (*func)(const void*, const void*))
- {
- int max = 0;
- for (int i = 1; i < length; i++) max = func((arr + i * size), (arr + max * size)) > 0 ? i : max;
- return max;
- }
- int main ()
- {
- int com;
- char* scan;
- scanf("%d ", &com);
- void* array = malloc(20 * com);
- int (*f)(const void*, const void*);
- switch (com)
- {
- case 1:;
- scan = "%c ";
- f = &cmpChar;
- break;
- case 4:;
- scan = "%d ";
- f = &cmpInt;
- break;
- case 8:;
- scan = "%lf ";
- f = &cmpDouble;
- break;
- }
- for (int i = 0; i < 20; i++) scanf(scan, array + i * com);
- printf("%d\n", universalMax(array, 20, com, f));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement