Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int count_if(void *base, size_t num, size_t size, int(*pred)(const void *)) {
- int count = 0;
- void *elem = base;
- for (int i = 1; i < num + 1; i++) {
- if (pred(elem) == 1)
- count++;
- elem = base + i * size;
- }
- return count;
- }
- int pred_int(const void *base){
- int elem = *(int*)base;
- if(abs(elem)%2==0)
- return 1;
- return 0;
- }
- int pred_double(const void *base) {
- double elem = *(double *) base;
- if (((int)abs(elem) % 2 == 0) && (((fabs(elem) - abs((int) elem))) < 0.0000001)) {
- return 1;
- }
- return 0;
- }
- int main(){
- int n = 20;
- int size = 0;
- scanf("%d", &size);
- if (size==4){
- int input_array[n];
- for(int i = 0; i<n; i++)
- scanf("%d", &input_array[i]);
- int p = count_if(input_array, n, sizeof(int), pred_int);
- printf("%d", p);
- }
- if (size==8){
- double input_array[n];
- for(int i = 0; i<n; i++)
- scanf("%lf", &input_array[i]);
- int p = count_if(input_array, n, sizeof(double), pred_double);
- printf("%d", p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement